Commit 1c3adf5c35668a5990667c1daa8ab88ed4cae0e6

Authored by MoisesMachado
1 parent 2d316bf4

ActionItem261: now all the tests of the GeoKit plugin that depends on sqlite are…

… passing and the extract data script i getting the database settings from a file called database_farejador.yml


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1647 3f533792-8f58-4932-b0fe-aaf55b0a4547
lib/sqlite_extension.rb
... ... @@ -30,11 +30,15 @@ if ActiveRecord::Base.connection.adapter_name =~ /^sqlite$/i
30 30 func.set_result(Math.sqrt(value))
31 31 end
32 32  
33   -# database.create_function('dist', 5, :numeric) do |func, lat1, long1, lat2, long2, radius|
34   -# lat2, long2 = [lat2, long2].map{|l|l * Math::PI/180.0}
35   -# func.set_result = radius * Math.acos([1,
36   -# Math.cos(lat1.to_f) * Math.cos(long1.to_f) * Math.cos(lat2.to_f) * Math.cos(long2.to_f) +
37   -# Math.cos(lat1.to_f) * Math.sin(long1.to_f) * Math.cos(lat2.to_f) * Math.sin(long2.to_f) +
38   -# Math.sin(lat1.to_f) * Math.sin(lat2.to_f)].min)
39   -# end
  33 + database.create_function('spheric_distance', 5, :real) do |func, lat1, long1, lat2, long2, radius|
  34 + func.set_result(
  35 + radius.to_f * Math.acos(
  36 + [1,
  37 + Math.cos(lat1.to_f) * Math.cos(long1.to_f) * Math.cos(lat2.to_f) * Math.cos(long2.to_f) +
  38 + Math.cos(lat1.to_f) * Math.sin(long1.to_f) * Math.cos(lat2.to_f) * Math.sin(long2.to_f) +
  39 + Math.sin(lat1.to_f) * Math.sin(lat2.to_f)
  40 + ].min
  41 + )
  42 + )
  43 + end
40 44 end
... ...
script/extract_sies_data.rb
  1 +#!/usr/bin/ruby
  2 +
1 3 $LOAD_PATH.unshift('/usr/share/rails/activerecord/lib')
2 4 $LOAD_PATH.unshift('/usr/share/rails/activesupport/lib')
3 5  
... ... @@ -6,13 +8,8 @@ require 'active_support'
6 8  
7 9 LIMIT = 5
8 10  
9   -ActiveRecord::Base.establish_connection(
10   - :adapter => 'mysql',
11   - :host => 'localhost',
12   - :database => 'farejador',
13   - :username => 'root',
14   - :password => 'root'
15   -)
  11 +# To connect with the database that contains the data to be extracted cofigure it in the 'database_farejador.yml' with the name 'farejador'
  12 +ActiveRecord::Base.establish_connection(YAML::load(IO.read('database_farejador.yml'))['farejador'])
16 13  
17 14 class Enterprise < ActiveRecord::Base
18 15 set_table_name 'cons_dadosbasicos'
... ...
test/unit/sqlite_extension_test.rb
... ... @@ -32,8 +32,9 @@ class SQliteExtensionTest &lt; Test::Unit::TestCase
32 32 assert_in_delta 1.4142, ActiveRecord::Base.connection.execute('select sqrt(2) as sqrt').first['sqrt'], 0.0001
33 33 end
34 34  
35   -# should 'have a distance function' do
36   -# 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
37   -# end
  35 + should 'have a distance function' do
  36 + args = [32.918593, -96.958444, 32.951613, -96.958444].map{|l|l * Math::PI/180}
  37 + assert_in_delta 2.28402, ActiveRecord::Base.connection.execute("select spheric_distance(#{args.inspect[1..-2]}, 3963.19) as dist").first['dist'], 0.0001
  38 + end
38 39  
39 40 end
... ...