Created
June 8, 2011 17:13
-
-
Save kirkconnell/1014849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :pinow do | |
desc 'Update the population field of all city locations with the help of a csv file' | |
task :populate_cities => :environment do | |
require 'fastercsv' | |
#constants | |
CODE = 0 | |
NAME = 5 | |
STATE_NAME = 6 | |
POP = 7 | |
#statistical | |
good = 0 | |
#look at http://www.census.gov/popest/cities/files/SUB-EST2009.pdf for desc codes | |
right_codes = [ "061" , "162" ] | |
#Save locations that didn't match | |
FasterCSV.open("no_match_location.csv", "w") do |csv| | |
csv << ["location_name", "state_name","population"] | |
FasterCSV.foreach("SUB-EST2009-ALL.csv") do |city_row| | |
if city_row[CODE].present? && right_codes.include?(city_row[CODE].to_s) | |
location_type = "city" | |
#create identifier----------- | |
if (city_row[NAME] =~ /town/).present? | |
location_type = "town" | |
elsif (city_row[NAME] =~ /village/).present? | |
location_type = "-village" | |
elsif (city_row[NAME] =~ /borough/).present? | |
location_type = "borough" | |
elsif (city_row[NAME] =~ /township/).present? | |
location_type = "township" | |
end | |
#------------------------------ | |
location_url = File.join("", city_row[STATE_NAME].parameterize, city_row[NAME].gsub(/#{location_type}$/, "").parameterize) | |
location = Location.find_active_cities(:first, :conditions => ["locations.url_for = ?", location_url]) | |
if location.present? | |
location.update_attributes(:population => city_row[POP] ) | |
good += 1 | |
else | |
csv << [city_row[NAME], city_row[STATE_NAME],city_row[POP]] | |
end | |
end | |
end | |
end | |
print "Location populated correctly: #{good}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment