Created
June 10, 2011 22:29
-
-
Save kirkconnell/1019915 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 :locations do | |
desc 'Update the population field of all city locations with the help of a csv file' | |
task :populate_cities, :pop_file, :no_match_file, :needs => :environment do | t , args | | |
raise "Please send a valid CSV File, example: rake locations:populate_cities[pathCVSFile,noMatchFilePath*]" if args[:pop_file].nil? | |
csv_file = args[:pop_file] | |
unmatch_file = args[:no_match_file] || "no_match_location.csv" | |
require 'fastercsv' | |
#constants | |
NAME = 3 | |
STATE_NAME = 4 | |
POP = 5 | |
#statistical | |
good = 0 | |
#Before begin reset all population | |
Location.update_all("population = NULL","location_type_id = 5 AND active = true") | |
#Save locations that didn't match | |
FasterCSV.open(unmatch_file, "w") do |csv| | |
csv << ["location_name", "state_name","population", "url_for_attempt"] | |
FasterCSV.foreach(csv_file) do |city_row| | |
csv_city_name = city_row[NAME].gsub(/\(.*\)/, "").squeeze(" ").strip | |
location_type = "city" | |
if (csv_city_name =~ /town$/).present? | |
location_type = "town" | |
elsif (csv_city_name =~ /village/).present? | |
location_type = "village" | |
elsif (csv_city_name =~ /borough/).present? | |
location_type = "borough" | |
elsif (csv_city_name =~ /township/).present? | |
location_type = "township" | |
elsif (csv_city_name =~ /CDP/).present? | |
location_type = "CDP" | |
elsif (csv_city_name =~ /municipality/).present? | |
location_type = "municipality" | |
elsif (csv_city_name =~ /comunidad/).present? | |
location_type = "comunidad" | |
elsif (csv_city_name =~ /zona urbana/).present? | |
location_type = "zona urbana" | |
end | |
csv_city_name = csv_city_name.gsub(/#{location_type}/, "").squeeze(" ") | |
location_url = File.join("", city_row[STATE_NAME].parameterize, csv_city_name.parameterize) | |
print "Find for: " + location_url + "\n" | |
location = Location.find_active_cities(:first, :conditions => ["locations.url_for = ?", location_url]) | |
if location.present? && ( location.population.nil? || location.population.to_i < city_row[POP].to_i ) | |
location.update_attributes(:population => city_row[POP] ) | |
good += 1 | |
else | |
csv << [city_row[NAME], city_row[STATE_NAME],city_row[POP], location_url] | |
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