Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Locations.kml (pulled from an iPhone backup) to SQLite
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<TimeStamp>
<when>2015-12-18T19:12:32</when>
</TimeStamp>
<name>2015-12-18 19:12:32 Source: WhatsApp</name>
<Point>
<coordinates>-0.120970480144024,51.510383605957</coordinates>
</Point>
</Placemark>
<Placemark>
<TimeStamp>
<when>2018-04-20T22:33:36</when>
</TimeStamp>
<name>2018-04-20 22:33:36 Source: WhatsApp</name>
<Point>
<coordinates>-122.425651550293,37.7773132324219</coordinates>
</Point>
</Placemark>
<Placemark>
<TimeStamp>
<when>2015-02-02T20:32:46</when>
</TimeStamp>
<name>2015-02-02 20:32:46 Source: Photos</name>
<Point>
<coordinates>-110.0553,35.2266</coordinates>
</Point>
</Placemark>
</Document>
</kml>
from xml.etree import ElementTree as ET
import sys
import sqlite_utils
KML = "{http://www.opengis.net/kml/2.2}"
def iterate_kml(filepath):
fp = open(filepath)
parser = ET.XMLPullParser(["end"])
while True:
chunk = fp.read(1024 * 8)
parser.feed(chunk)
for event, element in parser.read_events():
assert event == "end"
if element.tag == f"{KML}Placemark":
datetime = element.find(f".//{KML}when").text
source = element.find(f".//{KML}name").text.split("Source: ")[-1]
longitude, latitude = map(
float, element.find(f".//{KML}coordinates").text.split(",")
)
yield {
"datetime": datetime,
"source": source,
"latitude": latitude,
"longitude": longitude,
}
if not chunk:
break
if __name__ == "__main__":
db = sqlite_utils.Database("phone-locations.db")
db["locations"].insert_all(iterate_kml(sys.argv[-1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.