Skip to content

Commit

Permalink
add task to import geo places again with expanded data for MRU countr…
Browse files Browse the repository at this point in the history
…ies and with populations.
  • Loading branch information
kclair committed Mar 21, 2011
1 parent 84c52b8 commit b33a237
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
9 changes: 9 additions & 0 deletions db/migrate/20110214113456_add_population_to_geo_places.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddPopulationToGeoPlaces < ActiveRecord::Migration
def self.up
add_column :geo_places, :population, :bigint
end

def self.down
remove_column :geo_places, :population
end
end
62 changes: 62 additions & 0 deletions lib/tasks/import_geo_data_mru.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace :cg do

desc "Imports GeoNames files containing geographic data into models."
task :import_geo_data_mru => :environment do
# require 'net/http'
all_data_countries = ['LR', 'SL', 'GN', 'CI']
# see http://www.geonames.org/export/codes.html
all_data_feature_classes = ['A','P']
path = "#{RAILS_ROOT}/tmp/geo_data"
uri = "/export/dump"
countries = "/countryInfo.txt"
admin_codes = "/admin1Codes.txt"
places = "/allCountries"
Dir.mkdir(path) if ! File.directory?(path)
# [countries, admin_codes, places+".zip"].each do |geofile|
# Net::HTTP.start("download.geonames.org") { |http|
# resp = http.get(uri+geofile)
# open(path+geofile, "w") { |file|
# file.write(resp.body)
# }
# }
# end
# system("unzip "+path+places+".zip -d #{path}")
open(path+places+".txt").each do |line|
row = line.split("\t")
# include places with populations less than 1000 if they are one of the MRU countries
if all_data_countries.include?(row[8])
next if !all_data_feature_classes.include?(row[6])
else
next if row[14].to_i < 1000
end
geocountry = GeoCountry.find_by_code(row[8])
next if geocountry.nil?
row[10] = '00' if row[10] !~ /\S/
STDERR.puts "on #{row[0]} :: #{row[10].to_s} :: #{row[8]} :: #{row[14]}"
geoadmincode = geocountry.geo_admin_codes.find_by_admin1_code(row[10])
# if there is no admin code matching the record, use the general admin code
geoadmincode ||= geocountry.geo_admin_codes.find_by_admin1_code('00')
row[3].sub!(/^(.*\S)\s*$/,'\1')
options = {
:geo_country_id => geoadmincode.geo_country_id,
:geo_admin_code_id => geoadmincode.id,
:geonameid => row[0],
:name => row[1],
:alternatenames => row[3],
:latitude => row[4],
:longitude => row[5],
:population => row[14].to_i
}
geoplace = geoadmincode.geo_places.find_by_geonameid(row[0])
if geoplace.nil?
STDERR.puts "Adding new place #{row[1]}"
GeoPlace.create!(options)
else
STDERR.puts "Updating place #{row[1]}"
geoplace.update_attributes(options)
end
end
end

end

0 comments on commit b33a237

Please sign in to comment.