Skip to content

Commit

Permalink
fixes #70; OSRM does have a weird URL concept for profiles, so revert [
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsnolde committed Jul 10, 2022
1 parent 991472f commit c18e8c9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## **Unreleased**

## [v1.0.3](https://pypi.org/project/routingpy/1.0.3/)

### Fixed
- OSRM does have a weird URL concept for profiles, so revert [#64](https://github.com/gis-ops/routing-py/issues/64))

## [v1.0.2](https://pypi.org/project/routingpy/1.0.2/)

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"

[tool.poetry]
name = "routingpy"
version = "0.3.3"
version = "1.0.3"
description = "One lib to route them all."
authors = ["Nils Nolde <[email protected]>", "Tim Ellersiek <[email protected]>"]
license = "Apache2"
Expand Down
53 changes: 37 additions & 16 deletions routingpy/routers/osrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class OSRM:
"""Performs requests to the OSRM API services."""

_DEFAULT_BASE_URL = "https://router.project-osrm.org"
_DEFAULT_BASE_URL = "https://routing.openstreetmap.de/routed-bike"

def __init__(
self,
Expand All @@ -38,13 +38,13 @@ def __init__(
retry_over_query_limit=False,
skip_api_error=None,
client=Client,
**client_kwargs
**client_kwargs,
):
"""
Initializes an OSRM client.
:param base_url: The base URL for the request. Defaults to the OSRM demo API
server. Should not have a trailing slash.
:param base_url: The base URL for the request. Defaults to the FOSSGIS OSRM
instance for "bike". Should not have a trailing slash.
:type base_url: str
:param user_agent: User Agent to be used when requesting.
Expand Down Expand Up @@ -84,13 +84,13 @@ def __init__(
retry_timeout,
retry_over_query_limit,
skip_api_error,
**client_kwargs
**client_kwargs,
)

def directions(
self,
locations,
profile,
profile="driving",
radiuses=None,
bearings=None,
alternatives=None,
Expand All @@ -100,7 +100,7 @@ def directions(
geometries=None,
overview=None,
dry_run=None,
**direction_kwargs
**direction_kwargs,
):
"""
Get directions between an origin point and a destination point.
Expand All @@ -115,6 +115,12 @@ def directions(
from in order of visit.
:type locations: list of list
:param profile: Optionally specifies the mode of transport to use when calculating
directions. Note that this strongly depends on how the OSRM server works. E.g.
the FOSSGIS instance expects 'driving' and chose to encode the 'profile' in the
base URL, e.g. https://routing.openstreetmap.de/routed-bike. Default "driving".
:type profile: str
:param radiuses: A list of maximum distances (measured in
meters) that limit the search of nearby road segments to every given waypoint.
The values must be greater than 0, an empty element signifies to use the backend default
Expand Down Expand Up @@ -175,11 +181,11 @@ def directions(
annotations,
geometries,
overview,
**direction_kwargs
**direction_kwargs,
)

return self.parse_direction_json(
self.client._request("/route/v1/driving/" + coords, get_params=params, dry_run=dry_run),
self.client._request(f"/route/v1/{profile}/{coords}", get_params=params, dry_run=dry_run),
alternatives,
geometries,
)
Expand All @@ -196,13 +202,14 @@ def get_direction_params(
annotations=None,
geometries=None,
overview=None,
**directions_kwargs
**directions_kwargs,
):
"""
Builds and returns the router's route parameters. It's a separate function so that
bindings can use routingpy's functionality. See documentation of .directions().
:param locations: NOT USED, only for consistency reasons with other providers.
:param profile: NOT USED, only for consistency reasons with other providers.
"""
params = dict()

Expand Down Expand Up @@ -283,14 +290,14 @@ def isochrones(self): # pragma: no cover
def matrix(
self,
locations,
profile,
profile="driving",
radiuses=None,
bearings=None,
sources=None,
destinations=None,
dry_run=None,
annotations=("duration", "distance"),
**matrix_kwargs
**matrix_kwargs,
):
"""
Gets travel distance and time for a matrix of origins and destinations.
Expand All @@ -303,7 +310,11 @@ def matrix(
from.
:type locations: list of list
:param profile: NOT USED. Only exists for consistency with other providers.
:param profile: Optionally specifies the mode of transport to use when calculating
directions. Note that this strongly depends on how the OSRM server works. E.g.
the FOSSGIS instance expects 'driving' and chose to encode the 'profile' in the
base URL, e.g. https://routing.openstreetmap.de/routed-bike. Default "driving".
:type profile: str
:param radiuses: A list of maximum distances (measured in
meters) that limit the search of nearby road segments to every given waypoint.
Expand Down Expand Up @@ -349,21 +360,23 @@ def matrix(
)

params = self.get_matrix_params(
locations, profile, sources, destinations, annotations, **matrix_kwargs
locations, profile, radiuses, bearings, sources, destinations, annotations, **matrix_kwargs
)

return self.parse_matrix_json(
self.client._request("/table/v1/driving/" + coords, get_params=params, dry_run=dry_run)
self.client._request(f"/table/v1/{profile}/{coords}", get_params=params, dry_run=dry_run)
)

@staticmethod
def get_matrix_params(
locations,
profile,
radiuses=None,
bearings=None,
sources=None,
destinations=None,
annotations=("duration", "distance"),
**matrix_kwargs
**matrix_kwargs,
):
"""
Builds and returns the router's route parameters. It's a separate function so that
Expand All @@ -374,6 +387,14 @@ def get_matrix_params(
"""
params = dict()

if radiuses:
params["radiuses"] = convert.delimit_list(radiuses, ";")

if bearings:
params["bearings"] = convert.delimit_list(
[convert.delimit_list(pair) for pair in bearings], ";"
)

if sources:
params["sources"] = convert.delimit_list(sources, ";")

Expand Down
4 changes: 2 additions & 2 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@
},
"osrm": {
"directions": {
"profile": "",
"profile": "driving",
"locations": PARAM_LINE_MULTI,
"radiuses": [PARAM_INT_BIG, PARAM_INT_BIG, PARAM_INT_BIG],
"bearings": [[PARAM_INT_SMALL, PARAM_INT_SMALL]] * 3,
Expand All @@ -515,7 +515,7 @@
"continue_straight": True,
},
"matrix": {
"profile": "",
"profile": "walking",
"locations": PARAM_LINE_MULTI,
"radiuses": [PARAM_INT_BIG, PARAM_INT_BIG, PARAM_INT_BIG],
"bearings": [[PARAM_INT_SMALL, PARAM_INT_SMALL]] * 3,
Expand Down
24 changes: 12 additions & 12 deletions tests/test_osrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_full_directions(self):

responses.add(
responses.GET,
"https://router.project-osrm.org/route/v1/driving/{}".format(coords),
f"https://routing.openstreetmap.de/routed-bike/route/v1/{query['profile']}/{coords}",
status=200,
json=ENDPOINTS_RESPONSES["osrm"]["directions_geojson"],
content_type="application/json",
Expand All @@ -51,7 +51,7 @@ def test_full_directions(self):
routes = self.client.directions(**query)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://router.project-osrm.org/route/v1/driving/8.688641,49.420577;8.680916,49.415776;8.780916,49.445776?"
f"https://routing.openstreetmap.de/routed-bike/route/v1/{query['profile']}/8.688641,49.420577;8.680916,49.415776;8.780916,49.445776?"
"alternatives=false&annotations=true&bearings=50%2C50%3B50%2C50%3B50%2C50&continue_straight=true&"
"geometries=geojson&overview=simplified&radiuses=500%3B500%3B500&steps=true&fallback_speed=42",
responses.calls[0].request.url,
Expand All @@ -69,7 +69,7 @@ def test_full_directions_alternatives(self):

responses.add(
responses.GET,
"https://router.project-osrm.org/route/v1/driving/{}".format(coords),
f"https://routing.openstreetmap.de/routed-bike/route/v1/{query['profile']}/{coords}",
status=200,
json=ENDPOINTS_RESPONSES["osrm"]["directions_geojson"],
content_type="application/json",
Expand All @@ -78,7 +78,7 @@ def test_full_directions_alternatives(self):
routes = self.client.directions(**query)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://router.project-osrm.org/route/v1/driving/8.688641,49.420577;8.680916,49.415776;8.780916,49.445776?"
f"https://routing.openstreetmap.de/routed-bike/route/v1/{query['profile']}/8.688641,49.420577;8.680916,49.415776;8.780916,49.445776?"
"alternatives=true&annotations=true&bearings=50%2C50%3B50%2C50%3B50%2C50&continue_straight=true&"
"geometries=geojson&overview=simplified&radiuses=500%3B500%3B500&steps=true",
responses.calls[0].request.url,
Expand All @@ -100,7 +100,7 @@ def test_directions_polyline5(self):

responses.add(
responses.GET,
"https://router.project-osrm.org/route/v1/driving/{}".format(coords),
f"https://routing.openstreetmap.de/routed-bike/route/v1/{query['profile']}/{coords}",
status=200,
json=ENDPOINTS_RESPONSES["osrm"]["directions_polyline"],
content_type="application/json",
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_directions_polyline6(self):

responses.add(
responses.GET,
"https://router.project-osrm.org/route/v1/driving/{}".format(coords),
f"https://routing.openstreetmap.de/routed-bike/route/v1/{query['profile']}/{coords}",
status=200,
json=ENDPOINTS_RESPONSES["osrm"]["directions_polyline6"],
content_type="application/json",
Expand Down Expand Up @@ -230,18 +230,18 @@ def test_full_matrix(self):

responses.add(
responses.GET,
"https://router.project-osrm.org/table/v1/driving/{}".format(coords),
f"https://routing.openstreetmap.de/routed-bike/table/v1/{query['profile']}/{coords}",
status=200,
json=ENDPOINTS_RESPONSES["osrm"]["matrix"],
content_type="application/json",
)

matrix = self.client.matrix(**query)

print(responses.calls[0].request.url)
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://router.project-osrm.org/table/v1/driving/8.688641,49.420577;8.680916,49.415776;8.780916,49.445776"
"?annotations=distance%2Cduration&fallback_speed=42",
f"https://routing.openstreetmap.de/routed-bike/table/v1/{query['profile']}/8.688641,49.420577;8.680916,49.415776;8.780916,49.445776?annotations=distance%2Cduration&bearings=50%2C50%3B50%2C50%3B50%2C50&fallback_speed=42&radiuses=500%3B500%3B500",
responses.calls[0].request.url,
)
self.assertIsInstance(matrix, Matrix)
Expand All @@ -259,7 +259,7 @@ def test_few_sources_destinations_matrix(self):

responses.add(
responses.GET,
"https://router.project-osrm.org/table/v1/driving/{}".format(coords),
f"https://routing.openstreetmap.de/routed-bike/table/v1/{query['profile']}/{coords}",
status=200,
json=ENDPOINTS_RESPONSES["osrm"]["matrix"],
content_type="application/json",
Expand All @@ -268,8 +268,8 @@ def test_few_sources_destinations_matrix(self):
self.client.matrix(**query)

self.assertEqual(1, len(responses.calls))
print(responses.calls[0].request.url)
self.assertURLEqual(
"https://router.project-osrm.org/table/v1/driving/8.688641,49.420577;8.680916,49.415776;8.780916,49.445776?"
"destinations=0%3B2&sources=1%3B2&annotations=distance%2Cduration",
f"https://routing.openstreetmap.de/routed-bike/table/v1/{query['profile']}/8.688641,49.420577;8.680916,49.415776;8.780916,49.445776?annotations=distance%2Cduration&bearings=50%2C50%3B50%2C50%3B50%2C50&destinations=0%3B2&radiuses=500%3B500%3B500&sources=1%3B2",
responses.calls[0].request.url,
)

0 comments on commit c18e8c9

Please sign in to comment.