Friday, August 29, 2008
ATOM to KML converter
A friend of mine is currently on a research
cruise and has been blogging
along the way. Each time he blogs, he posts a position update in the form
of a link to Google Maps.
It occurred to me that it'd be nice to have a real-time route map of where
he'd been, and that a Google maps
mashup would be a good way to do this. The problem was getting the data
to create the mashup. His blog includes an Atom Syndication
Format feed for syndication, so I figured that what I needed was a way
to automatically convert this data into something Google maps could
I've been playing a fair amount with Google's Keyhole Markup
Language (KML) for work reasons — we use it to record GIS data
about our network infrastructure for use in Google Earth. KML works nicely in Maps
too, so it seemed that what I really needed was a way to take ATOM data from
a blog and convert it to KML.
A little bit of PHP later and I had what I needed. A script that read the
ATOM, extracted position information from it, and converted that into a KML
script I could use in my mashup.
Figuring that this would probably be useful to someone else, I've tried to
make my ATOM to KML converter as generic as possible. I knows nothing of
the particular blog, and extracts everything it needs to know from the blog
The result is available as atomtokml.php.
You'll need Apache and PHP (with the XML, PREG and cURL extensions), as
well as the SimplePie ATOM PHP class. The
latter is simplepie.inc and need to go somewhere in PHP's search
Configuration is easy or non-existent. There are a couple of variables at
the top of the atomtokml.php script that you may need to set.
To use it, simply call it from a web browser. By default it'll probably
open in in Google Earth. You can also use the GGeoXml
function to load it in a Maps mashup.
The script understands a few parameters to make life easier.
nocache=1 will disable SimplePie's ATOM cache and fetch the feed
directly from source again — useful for forcing a reload.
text=1 will display the resulting KML as text/plain rather
than application/vnd.google-earth.kml+xml, which will probably make
it display in your browser — useful for finding out why a post didn't
make it into the KML. Finally, feed=URL will override the
default feed URL and fetch a different ATOM feed — this'll let you use
the same script for multiple blogs.
From a blogger's perspective, there are two ways to get a post into the KML.
The first is to make sure your ATOM feed has the longitude & latitude
fields set for each post. Realising that most blogging software doesn't
support this, the second is to post links to Google Maps in entries you want to
appear. The script can parse Google Maps' permalink format and extract
longitude & latitude from the URL.
As usual, YMMMV.
UPDATE 2008/08/31: bug fixes
posted by guy at: 07:55 SAST |
path: /systems |
I've updated atomtokml.php to fix a
few bugs in the KML output (it seems I didn't read the spec
properly). The output now validates correctly in a couple of different KML validators. Unfortunately it
seems that, having done this, Google Earth doesn't parse a KML 2.2. file
properly :) It'll load fine, but if you turn verbose warnings on, you'll get
warned about some of the imported elements.