From 230da1218970335949b39e1f029684594a3a8e84 Mon Sep 17 00:00:00 2001 From: khamaileon Date: Fri, 27 Oct 2023 12:10:46 +0200 Subject: [PATCH] handle ZERO_RESULTS in google matrix (#122) --- routingpy/routers/google.py | 24 ++++++++++++++++-------- tests/test_helper.py | 1 + 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/routingpy/routers/google.py b/routingpy/routers/google.py index d832179..07e2c2f 100644 --- a/routingpy/routers/google.py +++ b/routingpy/routers/google.py @@ -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) diff --git a/tests/test_helper.py b/tests/test_helper.py index 5ac5597..854b596 100644 --- a/tests/test_helper.py +++ b/tests/test_helper.py @@ -421,6 +421,7 @@ { "elements": [ { + "status": "OK", "distance": {"text": "225 mi", "value": 361957}, "duration": {"text": "3 hours 50 mins", "value": 13813}, }