Skip to content

Commit

Permalink
handle ZERO_RESULTS in google matrix (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
khamaileon committed Oct 27, 2023
1 parent 2f0594b commit 230da12
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
24 changes: 16 additions & 8 deletions routingpy/routers/google.py
Expand Up @@ -515,13 +515,21 @@ def parse_matrix_json(response):
if response is None: # pragma: no cover
return Matrix()

durations = [
[destination["duration"]["value"] for destination in origin["elements"]]
for origin in response["rows"]
]
distances = [
[destination["distance"]["value"] for destination in origin["elements"]]
for origin in response["rows"]
]
durations = []
distances = []
for row in response["rows"]:
row_durations = []
row_distances = []
for element in row["elements"]:
if element["status"] == "OK":
row_durations.append(element["duration"]["value"])
row_distances.append(element["distance"]["value"])

else:
row_durations.append(None)
row_distances.append(None)

durations.append(row_durations)
distances.append(row_distances)

return Matrix(durations, distances, response)
1 change: 1 addition & 0 deletions tests/test_helper.py
Expand Up @@ -421,6 +421,7 @@
{
"elements": [
{
"status": "OK",
"distance": {"text": "225 mi", "value": 361957},
"duration": {"text": "3 hours 50 mins", "value": 13813},
}
Expand Down

0 comments on commit 230da12

Please sign in to comment.