Skip to content

Commit

Permalink
🚑 Recalculate points and distance if a matching brouter polyline is f…
Browse files Browse the repository at this point in the history
…ound (#2516)
  • Loading branch information
HerrLevin authored Apr 21, 2024
1 parent b11deeb commit f078171
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions app/Http/Controllers/Backend/BrouterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand All @@ -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]);

Expand Down

0 comments on commit f078171

Please sign in to comment.