Skip to content

Instantly share code, notes, and snippets.

@nbumbarger
Created June 1, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbumbarger/0a9615cca929e0a31029 to your computer and use it in GitHub Desktop.
Save nbumbarger/0a9615cca929e0a31029 to your computer and use it in GitHub Desktop.
Geocode a list of addresses
# From: http://www.nickbumbarger.com/2015/05/31/spatial_data_production.html
require "HTTParty"
def geocode(address)
results = HTTParty.get("https://maps.googleapis.com/maps/api/geocode/json?address="+address)["results"]
location = results.first["geometry"]["location"]
end
#Start with the original address data
data = "GA Washington DC\t1133 15th Street NW, 8th Floor Washington, DC 20005\nGA New York\t902 Broadway, 4th Floor New York, NY 10010\nGA Chicago\t444 N Wabash Ave, 5th Floor Chicago, IL 60611\n"
#Split into names and addresses, and send the address to the geocoder
#Reform the original text with coordinates in the place of addresses
new_data = String.new
data.split("\n").each do |line|
name, address = line.split("\t")
coords = geocode(address)
new_data += "#{name}\t#{coords['lng']},#{coords['lat']}\n"
end
puts new_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment