#!/bin/python3 ''' xlm2kml.py A crude converter from Favourites.xlm (MagicEarth GPS favourites) into .kml (which can be imported in various applications, such as OrganicMaps). Steps: 1) In MagicEarth, go to Favourites, share > Save as file. 2) Copy "Favourites.xlm" in the same directory as this script. 3) Run this script. "Favourites.kml" will be produced. 4) Copy "Favourites.kml" to your phone. 5) In OrganicMaps, go to Favourites > Import and choose the folder where "Favourites.kml" is. This code is public domain. Author: sebsauvage (at) sebauvage (dot) net (https://sebsauvage.net/) ''' import sqlite3,codecs,xml.sax.saxutils def readFavourites(): ''' Reads Favourites.xlm in current directory (as exported by MagicEarth) and returns an array of tuples (favourite name, longitude, latitude) ''' con = sqlite3.connect("Favourites.xlm") favourites = [] for row in con.execute("select name,coord from LMK"): name,coordHexa = row[0], "%016X" % row[1] # MagicEarth coordinates are a 64 bits integer. # We split this integer in two 32 bits integers: latitude = int.from_bytes(bytes.fromhex(coordHexa[0:8 ]),'big',signed=True) / 3200000 longitude = int.from_bytes(bytes.fromhex(coordHexa[8:16]),'big',signed=True) / 3200000 ''' 64 bits MagicEarth coordinates to latitude/longitude conversion from : https://www.android-hilfe.de/forum/magic-earth.3177/favoriten-editieren-mit-einem-externen-editor.972945.html#post-12508288) ''' favourites.append((xml.sax.saxutils.escape(name),longitude,latitude)) con.close() return favourites def writeKml(): ''' Reads Favourites.xlm in current directory (as exported by MagicEarth) and write Favourites.kml. ''' file = codecs.open("Favourites.kml", "w", "utf-8") file.write(u'Favourites\n') for fav in readFavourites(): print(fav) file.write(u"{0[0]}{0[1]},{0[2]}\n".format(fav)) file.write(u'\n') file.close() writeKml()