Permalink
Browse files

SQLite Support

Used callbacks to enable sqlite to perform cnecessary calculations.
  • Loading branch information...
1 parent 9206702 commit 5c09babeda89843fbf2afbe0c36f56ea6df0ca87 @sionide21 sionide21 committed Mar 15, 2011
Showing with 51 additions and 0 deletions.
  1. +51 −0 lib/geokit-rails3/adapters/sqlite.rb
@@ -0,0 +1,51 @@
+module Geokit
+ module Adapters
+ class SQLite < Abstract
+
+ def self.add_numeric(name)
+ @@connection.create_function name, 1, :numeric do |func, *args|
+ func.result = yield(*args)
+ end
+ end
+
+ def self.add_math(name)
+ add_numeric name do |*n|
+ Math.send name, *n
+ end
+ end
+
+ class << self
+ 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 }
+
+ self.loaded = true
+ end
+ end
+
+ def sphere_distance_sql(lat, lng, multiplier)
+ %|
+ (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
+
+ def flat_distance_sql(origin, lat_degree_units, lng_degree_units)
+ %|
+ 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
+ end
+end

0 comments on commit 5c09bab

Please sign in to comment.