From 6b49883f4c3e76202f6d4785e8a63b32a598d555 Mon Sep 17 00:00:00 2001 From: khamaileon Date: Fri, 27 Oct 2023 10:20:59 +0200 Subject: [PATCH] handle ZERO_RESULTS in google matrix --- routingpy/routers/google.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/routingpy/routers/google.py b/routingpy/routers/google.py index d832179..d7df104 100644 --- a/routingpy/routers/google.py +++ b/routingpy/routers/google.py @@ -515,13 +515,26 @@ 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"] - ] + @staticmethod + def parse_matrix_json(response): + if response is None: # pragma: no cover + return Matrix() + + 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)