From 08fe60f57219d6de876d1367b0a14832d4be36a7 Mon Sep 17 00:00:00 2001 From: Levin Herr Date: Sun, 15 Oct 2023 23:01:08 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Catch=20missing=20polylines=20(#?= =?UTF-8?q?1984)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Backend/BrouterController.php | 6 +++++- .../Controllers/Backend/Support/LocationController.php | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Backend/BrouterController.php b/app/Http/Controllers/Backend/BrouterController.php index 571c2ff05..629f4690f 100644 --- a/app/Http/Controllers/Backend/BrouterController.php +++ b/app/Http/Controllers/Backend/BrouterController.php @@ -186,10 +186,14 @@ public static function checkPolyline(HafasTrip $hafasTrip): void { } private static function checkIfPolylineHasMissingParts(HafasTrip $hafasTrip): bool { + if (is_null($hafasTrip->polyline)) { + Log::debug('Missing route found. No polyline available.'); + return true; + } $geoJson = json_decode($hafasTrip->polyline->polyline); $features = $geoJson->features; $lastStopOver = null; // To detect whether as the crow flies or real routing - foreach ($features as $key => $data) { + foreach ($features as $data) { if (!isset($data->properties->id)) { $lastStopOver = null; } else { diff --git a/app/Http/Controllers/Backend/Support/LocationController.php b/app/Http/Controllers/Backend/Support/LocationController.php index d22739b0a..dd8348d2f 100644 --- a/app/Http/Controllers/Backend/Support/LocationController.php +++ b/app/Http/Controllers/Backend/Support/LocationController.php @@ -69,7 +69,7 @@ public function calculateLivePosition(): ?LivePointDto { $hafasTrip = $this->hafasTrip; $newStopovers = $this->filterStopoversFromStatus(); - if (!$newStopovers) { + if (!$newStopovers || !isset($hafasTrip->polyline->polyline)) { return null; } if (count($newStopovers) === 1) { @@ -144,7 +144,10 @@ private function getDistanceFromGeoJson(stdClass $geoJson): int { * @throws JsonException */ private function getPolylineWithTimestamps(): stdClass { - $geoJsonObj = json_decode($this->hafasTrip->polyline->polyline, false, 512, JSON_THROW_ON_ERROR); + $geoJsonObj = json_decode('{"type":"FeatureCollection","features":[]}', false, 512, JSON_THROW_ON_ERROR); + if (isset($this->hafasTrip->polyline)) { + $geoJsonObj = json_decode($this->hafasTrip->polyline->polyline, false, 512, JSON_THROW_ON_ERROR); + } $stopovers = $this->hafasTrip->stopovers; $stopovers = $stopovers->map(function ($stopover) {