Skip to content

Instantly share code, notes, and snippets.

@Schnouki
Created April 21, 2011 12:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Schnouki/934357 to your computer and use it in GitHub Desktop.
Save Schnouki/934357 to your computer and use it in GitHub Desktop.
iPhone tracking database to KML format converter
#!/usr/bin/env python3
#
# Copyright (C) 2011 Thomas Jost
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
import datetime
import sqlite3
import sys
if len(sys.argv) < 2:
print("Syntax: {0} path-to-consolidated.db > output.kml".format(sys.argv[0]), file=sys.stderr)
sys.exit(1)
conn = sqlite3.connect(sys.argv[1])
conn.row_factory = sqlite3.Row
curs = conn.cursor()
curs.execute("select * from celllocation")
# KML header
print("""<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<name>iPhone tracking</name>
<open>1</open>
<description>iPhone tracking data, directly extracted from a fresh "consolidated.db" database</description>
<Style id="transPurpleLineGreenPoly">
<LineStyle>
<color>7fff00ff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>""")
# KML data
cur_ts = ""
for row in curs:
lat, lon, ts = row["Latitude"], row["Longitude"], row["Timestamp"]
# Apple uses timestamps that store the number of seconds since 2001...
dt = datetime.date.fromtimestamp(int(ts) + 978307200)
iso_ts = dt.isoformat()
human_ts = dt.strftime("%x")
if cur_ts != "" and human_ts != cur_ts:
print(""" </coordinates>
</LineString>
</Placemark>""")
if human_ts != cur_ts:
cur_ts = human_ts
print("""
<Placemark>
<name>Location on {0}</name>
<TimeStamp>
<when>{1}</when>
</TimeStamp>
<styleUrl>#transPurpleLineGreenPoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<coordinates>""".format(human_ts, iso_ts))
print(" {0},{1}".format(lon, lat))
# KML footer
print(""" </coordinates>
</LineString>
</Placemark>
</Document>
</kml>""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment