Last active
August 29, 2015 14:22
-
-
Save nbumbarger/a152517c2c7a9680f026 to your computer and use it in GitHub Desktop.
Convert addresses to KML (geocoding API)
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 | |
def placemark_template(name, lat, lng) | |
placemark = "<Placemark>\n<name>#{name}</name>\n<Point>\n<coordinates>\n#{lng},#{lat},0\n</coordinates>\n</Point>\n</Placemark>\n" | |
end | |
#Start with the address data | |
data = "GA Washington DC\t1133 15th Street NW, 8th Floor Washington, DC 2005\nGA New York\t902 Broadway, 4th Floor New York, NY 10010\nGA Chicago\t444 N Wabash Ave, 5th Floor Chicago, IL 6011\n" | |
kml = String.new | |
data.split("\n").each do |line| | |
name, address = line.split("\t") | |
coords = geocode(address) | |
kml += placemark_template(name, coords["lat"], coords["lng"]) | |
end | |
head = "<?xml version='1.0' encoding='UTF-8'?>\n<kml xmlns='http://www.opengis.net/kml/2.2'>\n<Document>\n" | |
tail = "</Document>\n</kml>" | |
kml = head + body + tail | |
#End with KML that can be viewed in Google Earth | |
puts kml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment