Search code examples
pythongoogle-mapsgpsgeojsongpx

how to convert google-maps GeoJSON to GPX, retaining location names


I have exported my google-maps Point Of Interests (saved places / locations) via the takeout tool. How can i convert this to GPX, so that i can import it into OSMAnd?

I tried using gpsbabel:

gpsbabel -i geojson -f my-saved-locations.json -o gpx -F my-saved-locations_converted.gpx

But this did not retain the title/name of each point of interest - and instead just used names like WPT001, WPT002, etc.


Solution

  • in the end I solved this by creating a small python script to convert between the formats. This could be easily adapted for specific needs:

    #!/usr/bin/env python3
    
    import argparse
    import json
    import xml.etree.ElementTree as ET
    from xml.dom import minidom
    
    
    def ingestJson(geoJsonFilepath):
        poiList = []
        with open(geoJsonFilepath) as fileObj:
            data = json.load(fileObj)
            for f in data["features"]:
                poiList.append({'title': f["properties"]["Title"],
                                'lon': f["geometry"]["coordinates"][0],
                                'lat': f["geometry"]["coordinates"][1],
                                'link': f["properties"].get("Google Maps URL", ''),
                                'address': f["properties"]["Location"].get("Address", '')})
        return poiList
    
    
    def dumpGpx(gpxFilePath, poiList):
        gpx = ET.Element("gpx", version="1.1", creator="", xmlns="http://www.topografix.com/GPX/1/1")
        for poi in poiList:
            wpt = ET.SubElement(gpx, "wpt", lat=str(poi["lat"]), lon=str(poi["lon"]))
            ET.SubElement(wpt, "name").text = poi["title"]
            ET.SubElement(wpt, "desc").text = poi["address"]
            ET.SubElement(wpt, "link").text = poi["link"]
        xmlstr = minidom.parseString(ET.tostring(gpx)).toprettyxml(encoding="utf-8", indent="  ")
        with open(gpxFilePath, "wb") as f:
            f.write(xmlstr)
    
    
    def main():
        parser = argparse.ArgumentParser()
        parser.add_argument('--inputGeoJsonFilepath', required=True)
        parser.add_argument('--outputGpxFilepath', required=True)
        args = parser.parse_args()
    
        poiList = ingestJson(args.inputGeoJsonFilepath)
        dumpGpx(args.outputGpxFilepath, poiList=poiList)
    
    
    if __name__ == "__main__":
        main()
    

    ...

    it can be called like so:

    ./convert-googlemaps-geojson-to-gpx.py \
      --inputGeoJsonFilepath my-saved-locations.json \
      --outputGpxFilepath my-saved-locations_converted.gpx