Created
April 19, 2011 05:37
-
-
Save bobzoller/926866 to your computer and use it in GitHub Desktop.
A ruby script that inserts proper TimeStamp nodes into a Google Latitude KML export.
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| require 'rubygems' | |
| require 'nokogiri' | |
| require 'chronic' | |
| if (!ARGV[0]) | |
| STDERR.puts "usage: #{__FILE__} <input.kml>" | |
| exit 1 | |
| end | |
| doc = Nokogiri::XML(open(ARGV[0])) | |
| STDERR.puts "processing KML file..." | |
| doc.xpath('//xmlns:Placemark').each do |placemark_node| | |
| STDERR.print "Placemark found: " | |
| name_node = placemark_node.at_xpath('xmlns:name') | |
| timestamp_node = placemark_node.at_xpath('xmlns:TimeStamp') | |
| if name_node && !timestamp_node | |
| timestamp_node = Nokogiri::XML::Node.new "TimeStamp", doc | |
| when_node = Nokogiri::XML::Node.new "when", doc | |
| begin | |
| when_node.content = Chronic.parse(name_node.content).utc.iso8601 | |
| rescue | |
| STDERR.puts "couldn't parse \"#{name_node.content}\"." | |
| exit 1 | |
| end | |
| timestamp_node.add_child(when_node) | |
| placemark_node.add_child(timestamp_node) | |
| STDERR.puts "added #{timestamp_node.to_s} node." | |
| elsif timestamp_node | |
| STDERR.puts "existing #{timestamp_node.to_s} node." | |
| else | |
| STDERR.puts "couldn't find a <Data name=\"timestamp\"> node." | |
| end | |
| end | |
| STDOUT.puts doc.to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment