diff --git a/lib/sqlite_extension.rb b/lib/sqlite_extension.rb index 743475a..ce63bb2 100644 --- a/lib/sqlite_extension.rb +++ b/lib/sqlite_extension.rb @@ -30,11 +30,15 @@ if ActiveRecord::Base.connection.adapter_name =~ /^sqlite$/i func.set_result(Math.sqrt(value)) end -# database.create_function('dist', 5, :numeric) do |func, lat1, long1, lat2, long2, radius| -# lat2, long2 = [lat2, long2].map{|l|l * Math::PI/180.0} -# func.set_result = radius * 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 + 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/script/extract_sies_data.rb b/script/extract_sies_data.rb index 03d6896..a74c46b 100644 --- a/script/extract_sies_data.rb +++ b/script/extract_sies_data.rb @@ -1,3 +1,5 @@ +#!/usr/bin/ruby + $LOAD_PATH.unshift('/usr/share/rails/activerecord/lib') $LOAD_PATH.unshift('/usr/share/rails/activesupport/lib') @@ -6,13 +8,8 @@ require 'active_support' LIMIT = 5 -ActiveRecord::Base.establish_connection( - :adapter => 'mysql', - :host => 'localhost', - :database => 'farejador', - :username => 'root', - :password => 'root' -) +# To connect with the database that contains the data to be extracted cofigure it in the 'database_farejador.yml' with the name 'farejador' +ActiveRecord::Base.establish_connection(YAML::load(IO.read('database_farejador.yml'))['farejador']) class Enterprise < ActiveRecord::Base set_table_name 'cons_dadosbasicos' diff --git a/test/unit/sqlite_extension_test.rb b/test/unit/sqlite_extension_test.rb index 958067a..9dc3363 100644 --- a/test/unit/sqlite_extension_test.rb +++ b/test/unit/sqlite_extension_test.rb @@ -32,8 +32,9 @@ class SQliteExtensionTest < Test::Unit::TestCase assert_in_delta 1.4142, ActiveRecord::Base.connection.execute('select sqrt(2) as sqrt').first['sqrt'], 0.0001 end -# should 'have a distance function' do -# assert_in_delta 2.28402, ActiveRecord::Base.connection.execute('select dist(32.918593, -96.958444, 32.895155, -96.958444, 3963.19) as dist').first['dist'], 0.0001 -# end + should 'have a distance function' do + 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 -- libgit2 0.21.2