Skip to content

Instantly share code, notes, and snippets.

@bobzoller
Created April 19, 2011 05:37
Show Gist options
  • Select an option

  • Save bobzoller/926866 to your computer and use it in GitHub Desktop.

Select an option

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.
#!/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