-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fixed distance calculation for foreign trips (#247)
* Improved distance calculation * Renamed function * Catched test cases * Codacy * Codacy
- Loading branch information
1 parent
5b0779a
commit aa048f2
Showing
3 changed files
with
134 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Backend; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\HafasTrip; | ||
use App\Models\TrainStopover; | ||
|
||
class GeoController extends Controller | ||
{ | ||
public static function calculateDistance( | ||
HafasTrip $hafasTrip, | ||
TrainStopover $origin, | ||
TrainStopover $destination | ||
): float|int { | ||
if ($hafasTrip->polyline == null || $hafasTrip?->getPolyLine?->polyline == null) { | ||
return self::calculateDistanceByStopovers($hafasTrip, $origin, $destination); | ||
} | ||
|
||
$allFeatures = json_decode($hafasTrip->getPolyLine->polyline); | ||
|
||
$originIndex = null; | ||
$destinationIndex = null; | ||
foreach ($allFeatures->features as $key => $data) { | ||
if (!isset($data->properties->id)) { | ||
continue; | ||
} | ||
if ($origin->trainStation->ibnr == $data->properties->id && $originIndex == null) { | ||
$originIndex = $key; | ||
} | ||
if ($destination->trainStation->ibnr == $data->properties->id) { | ||
$destinationIndex = $key; | ||
} | ||
} | ||
|
||
$slicedFeatures = array_slice($allFeatures->features, $originIndex, $destinationIndex - $originIndex + 1); | ||
|
||
$distance = 0; | ||
$lastStopover = null; | ||
foreach ($slicedFeatures as $stopover) { | ||
if ($lastStopover == null) { | ||
$lastStopover = $stopover; | ||
continue; | ||
} | ||
|
||
$distance += self::calculateDistanceBetweenCoordinates( | ||
latitudeA: $lastStopover->geometry->coordinates[0], | ||
longitudeA: $lastStopover->geometry->coordinates[1], | ||
latitudeB: $stopover->geometry->coordinates[0], | ||
longitudeB: $stopover->geometry->coordinates[1] | ||
); | ||
|
||
$lastStopover = $stopover; | ||
} | ||
return $distance; | ||
} | ||
|
||
/** | ||
* Fallback calculation if no polyline is given. Calculates the length using the coordinates of the stations. | ||
* @param HafasTrip $hafasTrip | ||
* @param TrainStopover $origin | ||
* @param TrainStopover $destination | ||
* @return float | ||
*/ | ||
private static function calculateDistanceByStopovers( | ||
HafasTrip $hafasTrip, | ||
TrainStopover $origin, | ||
TrainStopover $destination | ||
): float { | ||
$stopovers = $hafasTrip->stopoversNEW->sortBy('departure'); | ||
$originStopoverIndex = $stopovers->search($origin); | ||
$destinationStopoverIndex = $stopovers->search($destination); | ||
|
||
$stopovers = $stopovers->slice($originStopoverIndex, $destinationStopoverIndex - $originStopoverIndex + 1); | ||
|
||
$distance = 0; | ||
$lastStopover = null; | ||
foreach ($stopovers as $stopover) { | ||
if ($lastStopover == null) { | ||
$lastStopover = $stopover; | ||
continue; | ||
} | ||
$distance += self::calculateDistanceBetweenCoordinates( | ||
latitudeA: $lastStopover->trainStation->latitude, | ||
longitudeA: $lastStopover->trainStation->longitude, | ||
latitudeB: $stopover->trainStation->latitude, | ||
longitudeB: $stopover->trainStation->longitude | ||
); | ||
} | ||
return $distance; | ||
} | ||
|
||
private static function calculateDistanceBetweenCoordinates( | ||
float $latitudeA, | ||
float $longitudeA, | ||
float $latitudeB, | ||
float $longitudeB, | ||
int $decimals = 3 | ||
): float { | ||
if ($longitudeA === $longitudeB && $latitudeA === $latitudeB) { | ||
return 0.0; | ||
} | ||
|
||
$equatorialRadiusInKilometers = 6378.137; | ||
|
||
$pi = pi(); | ||
$latA = $latitudeA / 180 * $pi; | ||
$lonA = $longitudeA / 180 * $pi; | ||
$latB = $latitudeB / 180 * $pi; | ||
$lonB = $longitudeB / 180 * $pi; | ||
$distance = acos(sin($latA) * sin($latB) + cos($latA) * cos($latB) * cos($lonB - $lonA)) | ||
* $equatorialRadiusInKilometers; | ||
|
||
return round($distance, $decimals); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters