Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add matrix and direction kwargs for osrm #49

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions routingpy/routers/osrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ def directions(
geometries=None,
overview=None,
dry_run=None,
**direction_kwargs
):
"""Get directions between an origin point and a destination point.

Use ``direction_kwargs`` for any missing ``directions`` request options.

For more information, visit http://project-osrm.org/docs/v5.5.1/api/#route-service.

:param locations: The coordinates tuple the route should be calculated
Expand Down Expand Up @@ -163,7 +166,15 @@ def directions(
)

params = self.get_direction_params(
radiuses, bearings, alternatives, steps, continue_straight, annotations, geometries, overview
radiuses,
bearings,
alternatives,
steps,
continue_straight,
annotations,
geometries,
overview,
**direction_kwargs
)

return self._parse_direction_json(
Expand All @@ -184,6 +195,7 @@ def get_direction_params(
annotations=None,
geometries=None,
overview=None,
**directions_kwargs
):
params = dict()

Expand Down Expand Up @@ -213,6 +225,8 @@ def get_direction_params(
if overview is not None:
params["overview"] = convert.convert_bool(overview)

params.update(directions_kwargs)

return params

@staticmethod
Expand Down Expand Up @@ -269,10 +283,13 @@ def matrix(
destinations=None,
dry_run=None,
annotations=("duration", "distance"),
**matrix_kwargs
):
"""
Gets travel distance and time for a matrix of origins and destinations.

Use ``matrix_kwargs`` for any missing ``matrix`` request options.

For more information visit http://project-osrm.org/docs/v5.5.1/api/#table-service.

:param locations: The coordinates tuple the route should be calculated
Expand Down Expand Up @@ -326,7 +343,7 @@ def matrix(
[convert.delimit_list([convert.format_float(f) for f in pair]) for pair in locations], ";"
)

params = self.get_matrix_params(sources, destinations, annotations)
params = self.get_matrix_params(sources, destinations, annotations, **matrix_kwargs)

return self._parse_matrix_json(
self.client._request(
Expand All @@ -335,7 +352,9 @@ def matrix(
)

@staticmethod
def get_matrix_params(sources=None, destinations=None, annotations=("duration", "distance")):
def get_matrix_params(
sources=None, destinations=None, annotations=("duration", "distance"), **matrix_kwargs
):
params = dict()

if sources:
Expand All @@ -347,6 +366,8 @@ def get_matrix_params(sources=None, destinations=None, annotations=("duration",
if annotations:
params["annotations"] = convert.delimit_list(annotations)

params.update(matrix_kwargs)

return params

@staticmethod
Expand Down
6 changes: 4 additions & 2 deletions tests/test_osrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def setUp(self):
def test_full_directions(self):
query = deepcopy(ENDPOINTS_QUERIES[self.name]["directions"])
query["alternatives"] = False
query["fallback_speed"] = 42
coords = convert.delimit_list([convert.delimit_list(pair) for pair in query["locations"]], ";")

responses.add(
Expand All @@ -52,7 +53,7 @@ def test_full_directions(self):
self.assertURLEqual(
"https://router.project-osrm.org/route/v1/car/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",
"geometries=geojson&overview=simplified&radiuses=500%3B500%3B500&steps=true&fallback_speed=42",
responses.calls[0].request.url,
)
self.assertIsInstance(routes, Direction)
Expand Down Expand Up @@ -224,6 +225,7 @@ def test_directions_polyline6(self):
@responses.activate
def test_full_matrix(self):
query = ENDPOINTS_QUERIES[self.name]["matrix"]
query["fallback_speed"] = 42
coords = convert.delimit_list([convert.delimit_list(pair) for pair in query["locations"]], ";")

responses.add(
Expand All @@ -239,7 +241,7 @@ def test_full_matrix(self):
self.assertEqual(1, len(responses.calls))
self.assertURLEqual(
"https://router.project-osrm.org/table/v1/car/8.688641,49.420577;8.680916,49.415776;8.780916,49.445776"
"?annotations=distance%2Cduration",
"?annotations=distance%2Cduration&fallback_speed=42",
responses.calls[0].request.url,
)
self.assertIsInstance(matrix, Matrix)
Expand Down