Instantly share code, notes, and snippets.

What would you like to do?
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
# 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)
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="">
<name>iPhone tracking</name>
<description>iPhone tracking data, directly extracted from a fresh "consolidated.db" database</description>
<Style id="transPurpleLineGreenPoly">
# 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 = + 978307200)
iso_ts = dt.isoformat()
human_ts = dt.strftime("%x")
if cur_ts != "" and human_ts != cur_ts:
print(""" </coordinates>
if human_ts != cur_ts:
cur_ts = human_ts
<name>Location on {0}</name>
<coordinates>""".format(human_ts, iso_ts))
print(" {0},{1}".format(lon, lat))
# KML footer
print(""" </coordinates>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment