Routing Modules
- urbanpy.routing.compute_osrm_dist_matrix(origins, destinations)[source]
Compute distance and travel time origin-destination matrices kahsgfhjasdgfkasjdhjhsdg
- Parameters:
origins (GeoDataFrame or GeoSeries) – Input Point geometries corresponding to the starting points of a route.
destinations (GeoDataFrame or GeoSeries) – Point geometries corresponding to the end points of a route.
- Returns:
dist_matrix (array_like) – Array with origins as rows and destinations as columns. Distance in meters.
dur_matrix (array_like) – Array with origins as rows and destinations as columns. Duration in minutes.
Examples
>>> hex = urbanpy.geom.gen_hexagons(8, lima) >>> fs = urbanpy.download.overpass_pois(hex.total_bounds, 'food_supply') >>> urbanpy.routing.start_osrm_server('peru', 'south-america') >>> dist, dur = urbanpy.routing.compute_osrm_dist_matrix(points.head(), fs.head(), 'walking') array([[ 82012.4, 93899.9, 88964.6, 111839.8, 87844. ], [ 26346.3, 23725.6, 19618.9, 38607.9, 22725.6], [ 26504.4, 23883.7, 19777. , 38766. , 22883.7], [ 33314.3, 30693.6, 26586.9, 45575.9, 29693.5], [ 26659.3, 24038.7, 19931.9, 38920.9, 23038.6]]) array([[59093.7, 67666.8, 64123.1, 80592.8, 63331.5], [19027.5, 17094.6, 14152.3, 27797.6, 16402.8], [19141.5, 17208.6, 14266.3, 27911.6, 16516.8], [24044.9, 22112. , 19169.7, 32815. , 21420.2], [19253.1, 17320.2, 14377.9, 28023.2, 16628.4]])
- urbanpy.routing.google_maps_dir_matrix(origin, destination, mode, api_key, **kwargs)[source]
Google Maps distance matrix support.
- Parameters:
origin (tuple, list or str) – Origin for distance calculation. If tuple or list, a matrix for all lat-lon pairs will be computed (origin as rows). If str, google_maps will georeference and then compute.
destination (tuple, list or str) – Origin for distance calculation. If tuple or list, a matrix for all lat-lon pairs will be computed (origin as rows). If str, google_maps will georeference and then compute.
mode (str. One of {"driving", "walking", "transit", "bicycling"}) – Mode for travel time calculation
api_key (str) – Google Maps API key
**kwargs – Additional keyword arguments for the distance matrix API. See https://github.com/googlemaps/google-maps-services-python/blob/master/googlemaps/directions.py
- Returns:
dist (int) – Distance for the o/d pair. Depends on metric parameter
dur (int) – Travel time duration, depends on units kwargs
Examples
>>> API_KEY = 'example-key' >>> urbanpy.routing.google_maps_dir_matrix('San Juan de Lurigancho', 'Miraflores', 'walking', API_KEY) (18477, 13494)
- urbanpy.routing.google_maps_dist_matrix(origin, destination, mode: str, api_key: str, **kwargs) Tuple[int, int] [source]
Google Maps distance matrix support.
- Parameters:
origin (tuple, list or str) – Origin for distance calculation. If tuple or list, a matrix for all lat-lon pairs will be computed (origin as rows). If str, google_maps will georeference and then compute.
destination (tuple, list or str) – Origin for distance calculation. If tuple or list, a matrix for all lat-lon pairs will be computed (origin as rows). If str, google_maps will georeference and then compute.
mode (str. One of {"driving", "walking", "transit", "bicycling"}) – Mode for travel time calculation
api_key (str) – Google Maps API key
**kwargs – Additional keyword arguments for the distance matrix API. See https://github.com/googlemaps/google-maps-services-python/blob/master/googlemaps/distance_matrix.py
- Returns:
dist (int) – Distance for the o/d pair. Depends on metric parameter
time (int) – Travel time duration
Examples
>>> API_KEY = 'example-key' >>> urbanpy.routing.google_maps_dist_matrix('San Juan de Lurigancho', 'Miraflores', 'walking', API_KEY) (18477, 13494) >>> urbanpy.routing.google_maps_dist_matrix((-12,-77), (-12.01,-77.01), 'walking', API_KEY) (2428, 1838) >>> urbanpy.routing.google_maps_dist_matrix([(-12,-77),(-12.11,-77.01)], [(-12.11,-77.01),(-12,-77)], 'walking', API_KEY) ([[13743, 0], [0, 13720]], [[10232, 0], [0, 10674]])
- urbanpy.routing.isochrone_from_api(locations, time_range, profile, api, api_key)[source]
Get isochrones from either the OpenRouteService or MapBox API
- Parameters:
locations (array_like (float, float)) – Set of locations from which to calculate the isochrones in lon, lat pairs. Larger sets may exceed API limits.
time_range (array_like (int)) – Time intervals to compute i.e. the travel time ranges for the isochrones. Depends on the API
profile (str. Depends on the API.) – Mobility profile to compute the isochrones
api (str. One of {"ors", "mapbox"}) – API to request the isochrones from.
api_key (str) – API auth key
- Returns:
isochrones – GeoPandas GeoDataFrame containing the isochrone polygons with columns: contour (time range), group index (isochrone-location matcher) and geometry
- Return type:
GeoDataFrame
Examples
>>> import urbanpy as up >>> API_KEY = "some_key" >>> up.routing.isochrone_from_api([[8.681495,49.41461],[8.686507,49.41943]], [300,900], 'foot-walking', 'ors', API_KEY) contour | group_index | geometry 300.0 | 0 | POLYGON ((8.67640 49.41485, 8.67643 49.41461, ... 900.0 | 0 | POLYGON ((8.66750 49.41169, 8.66755 49.41164, ... 300.0 | 1 | POLYGON ((8.68103 49.41902, 8.68167 49.41815, ... 900.0 | 1 | POLYGON ((8.67108 49.41982, 8.67109 49.41946, ... >>> up.routing.isochrone_from_api([[8.681495,49.41461],[8.686507,49.41943]], [5,10], 'driving', 'mapbox', API_KEY) contour | group_index | geometry 10 | 0 | POLYGON ((8.68149 49.43661, 8.68100 49.43461, ... 5 | 0 | POLYGON ((8.67850 49.42361, 8.67621 49.42190, ... 10 | 1 | POLYGON ((8.67258 49.44751, 8.67138 49.44743, ... 5 | 1 | POLYGON ((8.68265 49.42957, 8.68151 49.42846, ...
- urbanpy.routing.isochrone_from_graph(graph, locations, time_range, profile)[source]
Create isochrones from a network graph.
- Parameters:
graph (NetworkX or OSMnx graph.) – Input graph to compute isochrones from.
locations (array_like) – Locations (lon, lat) from which to trace the isochrones.
time_range (int) – Travel time from which to construct the isochrones
profile (str or int) – If str, a default avg. speed will be used to compute the travel time. If int, this value will be used as the avg. speed to compute the isochrones.
- Returns:
isochrones – GeoDataFrame containing the isochrones for the set of locations and time_ranges.
- Return type:
GeoDataFrame
Examples
>>> import urbanpy as up >>> import osmnx as ox >>> G = ox.graph_from_place('Berkeley, CA, USA', network_type='walking') >>> up.routing.isochrone_from_graph(G, [[],[]], [5, 10], 'walking')
- urbanpy.routing.nx_route(graph, source, target, weight, length=True)[source]
Compute shortest path from a source and target node.
- Parameters:
graph (NetworkX Graph) – Input graph from which to calculate paths
source (str or int) – ID of the source node from which to calculate path. Depending on the graph, this may be a string or integer or a combination of both as tuples.
target (str or int) – ID of the target node. Type corresponds to node id types in the input graph.
weight (str) – Attribute to be used as weights in the path. If None returns the sequence of nodes or the number of nodes to travel as length.
length (bool) – Flag for whether to calculate the path lenght or the sequence of nodes to follow. If weight is none and length is true, the number of nodes in the path will be returned.
- Returns:
path (list) – Sequence of node ids in a list.
path_length (float or int) – Length of the path according to the weight attribute.
Examples
>>> import osmnx as ox >>> import urbanpy as up >>> from random import choices >>> G = ox.graph_from_place('Lima, Peru') >>> source, target = choices(list(G.nodes), k=2) >>> up.routing.nx_route(G, source, target, 'length') 8348.236999999996
- urbanpy.routing.ors_api(locations, origin, destination, profile, metrics, api_key)[source]
Interface with OpenRoute Service API for distance matrix computation.
- Parameters:
origin (list) – Input origin(s) indices in the location array to compute travel times and distances
destination (list) – Input destination(s) indices in the location array to compute travel times and distances
profile (str. One of {'driving-car', 'foot-walking', 'cycling-regular'})
metrics (list) – Combination of metrics to compute (distance and travel time, or one of them)
api_key (str) – Authorization API key for OpenRouteService (see API limits)
- Returns:
dist (list) – Distance matrix
dur (list) – Travel time matrix
Examples
>>> locations = [[9.70093,48.477473],[9.207916,49.153868],[37.573242,55.801281],[115.663757,38.106467]] >>> metrics = ["distance","duration"] >>> sources = [0] >>> destinations = [1,2,3] >>> api_key = ... >>> urbanpy.routing.ors_api(locations, sources, destinations, metrics, 'driving-car', api_key) [[5753.86,88998.08,399003.44]] [[140861.31,2434228.75,1.0262603E7]]
- urbanpy.routing.osrm_route(origin: DataFrame | GeoDataFrame, destination: DataFrame | GeoDataFrame) Tuple[float, float] [source]
Query an OSRM routing server for routes between an origin and a destination using a specified profile.
Travel mode (“foot”, “bicycle”, or “car”) is determined by the profile selected when starting the OSRM server
- Parameters:
origin (DataFrame with columns x and y or Point geometry) – Input origin in lat lon pairs (y, x) to pass into the routing engine
destination (DataFrame with columns x and y or Point geometry) – Input destination in lat lon pairs (y,x) to pass to the routing engine
- Returns:
distance (float) – Total travel distance from origin to destination in meters
duration (float) – Total travel time in minutes
- urbanpy.routing.start_osrm_server(country: str, continent: str, profile: str) None [source]
Download data for OSRM, process it and start a local osrm server
- Parameters:
country (str) – Which country to download data from. Expected in lower case & dashes replace spaces.
continent (str) – Which continent of the given country. Expected in lower case & dashes replace spaces.
profile (str. One of {'foot', 'car', 'bicycle'}) – Travel mode to use when routing and estimating travel time.
Examples
>>> urbanpy.routing.start_osrm_server('peru', 'south-america', 'foot') Starting server ... Server was started succesfully.
- urbanpy.routing.stop_osrm_server(country: str, continent: str, profile: str) None [source]
Run docker stop on the server’s container.
- Parameters:
country (str) – Which country the osrm to stop is routing. Expected in lower case & dashes replace spaces.
continent (str) – Continent of the given country. Expected in lower case & dashes replace spaces.
profile (str. One of {'foot', 'car', 'bicycle'}) – Travel mode to use when routing and estimating travel time.
Examples
>>> urbanpy.routing.stop_osrm_server('peru', 'south-america', 'foot') Server stopped succesfully