Last active
July 21, 2019 06:19
-
-
Save simonw/2f09eb4b9fa41a3721f3e87f18813221 to your computer and use it in GitHub Desktop.
Convert Locations.kml (pulled from an iPhone backup) to SQLite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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