commit | author | age
|
138f39
|
1 |
<?php |
c51c83
|
2 |
require('vendor/autoload.php'); |
JK |
3 |
require('common.php'); |
138f39
|
4 |
|
JK |
5 |
use transit_realtime\FeedMessage; |
|
6 |
|
|
7 |
class IdMapper { |
|
8 |
private $jsonTrips = []; |
|
9 |
private $gtfsTrips = []; |
|
10 |
|
|
11 |
private $specialNames = [ |
|
12 |
'Zjazd do zajezdni', |
|
13 |
'Przejazd techniczny', |
|
14 |
'Wyjazd na trasÄ™', |
|
15 |
]; |
|
16 |
|
|
17 |
public static function convertTripId($tripId) { |
|
18 |
$tripId = explode('_', $tripId); |
|
19 |
if($tripId[0] != 'block') return; |
|
20 |
if($tripId[2] != 'trip') return; |
|
21 |
return 4096 * (int)$tripId[1] + (int)$tripId[3]; |
|
22 |
} |
|
23 |
|
|
24 |
public function loadJson($file) { |
|
25 |
$json = json_decode(file_get_contents($file)); |
|
26 |
foreach($json->vehicles as $vehicle) { |
|
27 |
if(isset($vehicle->isDeleted) && $vehicle->isDeleted) continue; |
|
28 |
if(!isset($vehicle->tripId) || !$vehicle->tripId) continue; |
|
29 |
if(!isset($vehicle->name) || !$vehicle->name) continue; |
|
30 |
if(!isset($vehicle->latitude) || !$vehicle->latitude) continue; |
|
31 |
if(!isset($vehicle->longitude) || !$vehicle->longitude) continue; |
|
32 |
foreach($this->specialNames as $name) { |
|
33 |
if(substr($vehicle->name, -strlen($name)) == $name) { |
|
34 |
continue; |
|
35 |
} |
|
36 |
} |
|
37 |
$this->jsonTrips[(int)$vehicle->tripId] = [ |
|
38 |
'id' => $vehicle->id, |
|
39 |
'latitude' => (float)$vehicle->latitude / 3600000.0, |
|
40 |
'longitude' => (float)$vehicle->longitude / 3600000.0, |
|
41 |
]; |
|
42 |
} |
|
43 |
ksort($this->jsonTrips); |
|
44 |
} |
|
45 |
|
|
46 |
public function loadGtfs($file) { |
|
47 |
$data = file_get_contents($file); |
|
48 |
$feed = new FeedMessage(); |
|
49 |
$feed->parse($data); |
|
50 |
foreach ($feed->getEntityList() as $entity) { |
|
51 |
$vehiclePosition = $entity->getVehicle(); |
|
52 |
$position = $vehiclePosition->getPosition(); |
|
53 |
$vehicle = $vehiclePosition->getVehicle(); |
|
54 |
$trip = $vehiclePosition->getTrip(); |
|
55 |
$tripId = $trip->getTripId(); |
|
56 |
$this->gtfsTrips[self::convertTripId($tripId)] = [ |
c51c83
|
57 |
'id' => $entity->getId(), |
138f39
|
58 |
'num' => $vehicle->getLicensePlate(), |
JK |
59 |
'tripId' => $tripId, |
|
60 |
'latitude' => $position->getLatitude(), |
|
61 |
'longitude' => $position->getLongitude(), |
|
62 |
]; |
|
63 |
} |
|
64 |
ksort($this->gtfsTrips); |
|
65 |
} |
|
66 |
|
|
67 |
public function findOffset() { |
|
68 |
if(count($this->jsonTrips) == 0 || count($this->gtfsTrips) == 0) { |
|
69 |
return NULL; |
|
70 |
} |
|
71 |
|
|
72 |
$jsonTripIds = array_keys($this->jsonTrips); |
|
73 |
$gtfsTripIds = array_keys($this->gtfsTrips); |
|
74 |
|
|
75 |
$possibleOffsets = []; |
|
76 |
for($i = 0; $i < count($this->jsonTrips); $i++) { |
|
77 |
for($j = 0; $j < count($this->gtfsTrips); $j++) { |
|
78 |
$possibleOffsets[$jsonTripIds[$i] - $gtfsTripIds[$j]] = TRUE; |
|
79 |
} |
|
80 |
} |
|
81 |
$possibleOffsets = array_keys($possibleOffsets); |
|
82 |
|
|
83 |
$bestOffset = 0; |
|
84 |
$maxMatched = 0; |
|
85 |
$options = 0; |
|
86 |
|
|
87 |
foreach($possibleOffsets as $offset) { |
|
88 |
$matched = 0; |
|
89 |
|
|
90 |
foreach($gtfsTripIds as $tripId) { |
|
91 |
$tripId += $offset; |
|
92 |
if(isset($this->jsonTrips[$tripId])) { |
|
93 |
$matched++; |
|
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
if($matched > $maxMatched) { |
|
98 |
$bestOffset = $offset; |
|
99 |
$maxMatched = $matched; |
|
100 |
$options = 1; |
|
101 |
} elseif($matched == $maxMatched) { |
|
102 |
$options++; |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
if($options != 1) { |
c51c83
|
107 |
fwrite(STDERR, 'Found '.$options.' possible mappings!'."\n"); |
138f39
|
108 |
return FALSE; |
JK |
109 |
} |
|
110 |
return $bestOffset; |
|
111 |
} |
|
112 |
|
|
113 |
public function getMapping($offset) { |
|
114 |
$result = []; |
|
115 |
foreach($this->gtfsTrips as $gtfsTripId => $gtfsTrip) { |
|
116 |
$jsonTripId = $gtfsTripId + $offset; |
|
117 |
if(isset($this->jsonTrips[$jsonTripId])) { |
c51c83
|
118 |
$data = numToTypeB($gtfsTrip['id']); |
JK |
119 |
$num = $gtfsTrip['num']; |
|
120 |
if($data['num'] != $num) { |
15881f
|
121 |
// Ignore due to incorrect depot markings in the data |
JK |
122 |
//fwrite(STDERR, 'Got '.$num.', database has '.$data['num']."\n"); |
c51c83
|
123 |
} |
6609f2
|
124 |
$result[$this->jsonTrips[$jsonTripId]['id']] = $data; |
138f39
|
125 |
} |
JK |
126 |
} |
|
127 |
return $result; |
|
128 |
} |
|
129 |
} |
|
130 |
|
|
131 |
$mapper = new IdMapper(); |
|
132 |
$mapper->loadJson('./data/vehicles_A.json'); |
|
133 |
$mapper->loadGtfs('./data/VehiclePositions_A.pb'); |
|
134 |
$offset = $mapper->findOffset(); |
|
135 |
if($offset) { |
|
136 |
echo json_encode($mapper->getMapping($offset)); |
|
137 |
} |