在数据集编辑器中绘制行车路线

Draw driving route in dataset editor

有没有一种简单的方法可以在 Mapbox Studio 数据集编辑器中绘制(或通过 waypoints 定义)与道路中心线对齐的行车路线,就像在 Google 地图 (image)?我知道如何绘制点、线和多边形,但没有驾驶(或 walking/cycling)路线的选项。

如果没有,是否有建议的解决方法?我看到了 how to import Google KML into a tileset,但是导入的路线没有捕捉到 Mapbox 道路。

谢谢

可以使用 Mapbox's Directions API Playground 获得可导航的路线。将响应保存到文件中,例如 navigation_route.geojson,以便在下面的 python 代码段中使用。

然后可以使用下面的代码片段从响应中提取多段线,该片段将提取的多段线保存到类似命名的文件中(例如下面的 navigation_route_parsed.geojson)。该文件可以作为数据集上传到 Mapbox Studio,然后作为图层导入到 Mapbox Studio 中的地图,最终创建具有多条可导航路线的地图。

import json

input_file_name_prefix = 'navigation_route'
input_file_name = input_file_name_prefix + '.geojson'
output_file_name = input_file_name_prefix + '_parsed.geojson'

data = {}
with open(input_file_name) as json_file:
    data = json.load(json_file)
   
features = data["routes"][0]

coordinates = []

for leg in features["legs"]:
    for step in leg["steps"]:
        coordinates += step["geometry"]["coordinates"]

feature_collection = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "LineString",
                "coordinates": coordinates
            }
        }
    ]
}

with open(output_file_name, 'w') as outfile:
    json.dump(feature_collection, outfile, indent=2)