From f0781713c09448d510a6c3ba87eb71b312458fbd Mon Sep 17 00:00:00 2001 From: Levin Herr Date: Sun, 21 Apr 2024 15:13:20 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=20Recalculate=20points=20and=20dis?= =?UTF-8?q?tance=20if=20a=20matching=20brouter=20polyline=20is=20found=20(?= =?UTF-8?q?#2516)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Backend/BrouterController.php | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Backend/BrouterController.php b/app/Http/Controllers/Backend/BrouterController.php index 8a93b9808..f04c880dc 100644 --- a/app/Http/Controllers/Backend/BrouterController.php +++ b/app/Http/Controllers/Backend/BrouterController.php @@ -84,10 +84,15 @@ public static function reroutePolyline(Trip $trip): void { } //0. Check if brouter Polyline is already available - $polyline = PolyLine::where('parent_id', $trip->polyline_id)->orderBy('id', 'desc')->first(); - if ($polyline?->source === 'brouter') { + $childPolyline = PolyLine::where('parent_id', $trip->polyline_id)->orderBy('id', 'desc')->first(); + $currentPolyline = $trip->polyline()->first(); + if ($childPolyline?->source === 'brouter' || $currentPolyline?->source === 'brouter') { Log::debug('[RefreshPolyline] Brouter Polyline already available for Trip#' . $trip->trip_id); - $trip->update(['polyline_id' => $polyline->id]); + + if ($currentPolyline?->source !== 'brouter') { + //If the current Polyline is not from Brouter, we need to recalculate the distance and points + self::recalculateDistanceAndPoints($trip, $childPolyline); + } return; } @@ -128,8 +133,8 @@ public static function reroutePolyline(Trip $trip): void { $highestMappedKey = null; foreach ($trip->stopovers as $stopover) { $properties = [ - 'id' => $stopover->station->ibnr, - 'name' => $stopover->station->name, + 'id' => $stopover->station->ibnr, + 'name' => $stopover->station->name, ]; //Get feature with the lowest distance to station @@ -155,12 +160,22 @@ public static function reroutePolyline(Trip $trip): void { $geoJson['features'][$closestFeatureKey]['properties'] = $properties; } - $polyline = PolyLine::create([ - 'hash' => Str::uuid(), //In this case a non required unique key - 'polyline' => json_encode($geoJson), - 'source' => 'brouter', - 'parent_id' => $trip->polyline_id - ]); + $childPolyline = PolyLine::create([ + 'hash' => Str::uuid(), //In this case a non required unique key + 'polyline' => json_encode($geoJson), + 'source' => 'brouter', + 'parent_id' => $trip->polyline_id + ]); + self::recalculateDistanceAndPoints($trip, $childPolyline); + } + + /** + * @param Trip $trip + * @param $polyline + * + * @return void + */ + public static function recalculateDistanceAndPoints(Trip $trip, $polyline): void { $oldPolyLine = self::getOldPolyline($trip); $trip->update(['polyline_id' => $polyline->id]);