Use a local lookup table for country bounding boxes rather than relying

on geonames.org being able to do it in a reasonable time.
This commit is contained in:
Tom Hughes 2009-05-11 16:50:09 +00:00
parent 535bc8f1d8
commit ed68d524de
5 changed files with 61 additions and 10 deletions

2
app/models/country.rb Normal file
View file

@ -0,0 +1,2 @@
class Country < ActiveRecord::Base
end

View file

@ -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

View 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

13
test/fixtures/countries.yml vendored Normal file
View file

@ -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:

View file

@ -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