Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
with open('connection_lines.geojson') as connection_lines_file:
connection_lines = json.loads(connection_lines_file.read())
route_features = []
for line_feature in connection_lines['features']:
origin = {
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': line_feature['geometry']['coordinates'][0]}}
destination = {
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': line_feature['geometry']['coordinates'][1]}}
directions_service = Directions(
access_token=os.environ['MAPBOX_ACCESS_TOKEN'])
response = directions_service.directions(
[origin, destination], 'mapbox.driving', alternatives=False)
route_feature = response.geojson()['features'][0]
route_feature['properties'] = line_feature['properties']
route_features.append(route_feature)
routes_gdf = gpd.GeoDataFrame.from_features(route_features)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment