Skip to content

Instantly share code, notes, and snippets.

@tcecyk
Last active October 1, 2023 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcecyk/3a5f4754cf7eff68134317e282404586 to your computer and use it in GitHub Desktop.
Save tcecyk/3a5f4754cf7eff68134317e282404586 to your computer and use it in GitHub Desktop.
converts Android "MagicEarth" map app Favourites.xlm to geojson, gpx and kml

Usage

I stopped worrying and use the break-system-packages flag

pip install -r requirements.txt  --break-system-packages

Put the Favourites.xlm in the same directory as the me2favs.py script, run it and you should see 3 favs.{geojson,gpx,kml} files

python3 me2favs.py
#!/usr/bin/env python3
#
# converts Android "MagicEarth" map app Favourites.xlm to geojson, gpx and kml
import sqlite3
import geojson
from geojson import FeatureCollection, Feature, Point
import gpx
from gpx import Waypoint, GPX
import simplekml
con = sqlite3.connect("Favourites.xlm")
cur = con.cursor()
res = cur.execute("""select name,
(cast((coord << 32 >> 32) as real) / 3200000) as long,
(cast((coord >> 32) as real) / 3200000) as lat
from LMK""")
favs = res.fetchall()
gpx = GPX.from_string("""<?xml version="1.0" encoding="UTF-8" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1"></gpx>""")
kml = simplekml.Kml()
doc = kml.newdocument(name="favourites")
features = []
for item in favs:
name = item[0]
long = item[1]
lat = item[2]
# geojson
point = Point((long, lat))
feat = Feature(geometry=point, properties={"name": name})
features.append(feat)
# gpx
wpt = Waypoint()
wpt.longitude = long
wpt.latitude = lat
wpt.name = name
gpx.waypoints.append(wpt)
# kml
pnt = doc.newpoint(name=name, coords=[(long, lat)])
print("write favs.geojson")
featcoll = FeatureCollection(features)
with open('favs.geojson', 'w', encoding='utf-8') as f:
f.write(geojson.dumps(featcoll))
print("write favs.gpx")
gpx.to_file("favs.gpx")
print("write favs.kml")
kml.save("favs.kml")
geojson
gpx
simplekml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment