Skip to content

Instantly share code, notes, and snippets.

@nbumbarger
Created June 1, 2015 12:35
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/53ef6f4d7576a2f4efb1 to your computer and use it in GitHub Desktop.
Save nbumbarger/53ef6f4d7576a2f4efb1 to your computer and use it in GitHub Desktop.
Convert a list of coordinates to KML
# 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