Class: Geokit::Adapters::SQLite

Inherits:
Abstract
  • Object
show all
Defined in:
lib/geokit-rails3/adapters/sqlite.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#initialize, #method_missing

Constructor Details

This class inherits a constructor from Geokit::Adapters::Abstract

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Geokit::Adapters::Abstract

Class Method Details

.add_math(name) ⇒ Object



11
12
13
14
15
# File 'lib/geokit-rails3/adapters/sqlite.rb', line 11

def self.add_math(name)
  add_numeric name do |*n|
    Math.send name, *n
  end
end

.add_numeric(name) ⇒ Object



5
6
7
8
9
# File 'lib/geokit-rails3/adapters/sqlite.rb', line 5

def self.add_numeric(name) 
  @@connection.create_function name, 1, :numeric do |func, *args|
    func.result = yield(*args)
  end
end

.load(klass) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/geokit-rails3/adapters/sqlite.rb', line 18

def load(klass)
  @@connection = klass.connection.raw_connection
  # Define the functions needed
  add_math 'sqrt'
  add_math 'cos'
  add_math 'acos'
  add_math 'sin'

  add_numeric('pow') { |n, m| n**m }
  add_numeric('radians') { |n| n * Math::PI / 180 }
  add_numeric('least') { |*args| args.min }
end

Instance Method Details

#flat_distance_sql(origin, lat_degree_units, lng_degree_units) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/geokit-rails3/adapters/sqlite.rb', line 42

def flat_distance_sql(origin, lat_degree_units, lng_degree_units)
  %|
    (CASE WHEN #{qualified_lat_column_name} IS NULL OR #{qualified_lng_column_name} IS NULL THEN NULL ELSE
    SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{qualified_lat_column_name}),2)+
    POW(#{lng_degree_units}*(#{origin.lng}-#{qualified_lng_column_name}),2))
    END)
   |
end

#sphere_distance_sql(lat, lng, multiplier) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/geokit-rails3/adapters/sqlite.rb', line 32

def sphere_distance_sql(lat, lng, multiplier)
  %|
    (CASE WHEN #{qualified_lat_column_name} IS NULL OR #{qualified_lng_column_name} IS NULL THEN NULL ELSE
    (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
    COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+
    SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier})
    END)
   |
end