-
Notifications
You must be signed in to change notification settings - Fork 0
/
closest_stops.php
executable file
·127 lines (98 loc) · 3.35 KB
/
closest_stops.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
ini_set("Allow_url_include", true);
//error_reporting(E_ALL);
$userLat = $_GET["lat"];
$userLon = $_GET["lon"];
$resultCount = 10;
$nestedDeparturesCount = 10
if (isset($_GET["resultCount"])) {
$resultCount = intval($_GET["resultCount"]);
}
if (isset($_GET["nestedDeparturesCount"])) {
$resultCount = intval($_GET["nestedDeparturesCount"]);
}
$userGeoLocation = array(
"lat" => doubleval($userLat),
"lon" => doubleval($userLon)
);
function distanceMeters($geo1, $geo2) {
$lat1 = $geo1["lat"];
$lon1 = $geo1["lon"];
$lat2 = $geo2["lat"];
$lon2 = $geo2["lon"];
$x = deg2rad( $lon1 - $lon2 ) * cos( deg2rad( $lat1 ) );
$y = deg2rad( $lat1 - $lat2 );
$dist = 6371000.0 * sqrt( $x*$x + $y*$y );
return $dist;
}
function departuresForStopId($stopId) {
$content = file_get_contents('http://ivu.aseag.de/interfaces/ura/instant_V1?ReturnList=VehicleID,LineName,TripID,DirectionID,DestinationName,EstimatedTime&StopId='.$stopId);
$stringsToDelete = array("\"", "[", "]", "\r");
$filtered = str_replace($stringsToDelete, "", $content);
$lines1 = explode("\n",$filtered);
unset($lines1[0]); // remove item at index 0
$lines = array_values($lines1); // 'reindex' array
$connections = array();
foreach ($lines as $line) {
$elements = explode(",",$line);
$lineName = str_replace(".","",$elements[1]);
$vehicleId = $elements[4];
$tripId = intval($elements[5]);
$lineDestination = $elements[3];
$eta = intval($elements[6]);
$departureTime = new DateTime();
date_timezone_set($departureTime, timezone_open('Europe/Berlin'));
date_timestamp_set($departureTime, $eta/1000);
$connections[] = array (
"eta" => $eta/1000,
"local_time" => date_format($departureTime, "H:i:s"),
"line" => $lineName,
"destination" => $lineDestination,
"live_info" => ($vehicleId != "null"),
"tripId" => $tripId,
);
if (count($connections) >= $nestedDeparturesCount) {
break;
}
};
usort($connections, function ($item1, $item2) {
return $item1["eta"] <=> $item2["eta"];
});
return $connections;
}
$stopsString = file_get_contents("stops.txt");
$stops = explode("\n",$stopsString);
array_shift($stops);
$results = array();
foreach ($stops as $stop) {
$stop = str_replace("\"","", $stop);
$elements = explode(",",$stop);
$result = array();
$id = intval($elements[0]);
$name = $elements[2];
$lat = doubleval($elements[3]);
$lon = doubleval($elements[4]);
$result["id"] = $id;
$result["name"] = $name;
$stopGeoLoc = array(
"lat" => $lat,
"lon" => $lon
);
$result["geoLocation"] = $stopGeoLoc;
$result["distance"] = distanceMeters($userGeoLocation, $stopGeoLoc);
$results[] = $result;
}
usort($results, function ($item1, $item2) {
return $item1["distance"] <=> $item2["distance"];
});
array_splice($results, $resultCount);
$i = 0;
foreach ($results as $result) {
$results[$i]["departures"] = departuresForStopId($result["id"]);
$i++;
}
$encoded = json_encode($results,JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
//$encoded = json_encode($results);
header ('Content-type: application/json;charset=utf-8');
exit ($encoded);
?>