-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculateDistances.php
45 lines (37 loc) · 1.34 KB
/
calculateDistances.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
require_once("common.inc.php");
function crowDistance($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
return $dist * 60 * 1.1515 * 1.609344;
}
function drivingInfo($jsonFile) {
$directions = json_decode(file_get_contents($jsonFile));
$distance = 0;
$duration = 0;
$route = $directions->routes[0];
$leg = $route->legs[0];
$distance = $leg->distance->value / 1000;
$duration = $leg->duration->value / 3600;
return array( "distance" => $distance, "duration" => $duration);
}
for ($i = 0; $i < count($stadia); $i++) {
$stadiumI = $stadia[$i];
$crowDistance = 0;
$drivingDistance = 0;
$drivingDuration = 0;
for ($j = 0; $j < count($stadia); $j++) {
if ($j == $i) {
continue;
}
$stadiumJ = $stadia[$j];
$crowDistance += crowDistance($stadiumI[$idxLat], $stadiumI[$idxLon], $stadiumJ[$idxLat], $stadiumJ[$idxLon]);
$drivingInfo = drivingInfo($directionsDir."/".$stadiumI[$idxTeamName]." - ".$stadiumJ[$idxTeamName].".json");
$drivingDistance += $drivingInfo["distance"];
$drivingDuration += $drivingInfo["duration"];
}
echo $stadiumI[$idxTeamName]."\t".round($crowDistance, 2)."\t".round($drivingDistance, 2)."\t".round($drivingDuration, 2)."\n";
}
?>