如何将谷歌地图 GeoJSON 转换为 GPX,保留位置名称

我已通过外卖工具导出了我的谷歌地图兴趣点(保存的地点/位置)。我如何将其转换为 GPX,以便将其导入 OSMAnd?

我尝试使用 gpsbabel:

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

但这并没有保留每个兴趣点的标题/名称 - 而是仅使用 WPT001、WPT002 等名称。


MM们
浏览 65回答 3
3回答

三国纷争

为了使 GPX 针对 OrganicMaps 进行优化并处理一堆 Google 边缘情况(例如缺少星标位置的坐标和描述),我修改了 @DrGecko 的代码,如下所示:#!/usr/bin/env python3import argparseimport jsonimport reimport xml.etree.ElementTree as ETfrom xml.dom import minidomdef ingestJson(geoJsonFilepath):&nbsp; &nbsp; poiList = []&nbsp; &nbsp; with open(geoJsonFilepath, encoding='utf-8') as fileObj:&nbsp; &nbsp; &nbsp; &nbsp; data = json.load(fileObj)&nbsp; &nbsp; &nbsp; &nbsp; gmQuery = re.compile("http://maps.google.com/\\?q=([-0-9.]+),([-0-9.]+)")&nbsp; &nbsp; &nbsp; &nbsp; for f in data["features"]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title = f["properties"].get("location", {}).get("name", '')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gmapsUrl = f["properties"].get("google_maps_url", '')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lon = f["geometry"]["coordinates"][0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lat = f["geometry"]["coordinates"][1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmt = f["properties"].get("Comment", '')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link = f["properties"].get("google_maps_url", '')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Fill in blanks for Organic Maps&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if cmt == '':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmt = '<a href="'+link+'">'+link+'</a>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Handle starred places without proper coordinates/comments&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if lat == 0 and lon == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if title == '':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title = "Starred Place"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matches = gmQuery.findall(gmapsUrl)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(matches) == 1 and len(matches[0]) == 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lat = matches[0][0] # Google order is backwards from GeoJSON&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lon = matches[0][1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # We have basically no information, so at least provide OM the link&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmt = '<a href="'+link+'">'+link+'</a>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; poiList.append({'title': title,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'comment': cmt,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'date': f["properties"]["date"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'lon': lon,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'lat': lat,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'link': link,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'address': f["properties"].get("location", {}).get("address", '')})&nbsp; &nbsp; return poiListdef dumpGpx(gpxFilePath, poiList):&nbsp; &nbsp; gpx = ET.Element("gpx", version="1.1", creator="", xmlns="http://www.topografix.com/GPX/1/1")&nbsp; &nbsp; for poi in poiList:&nbsp; &nbsp; &nbsp; &nbsp; wpt = ET.SubElement(gpx, "wpt", lat=str(poi["lat"]), lon=str(poi["lon"]))&nbsp; &nbsp; &nbsp; &nbsp; ET.SubElement(wpt, "name").text = poi["title"]&nbsp; &nbsp; &nbsp; &nbsp; ET.SubElement(wpt, "desc").text = poi["address"]&nbsp; &nbsp; &nbsp; &nbsp; ET.SubElement(wpt, "cmt").text = poi["comment"]&nbsp; &nbsp; &nbsp; &nbsp; ET.SubElement(wpt, "link").text = poi["link"]&nbsp; &nbsp; &nbsp; &nbsp; ET.SubElement(wpt, "time").text = poi["date"]&nbsp; &nbsp; &nbsp; &nbsp; ext = ET.SubElement(wpt, "extensions")&nbsp; &nbsp; &nbsp; &nbsp; ET.SubElement(ext, "color").text = "0000ff"&nbsp; &nbsp; xmlstr = minidom.parseString(ET.tostring(gpx)).toprettyxml(encoding="utf-8", indent="&nbsp; ")&nbsp; &nbsp; with open(gpxFilePath, "wb") as f:&nbsp; &nbsp; &nbsp; &nbsp; f.write(xmlstr)def main():&nbsp; &nbsp; parser = argparse.ArgumentParser()&nbsp; &nbsp; parser.add_argument('--inputGeoJsonFilepath', required=True)&nbsp; &nbsp; parser.add_argument('--outputGpxFilepath', required=True)&nbsp; &nbsp; args = parser.parse_args()&nbsp; &nbsp; poiList = ingestJson(args.inputGeoJsonFilepath)&nbsp; &nbsp; dumpGpx(args.outputGpxFilepath, poiList=poiList)if __name__ == "__main__":&nbsp; &nbsp; main()

浮云间

最后我通过创建一个小的 python 脚本来在格式之间进行转换来解决这个问题。这可以很容易地适应特定需求:#!/usr/bin/env python3import argparseimport jsonimport xml.etree.ElementTree as ETfrom xml.dom import minidomdef ingestJson(geoJsonFilepath):&nbsp; &nbsp; poiList = []&nbsp; &nbsp; with open(geoJsonFilepath) as fileObj:&nbsp; &nbsp; &nbsp; &nbsp; data = json.load(fileObj)&nbsp; &nbsp; &nbsp; &nbsp; for f in data["features"]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; poiList.append({'title': f["properties"]["Title"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'lon': f["geometry"]["coordinates"][0],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'lat': f["geometry"]["coordinates"][1],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'link': f["properties"].get("Google Maps URL", ''),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'address': f["properties"]["Location"].get("Address", '')})&nbsp; &nbsp; return poiListdef dumpGpx(gpxFilePath, poiList):&nbsp; &nbsp; gpx = ET.Element("gpx", version="1.1", creator="", xmlns="http://www.topografix.com/GPX/1/1")&nbsp; &nbsp; for poi in poiList:&nbsp; &nbsp; &nbsp; &nbsp; wpt = ET.SubElement(gpx, "wpt", lat=str(poi["lat"]), lon=str(poi["lon"]))&nbsp; &nbsp; &nbsp; &nbsp; ET.SubElement(wpt, "name").text = poi["title"]&nbsp; &nbsp; &nbsp; &nbsp; ET.SubElement(wpt, "desc").text = poi["address"]&nbsp; &nbsp; &nbsp; &nbsp; ET.SubElement(wpt, "link").text = poi["link"]&nbsp; &nbsp; xmlstr = minidom.parseString(ET.tostring(gpx)).toprettyxml(encoding="utf-8", indent="&nbsp; ")&nbsp; &nbsp; with open(gpxFilePath, "wb") as f:&nbsp; &nbsp; &nbsp; &nbsp; f.write(xmlstr)def main():&nbsp; &nbsp; parser = argparse.ArgumentParser()&nbsp; &nbsp; parser.add_argument('--inputGeoJsonFilepath', required=True)&nbsp; &nbsp; parser.add_argument('--outputGpxFilepath', required=True)&nbsp; &nbsp; args = parser.parse_args()&nbsp; &nbsp; poiList = ingestJson(args.inputGeoJsonFilepath)&nbsp; &nbsp; dumpGpx(args.outputGpxFilepath, poiList=poiList)if __name__ == "__main__":&nbsp; &nbsp; main()...可以这样调用:./convert-googlemaps-geojson-to-gpx.py \&nbsp; --inputGeoJsonFilepath my-saved-locations.json \&nbsp; --outputGpxFilepath my-saved-locations_converted.gpx

慕妹3242003

还有一个名为“togpx”的 NPM 脚本:&nbsp;https ://github.com/tyrasd/togpx我没有尝试过,但它声称保留尽可能多的信息。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python