From a66e40149479828f37be29df0d9e51650336ee91 Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Fri, 11 Apr 2008 12:14:16 +0000 Subject: [PATCH] ActionItem261: modified the geokit plugin to works with sqlites --- vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb | 12 +++++++++++- vendor/plugins/geokit/lib/geo_kit/sqlite_extension.rb | 28 ++++++++++++++++++++++++++++ vendor/plugins/geokit/test/database.yml | 5 ++++- vendor/plugins/geokit/test/sqlite_extension_test.rb | 27 +++++++++++++++++++++++++++ vendor/plugins/geokit/test/test_helper.rb | 4 +++- 5 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 vendor/plugins/geokit/lib/geo_kit/sqlite_extension.rb create mode 100644 vendor/plugins/geokit/test/sqlite_extension_test.rb diff --git a/vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb b/vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb index a29f2be..14c81c8 100644 --- a/vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb +++ b/vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb @@ -390,6 +390,11 @@ module GeoKit 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}) SQL_END + when "sqlite" + # The /1.0 in the end is to force the convertion to float + sql=<<-SQL_END + (SPHERIC_DISTANCE(#{lat},#{lng},RADIANS(#{qualified_lat_column_name}),RADIANS(#{qualified_lng_column_name}),#{multiplier})/1.0) + SQL_END else sql = "unhandled #{connection.adapter_name.downcase} adapter" end @@ -411,6 +416,11 @@ module GeoKit SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{qualified_lat_column_name}),2)+ POW(#{lng_degree_units}*(#{origin.lng}-#{qualified_lng_column_name}),2)) SQL_END + when "sqlite" + sql=<<-SQL_END + (SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{qualified_lat_column_name}),2)+ + POW(#{lng_degree_units}*(#{origin.lng}-#{qualified_lng_column_name}),2))/1.0) + SQL_END else sql = "unhandled #{connection.adapter_name.downcase} adapter" end @@ -433,4 +443,4 @@ class Array end self.sort!{|a,b|a.send(distance_attribute_name) <=> b.send(distance_attribute_name)} end -end \ No newline at end of file +end diff --git a/vendor/plugins/geokit/lib/geo_kit/sqlite_extension.rb b/vendor/plugins/geokit/lib/geo_kit/sqlite_extension.rb new file mode 100644 index 0000000..e12b416 --- /dev/null +++ b/vendor/plugins/geokit/lib/geo_kit/sqlite_extension.rb @@ -0,0 +1,28 @@ +if ActiveRecord::Base.connection.adapter_name =~ /^sqlite$/i + + database = ActiveRecord::Base.connection.raw_connection + + database.create_function('pow', 2, :numeric) do |func, base, exponent| + func.set_result(base.to_f ** exponent.to_f) + end + + database.create_function('radians', 1, :numeric) do |func, value| + func.set_result(value.to_f * Math::PI / 180.0) + end + + database.create_function('sqrt', 1, :numeric) do |func, value| + func.set_result(Math.sqrt(value)) + end + + database.create_function('spheric_distance', 5, :real) do |func, lat1, long1, lat2, long2, radius| + func.set_result( + radius.to_f * Math.acos( + [1, + Math.cos(lat1.to_f) * Math.cos(long1.to_f) * Math.cos(lat2.to_f) * Math.cos(long2.to_f) + + Math.cos(lat1.to_f) * Math.sin(long1.to_f) * Math.cos(lat2.to_f) * Math.sin(long2.to_f) + + Math.sin(lat1.to_f) * Math.sin(lat2.to_f) + ].min + ) + ) + end +end diff --git a/vendor/plugins/geokit/test/database.yml b/vendor/plugins/geokit/test/database.yml index c2c6591..126a872 100644 --- a/vendor/plugins/geokit/test/database.yml +++ b/vendor/plugins/geokit/test/database.yml @@ -9,4 +9,7 @@ postgresql: host: localhost username: root password: - database: geokit_plugin_test \ No newline at end of file + database: geokit_plugin_test +sqlite: + adapter: sqlite3 + database: tmp/geokit_plugin_test.sqlite3 diff --git a/vendor/plugins/geokit/test/sqlite_extension_test.rb b/vendor/plugins/geokit/test/sqlite_extension_test.rb new file mode 100644 index 0000000..9b14a65 --- /dev/null +++ b/vendor/plugins/geokit/test/sqlite_extension_test.rb @@ -0,0 +1,27 @@ +require File.dirname(__FILE__) + '/test_helper' + +if ActiveRecord::Base.connection.adapter_name =~ /^sqlite$/i + + # this test only makes sense when using sqlite + class SQliteExtensionTest < Test::Unit::TestCase + + def test_pow_function + assert_in_delta 8.0, ActiveRecord::Base.connection.execute('select pow(2.0, 3.0) as result').first['result'], 0.0001 + end + + def test_radians_function + assert_in_delta Math::PI/2, ActiveRecord::Base.connection.execute('select radians(90) as rad').first['rad'], 0.0001 + end + + def test_sqrt_function + assert_in_delta 1.4142, ActiveRecord::Base.connection.execute('select sqrt(2) as sqrt').first['sqrt'], 0.0001 + end + + def test_spheric_distance_function + args = [32.918593, -96.958444, 32.951613, -96.958444].map{|l|l * Math::PI/180} + assert_in_delta 2.28402, ActiveRecord::Base.connection.execute("select spheric_distance(#{args.inspect[1..-2]}, 3963.19) as dist").first['dist'], 0.0001 + end + + end + +end diff --git a/vendor/plugins/geokit/test/test_helper.rb b/vendor/plugins/geokit/test/test_helper.rb index 5cd8f92..22dfb66 100644 --- a/vendor/plugins/geokit/test/test_helper.rb +++ b/vendor/plugins/geokit/test/test_helper.rb @@ -15,4 +15,6 @@ ActiveRecord::Base.establish_connection(databases[ENV['DB'] || 'mysql']) load(File.join(plugin_test_dir, 'schema.rb')) # Load fixtures from the plugin -Test::Unit::TestCase.fixture_path = File.join(plugin_test_dir, 'fixtures/') \ No newline at end of file +Test::Unit::TestCase.fixture_path = File.join(plugin_test_dir, 'fixtures/') + +require File.join(plugin_test_dir, '../../../../lib/sqlite_extension') -- libgit2 0.21.2