03 February 2014

Converting kml to gpx with python

Today I wanted to geo-code some of my photos.

I have an SLR digital camera (no gps of course), and an android phone. I recorded a track with My Tracks from google on the phone. (Not entirely recommended but works). I then fired up digikam to run the geo-correlation and add lat-long to the exif of the files only to discover digikam doesn't know how to read kml. Fooey.


I looked to gpsbabel, but it apparently can't handle this style of kml file, as differentiated by the coordinates being in the following style of markup:

<gx:coord>-1.885348 50.769434</gx:coord>
<gx:coord>-1.885193 50.769328 53.20000076293945</gx:coord>

So I wrote a python script to munge it into gpx shape:


This can be run as follows:

./kmlToGpx.py "25-01 12-48.kml" > "25-01 12-48.kml.gpx"

And worked a treat for me.

After I'd done this I discovered my pet tool gpsprune can open the new style kml. (I forked gpsprune a while ago and added a minor feature) However I'm glad to have a command-line tool as I have hundreds of tracks I want to convert.

Incidentally the phone can automatically sync the tracks to google drive, which is kinda handy and then you can download them from the site etc.


冯子力 said...

Thank you for your post. It helps.

Anonymous said...

Hi and Happy New Year! My recommendation is to have a look at this free tool http://gpx2kml.com/ that easily convert gpx to kml or vice versa, when you need it. Maybe it's worth giving a shot!

Anonymous said...

Thanks for the script.