Compute a distance matrix (Python)
Compute an origins-to-destinations matrix via the Thinwrap Matrix facade in Python — one call shape for Mapbox, Google, HERE and OSRM.
1. Install
pip install thinwrap-location2. Send the call
import os
from thinwrap.location import Matrix, MatrixOptions, MapboxConfig, LatLng, TravelMode, ConnectorError
m = Matrix(MapboxConfig(access_token=os.environ["MAPBOX_ACCESS_TOKEN"]))
try:
# Providers bill per origin x destination pair — keep probe matrices small.
res = m.matrix(MatrixOptions(
origins=[LatLng(52.5200, 13.4050)],
destinations=[LatLng(52.5163, 13.3777), LatLng(52.5096, 13.3765)],
travel_mode=TravelMode.DRIVING,
))
for cell in res.cells:
print(cell.origin_index, cell.destination_index,
cell.distance_meters, cell.duration_seconds)
except ConnectorError as err:
print(f"{err.provider_code.value}: {err.provider_message}")
Same code works for all supported providers — just change the provider ID.