I’m trying to extract two different elements from a KML file and turn them into a CSV. I’m starting with the great site here: http://ckdake.com/content/2012/highgroove-hack-night-kml-heatmaps.html that generates a csv of coordinates. All I want to do now is add the name tag to the start of each line. I’m a ruby/nokogiri n00b so I can stick this bit of code in which gets me a) a list of all names followed by b) a list of all coordinates. But again – I’d like them on the same line.
require 'rubygems'
require 'nokogiri' # gem install nokogiri
@doc = Nokogiri::XML(File.open("WashingtonDC2013-01-04 12h09m01s.kml"))
@doc.css('name').each do |name|
puts name.content
end
@doc.css('coordinates').each do |coordinates|
coordinates.text.split(' ').each do |coordinate|
(lat,lon,elevation) = coordinate.split(',')
puts "#{lat},#{lon}\n"
end
end
How about this:
I’m assuming here that there is one coordinates pair in the
<coordinates>tags for each<Placemark>. If there are more, they’ll all get appended onto the same line.If that doesn’t work, you’ll need to post some of the KML file itself so I can test on it. I’m just guessing based on this sample KML file.