Thanasoft-H2F/gestion/lib/Service/GeoService.php

129 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
/**
* Calendar App
*
* @copyright 2021 Anna Larch <anna.larch@gmx.net>
*
* @author Anna Larch <anna.larch@gmx.net>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Gestion\Service;
use Exception;
use OCA\Gestion\Constants\GeoConstant;
use OCA\Gestion\Helpers\GeoHelpers;
class GeoService {
public function __construct() {
}
/**
* Calcul la distance entre les deux points à vol d'oiseau
*/
private function getDistanceInKmBetweenTwoPoints($lat1, $lon1, $lat2, $lon2) {
$R = 6371; // Rayon moyen de la Terre en kilomètres
$dLat = deg2rad($lat2 - $lat1);
$dLon = deg2rad($lon2 - $lon1);
$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin($dLon/2) * sin($dLon/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$d = $R * $c;
return round($d, 2);
}
private function getTravelingHourBetweenTwoPoints(array $origin,array $destination,$mode = "driving"){
$baseUrl = "https://api.geoapify.com/v1/routing";
$originPoints = GeoHelpers::getPointsTextFromLatitudeAndLongitude($origin["latitude"],$origin["longitude"]);
$destinationPoints = GeoHelpers::getPointsTextFromLatitudeAndLongitude($destination["latitude"],$destination["longitude"]);
$fullUrl = $baseUrl."?waypoints=$originPoints|$destinationPoints&mode=$mode&apiKey=9e23d93e7f454c988344f9171bf867aa";
$curl = curl_init();
try {
curl_setopt_array($curl, array(
CURLOPT_URL => $fullUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
if ($response === false) {
return 0;
} else {
$timeInSecondes = json_decode($response)->features[0]->properties->time;
$travelTimeHours = $timeInSecondes / 3600;
$travelTimeHours = round($travelTimeHours, 2);
return $travelTimeHours;
}
}
catch(Exception $e){
return 0;
}
}
public function getTotalDistanceAndTotalTravelingHoursBetweenDevisLocationByRouteLines(array $routeLines){
$distanceCumul = 0;
$totalTravelingHoursBetweenTwoDevisLocation = 0;
$lastPoint = NULL;
for ($i=0; $i < sizeof($routeLines); $i++) {
$currentDistance = 0;
if($routeLines[$i]['lieu_id'] != NULL){
$lastPoint = $routeLines[$i];
}
if($lastPoint['lieu_id'] != NULL && $routeLines[$i+1]['lieu_id'] != NULL){
$currentDistance = $this->getDistanceInKmBetweenTwoPoints(
floatval(value: $lastPoint['latitude']),
floatval($lastPoint['longitude']),
floatval($routeLines[$i+1]['latitude']),
floatval($routeLines[$i+1]['longitude'])
);
$targetIsBetweenTwoDevisLocation = $lastPoint['source'] != "siege" && $routeLines[$i+1]["source"] != "siege";
if($targetIsBetweenTwoDevisLocation){
$originPoints = [
"latitude" => $lastPoint["latitude"],
"longitude" => $lastPoint["longitude"]
];
$destinationPoints = [
"latitude" => $routeLines[$i+1]["latitude"],
"longitude" => $routeLines[$i+1]["longitude"]
];
$totalTravelingHoursBetweenTwoDevisLocation+= $this->getTravelingHourBetweenTwoPoints(
$originPoints,
$destinationPoints,
GeoConstant::DRIVING_MODE
);
}
}
$distanceCumul += $currentDistance;
}
return [
"totalDistance" => $distanceCumul,
"totalTravelingHours" => $totalTravelingHoursBetweenTwoDevisLocation
];
}
}