From ed68d524de0703685ff15f1ab574c61ec67e2c22 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Mon, 11 May 2009 16:50:09 +0000 Subject: [PATCH] Use a local lookup table for country bounding boxes rather than relying on geonames.org being able to do it in a reasonable time. --- app/models/country.rb | 2 ++ db/migrate/031_create_countries.rb | 36 ++++++++++++++++++++++++++++++ lib/osm.rb | 12 ++-------- test/fixtures/countries.yml | 13 +++++++++++ test/unit/country_test.rb | 8 +++++++ 5 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 app/models/country.rb create mode 100644 db/migrate/031_create_countries.rb create mode 100644 test/fixtures/countries.yml create mode 100644 test/unit/country_test.rb diff --git a/app/models/country.rb b/app/models/country.rb new file mode 100644 index 000000000..b7cec2b68 --- /dev/null +++ b/app/models/country.rb @@ -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 index 000000000..e4106f486 --- /dev/null +++ b/db/migrate/031_create_countries.rb @@ -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 diff --git a/lib/osm.rb b/lib/osm.rb index 0a62d14c0..a392b88a8 100644 --- a/lib/osm.rb +++ b/lib/osm.rb @@ -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 index 000000000..f68d7023a --- /dev/null +++ b/test/fixtures/countries.yml @@ -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 index 000000000..2adc94773 --- /dev/null +++ b/test/unit/country_test.rb @@ -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 -- 2.43.2