Created
June 1, 2015 12:35
-
-
Save nbumbarger/53ef6f4d7576a2f4efb1 to your computer and use it in GitHub Desktop.
Convert a list of coordinates to KML
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 | |
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 raw text | |
data = "GA Washington DC\t38.90485\t-77.03394\nGA New York\t40.73930\t-73.98942\nGA Chicago\t41.89061\t-87.62688" | |
#Define an empty body and split the text into attributes | |
#Insert attributes into the placemark template and append result to the body | |
body = String.new | |
raw_data.split("\n").each do |line| | |
name, lat, lng = line.split("\t") | |
body += placemark_template(name, lat, lng) | |
end | |
#Add the standard KML header and footer to the result | |
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 | |
#The resulting text represents a complete, valid KML document | |
puts kml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment