Skip to content

Instantly share code, notes, and snippets.

@FerPerales
Created December 18, 2013 23:32
Show Gist options
  • Save FerPerales/8031720 to your computer and use it in GitHub Desktop.
Save FerPerales/8031720 to your computer and use it in GitHub Desktop.
Parser for zipcodes
file = File.open 'zip_codes.csv', 'r'
content = file.read
lines = content.split '>"'
output = ''
lines.each do |l|
begin
values = l.split ',"'
coordinates = values[1].
delete!("<Polygon><outerBoundaryIs><LinearRing><coordinates>").
delete!("</coordinates></LinearRing></outerBoundaryIs></Polygon>").split ' '
output += "z = ZipCode.create zip_code: '#{values[0].delete!("\n")}'\n"
coordinates.each do |c|
coord_values = c.split ','
output += "z.boundaries << Boundary.create(longitude: '#{coord_values[0]}', latitude: '#{coord_values[1]}')\n"
end
output += "\n"
rescue Exception
end
end
puts output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment