]> git.openstreetmap.org Git - rails.git/commitdiff
Use a local lookup table for country bounding boxes rather than relying
authorTom Hughes <tom@compton.nu>
Mon, 11 May 2009 16:50:09 +0000 (16:50 +0000)
committerTom Hughes <tom@compton.nu>
Mon, 11 May 2009 16:50:09 +0000 (16:50 +0000)
on geonames.org being able to do it in a reasonable time.

app/models/country.rb [new file with mode: 0644]
db/migrate/031_create_countries.rb [new file with mode: 0644]
lib/osm.rb
test/fixtures/countries.yml [new file with mode: 0644]
test/unit/country_test.rb [new file with mode: 0644]

diff --git a/app/models/country.rb b/app/models/country.rb
new file mode 100644 (file)
index 0000000..b7cec2b
--- /dev/null
@@ -0,0 +1,2 @@
+class Country < ActiveRecord::Base
+end
diff --git a/db/migrate/031_create_countries.rb b/db/migrate/031_create_countries.rb
new file mode 100644 (file)
index 0000000..e4106f4
--- /dev/null
@@ -0,0 +1,36 @@
+require 'lib/migrate'
+
+class CreateCountries < ActiveRecord::Migration
+  def self.up
+    create_table :countries, innodb_table do |t|
+      t.column :id,      :integer_pk,              :null => false
+      t.column :code,    :string,     :limit => 2, :null => false
+      t.column :min_lat, :double,                  :null => false
+      t.column :max_lat, :double,                  :null => false
+      t.column :min_lon, :double,                  :null => false
+      t.column :max_lon, :double,                  :null => false
+    end
+
+    add_index :countries, [:code], :name => "countries_code_idx", :unique => true
+
+    Net::HTTP.start('ws.geonames.org') do |http|
+      xml = REXML::Document.new(http.get("/countryInfo").body)
+
+      xml.elements.each("geonames/country") do |ele|
+        code = ele.get_text("countryCode").to_s
+        minlon = ele.get_text("bBoxWest").to_s
+        minlat = ele.get_text("bBoxSouth").to_s
+        maxlon = ele.get_text("bBoxEast").to_s
+        maxlat = ele.get_text("bBoxNorth").to_s
+
+        Country.create(:code => code,
+                       :min_lat => minlat.to_f, :max_lat => maxlat.to_f,
+                       :min_lon => minlon.to_f, :max_lon => maxlon.to_f)
+      end
+    end
+  end
+
+  def self.down
+    drop_table :countries
+  end
+end
index 0a62d14c0c77890ea67a634db4d0ceb5e6a82700..a392b88a825f54b5b0963b3c97437c8aaf16221b 100644 (file)
@@ -362,16 +362,8 @@ module OSM
       Net::HTTP.start('api.hostip.info') do |http|
         country = http.get("/country.php?ip=#{ip_address}").body
         country = "GB" if country == "UK"
-        Net::HTTP.start('ws.geonames.org') do |http|
-          xml = REXML::Document.new(http.get("/countryInfo?country=#{country}").body)
-          xml.elements.each("geonames/country") do |ele|
-            minlon = ele.get_text("bBoxWest").to_s
-            minlat = ele.get_text("bBoxSouth").to_s
-            maxlon = ele.get_text("bBoxEast").to_s
-            maxlat = ele.get_text("bBoxNorth").to_s
-            return { :minlon => minlon, :minlat => minlat, :maxlon => maxlon, :maxlat => maxlat }
-          end
-        end
+        country = Country.find_by_code(country)
+        return { :minlon => country.min_lon, :minlat => country.min_lat, :maxlon => country.max_lon, :maxlat => country.max_lat }
       end
     end
 
diff --git a/test/fixtures/countries.yml b/test/fixtures/countries.yml
new file mode 100644 (file)
index 0000000..f68d702
--- /dev/null
@@ -0,0 +1,13 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+  min_lat: 
+  max_lat: 
+  min_lon: 
+  max_lon: 
+
+two:
+  min_lat: 
+  max_lat: 
+  min_lon: 
+  max_lon: 
diff --git a/test/unit/country_test.rb b/test/unit/country_test.rb
new file mode 100644 (file)
index 0000000..2adc947
--- /dev/null
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class CountryTest < ActiveSupport::TestCase
+  # Replace this with your real tests.
+  test "the truth" do
+    assert true
+  end
+end