Converting kml to gpx with python

Tim Abell · February 3, 2014

Reading time: 1 minute

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.

people on the beach

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:

./ "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.

Share: Tweet | LinkedIn
Suggest improvements: page source on github

Get extra content that's just for my list. Get new blog posts to your inbox.
Join me on my journey through software and business.