mapboxmapbox-studio

Draw driving route in dataset editor


Is there an easy way to draw (or define by waypoints) a driving route that is snapped to road centerlines in Mapbox Studio dataset editor, like can be done in Google Maps (image)? I see how to draw points, lines and polygons, but no option for driving (or walking/cycling) routes.

If not, is there a suggested workaround? I see how to import Google KML into a tileset, but the imported route is not snapped to the Mapbox roadways.

Thanks


Solution

  • One can get a navigable route using Mapbox's Directions API Playground. Save the response to a file, eg navigation_route.geojson, for use in the python code snippet below.

    Then one can extract a polyline from the response using the snippet below which saves the extracted polyline to a similarly named file (eg navigation_route_parsed.geojson, below). This file can up uploaded to Mapbox Studio as a dataset and then imported to a map within Mapbox Studio as a layer to ultimately create a map with multiple navigable routes.

    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)