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:
parent
535bc8f1d8
commit
ed68d524de
5 changed files with 61 additions and 10 deletions
36
db/migrate/031_create_countries.rb
Normal file
36
db/migrate/031_create_countries.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue