Created
June 1, 2015 12:39
-
-
Save nbumbarger/0a9615cca929e0a31029 to your computer and use it in GitHub Desktop.
Geocode a list of addresses
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
# 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