Commit a66e40149479828f37be29df0d9e51650336ee91
1 parent
26982e37
Exists in
master
and in
28 other branches
ActionItem261: modified the geokit plugin to works with sqlites
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1651 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
5 changed files
with
73 additions
and
3 deletions
Show diff stats
vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb
| @@ -390,6 +390,11 @@ module GeoKit | @@ -390,6 +390,11 @@ module GeoKit | ||
| 390 | COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+ | 390 | COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+ |
| 391 | SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier}) | 391 | SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier}) |
| 392 | SQL_END | 392 | SQL_END |
| 393 | + when "sqlite" | ||
| 394 | + # The /1.0 in the end is to force the convertion to float | ||
| 395 | + sql=<<-SQL_END | ||
| 396 | + (SPHERIC_DISTANCE(#{lat},#{lng},RADIANS(#{qualified_lat_column_name}),RADIANS(#{qualified_lng_column_name}),#{multiplier})/1.0) | ||
| 397 | + SQL_END | ||
| 393 | else | 398 | else |
| 394 | sql = "unhandled #{connection.adapter_name.downcase} adapter" | 399 | sql = "unhandled #{connection.adapter_name.downcase} adapter" |
| 395 | end | 400 | end |
| @@ -411,6 +416,11 @@ module GeoKit | @@ -411,6 +416,11 @@ module GeoKit | ||
| 411 | SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{qualified_lat_column_name}),2)+ | 416 | SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{qualified_lat_column_name}),2)+ |
| 412 | POW(#{lng_degree_units}*(#{origin.lng}-#{qualified_lng_column_name}),2)) | 417 | POW(#{lng_degree_units}*(#{origin.lng}-#{qualified_lng_column_name}),2)) |
| 413 | SQL_END | 418 | SQL_END |
| 419 | + when "sqlite" | ||
| 420 | + sql=<<-SQL_END | ||
| 421 | + (SQRT(POW(#{lat_degree_units}*(#{origin.lat}-#{qualified_lat_column_name}),2)+ | ||
| 422 | + POW(#{lng_degree_units}*(#{origin.lng}-#{qualified_lng_column_name}),2))/1.0) | ||
| 423 | + SQL_END | ||
| 414 | else | 424 | else |
| 415 | sql = "unhandled #{connection.adapter_name.downcase} adapter" | 425 | sql = "unhandled #{connection.adapter_name.downcase} adapter" |
| 416 | end | 426 | end |
| @@ -433,4 +443,4 @@ class Array | @@ -433,4 +443,4 @@ class Array | ||
| 433 | end | 443 | end |
| 434 | self.sort!{|a,b|a.send(distance_attribute_name) <=> b.send(distance_attribute_name)} | 444 | self.sort!{|a,b|a.send(distance_attribute_name) <=> b.send(distance_attribute_name)} |
| 435 | end | 445 | end |
| 436 | -end | ||
| 437 | \ No newline at end of file | 446 | \ No newline at end of file |
| 447 | +end |
| @@ -0,0 +1,28 @@ | @@ -0,0 +1,28 @@ | ||
| 1 | +if ActiveRecord::Base.connection.adapter_name =~ /^sqlite$/i | ||
| 2 | + | ||
| 3 | + database = ActiveRecord::Base.connection.raw_connection | ||
| 4 | + | ||
| 5 | + database.create_function('pow', 2, :numeric) do |func, base, exponent| | ||
| 6 | + func.set_result(base.to_f ** exponent.to_f) | ||
| 7 | + end | ||
| 8 | + | ||
| 9 | + database.create_function('radians', 1, :numeric) do |func, value| | ||
| 10 | + func.set_result(value.to_f * Math::PI / 180.0) | ||
| 11 | + end | ||
| 12 | + | ||
| 13 | + database.create_function('sqrt', 1, :numeric) do |func, value| | ||
| 14 | + func.set_result(Math.sqrt(value)) | ||
| 15 | + end | ||
| 16 | + | ||
| 17 | + database.create_function('spheric_distance', 5, :real) do |func, lat1, long1, lat2, long2, radius| | ||
| 18 | + func.set_result( | ||
| 19 | + radius.to_f * Math.acos( | ||
| 20 | + [1, | ||
| 21 | + Math.cos(lat1.to_f) * Math.cos(long1.to_f) * Math.cos(lat2.to_f) * Math.cos(long2.to_f) + | ||
| 22 | + Math.cos(lat1.to_f) * Math.sin(long1.to_f) * Math.cos(lat2.to_f) * Math.sin(long2.to_f) + | ||
| 23 | + Math.sin(lat1.to_f) * Math.sin(lat2.to_f) | ||
| 24 | + ].min | ||
| 25 | + ) | ||
| 26 | + ) | ||
| 27 | + end | ||
| 28 | +end |
vendor/plugins/geokit/test/database.yml
| @@ -9,4 +9,7 @@ postgresql: | @@ -9,4 +9,7 @@ postgresql: | ||
| 9 | host: localhost | 9 | host: localhost |
| 10 | username: root | 10 | username: root |
| 11 | password: | 11 | password: |
| 12 | - database: geokit_plugin_test | ||
| 13 | \ No newline at end of file | 12 | \ No newline at end of file |
| 13 | + database: geokit_plugin_test | ||
| 14 | +sqlite: | ||
| 15 | + adapter: sqlite3 | ||
| 16 | + database: tmp/geokit_plugin_test.sqlite3 |
| @@ -0,0 +1,27 @@ | @@ -0,0 +1,27 @@ | ||
| 1 | +require File.dirname(__FILE__) + '/test_helper' | ||
| 2 | + | ||
| 3 | +if ActiveRecord::Base.connection.adapter_name =~ /^sqlite$/i | ||
| 4 | + | ||
| 5 | + # this test only makes sense when using sqlite | ||
| 6 | + class SQliteExtensionTest < Test::Unit::TestCase | ||
| 7 | + | ||
| 8 | + def test_pow_function | ||
| 9 | + assert_in_delta 8.0, ActiveRecord::Base.connection.execute('select pow(2.0, 3.0) as result').first['result'], 0.0001 | ||
| 10 | + end | ||
| 11 | + | ||
| 12 | + def test_radians_function | ||
| 13 | + assert_in_delta Math::PI/2, ActiveRecord::Base.connection.execute('select radians(90) as rad').first['rad'], 0.0001 | ||
| 14 | + end | ||
| 15 | + | ||
| 16 | + def test_sqrt_function | ||
| 17 | + assert_in_delta 1.4142, ActiveRecord::Base.connection.execute('select sqrt(2) as sqrt').first['sqrt'], 0.0001 | ||
| 18 | + end | ||
| 19 | + | ||
| 20 | + def test_spheric_distance_function | ||
| 21 | + args = [32.918593, -96.958444, 32.951613, -96.958444].map{|l|l * Math::PI/180} | ||
| 22 | + assert_in_delta 2.28402, ActiveRecord::Base.connection.execute("select spheric_distance(#{args.inspect[1..-2]}, 3963.19) as dist").first['dist'], 0.0001 | ||
| 23 | + end | ||
| 24 | + | ||
| 25 | + end | ||
| 26 | + | ||
| 27 | +end |
vendor/plugins/geokit/test/test_helper.rb
| @@ -15,4 +15,6 @@ ActiveRecord::Base.establish_connection(databases[ENV['DB'] || 'mysql']) | @@ -15,4 +15,6 @@ ActiveRecord::Base.establish_connection(databases[ENV['DB'] || 'mysql']) | ||
| 15 | load(File.join(plugin_test_dir, 'schema.rb')) | 15 | load(File.join(plugin_test_dir, 'schema.rb')) |
| 16 | 16 | ||
| 17 | # Load fixtures from the plugin | 17 | # Load fixtures from the plugin |
| 18 | -Test::Unit::TestCase.fixture_path = File.join(plugin_test_dir, 'fixtures/') | ||
| 19 | \ No newline at end of file | 18 | \ No newline at end of file |
| 19 | +Test::Unit::TestCase.fixture_path = File.join(plugin_test_dir, 'fixtures/') | ||
| 20 | + | ||
| 21 | +require File.join(plugin_test_dir, '../../../../lib/sqlite_extension') |