Jacek Kowalski
2019-06-30 0b6d2f4164b9d823fb2a49d9fb4e83242f695e09
commit | author | age
4673cc 1 <?php
JK 2 use transit_realtime\FeedMessage;
3
4 class Mapper {
9afb6b 5     private $ttssDate = NULL;
0c85a7 6     private $ttssTrips = [];
9afb6b 7     private $gtfsrtDate = NULL;
0c85a7 8     private $gtfsrtTrips = [];
4673cc 9     private $logger = NULL;
JK 10     
11     private $specialNames = [
12         'Zjazd do zajezdni',
13         'Przejazd techniczny',
14         'Wyjazd na trasÄ™',
15     ];
16     
17     public function __construct() {
18         $this->logger = new Monolog\Logger(__CLASS__);
19     }
20     
21     public static function convertTripId($tripId) {
22         $tripId = explode('_', $tripId);
23         if($tripId[0] != 'block') return;
24         if($tripId[2] != 'trip') return;
25         return 4096 * (int)$tripId[1] + (int)$tripId[3];
26     }
27     
28     public function loadTTSS($file) {
0c85a7 29         $ttss = json_decode(file_get_contents($file));
9afb6b 30         $this->ttssDate = $ttss->lastUpdate;
0c85a7 31         foreach($ttss->vehicles as $vehicle) {
4673cc 32             if(isset($vehicle->isDeleted) && $vehicle->isDeleted) continue;
JK 33             if(!isset($vehicle->tripId) || !$vehicle->tripId) continue;
34             if(!isset($vehicle->name) || !$vehicle->name) continue;
35             if(!isset($vehicle->latitude) || !$vehicle->latitude) continue;
36             if(!isset($vehicle->longitude) || !$vehicle->longitude) continue;
c077c7 37             list($line, $direction) = explode(' ', $vehicle->name, 2);
JK 38             foreach($this->specialNames as $specialName) {
39                 if(substr($vehicle->name, -strlen($specialName)) == $specialName) {
4673cc 40                     continue;
JK 41                 }
42             }
7b7026 43             $this->ttssTrips[(string)$vehicle->tripId] = [
JK 44                 'id' => (string)$vehicle->id,
c077c7 45                 'line' => $line,
JK 46                 'direction' => $direction,
4673cc 47                 'latitude' => (float)$vehicle->latitude / 3600000.0,
JK 48                 'longitude' => (float)$vehicle->longitude / 3600000.0,
49             ];
50         }
0c85a7 51         ksort($this->ttssTrips);
4673cc 52     }
JK 53     
9afb6b 54     public function getTTSSDate() {
JK 55         return $this->ttssDate / 1000.0;
56     }
57     
c077c7 58     public function getTTSSTrips() {
JK 59         return $this->ttssTrips;
60     }
61     
0c85a7 62     public function loadGTFSRT($file) {
4673cc 63         $data = file_get_contents($file);
JK 64         $feed = new FeedMessage();
65         $feed->parse($data);
9afb6b 66         $this->gtfsrtDate = $feed->header->timestamp;
4673cc 67         foreach ($feed->getEntityList() as $entity) {
JK 68             $vehiclePosition = $entity->getVehicle();
69             $position = $vehiclePosition->getPosition();
70             $vehicle = $vehiclePosition->getVehicle();
71             $trip = $vehiclePosition->getTrip();
72             $tripId = $trip->getTripId();
0c85a7 73             $this->gtfsrtTrips[self::convertTripId($tripId)] = [
7b7026 74                 'id' => (string)$entity->getId(),
4673cc 75                 'num' => $vehicle->getLicensePlate(),
JK 76                 'tripId' => $tripId,
77                 'latitude' => $position->getLatitude(),
78                 'longitude' => $position->getLongitude(),
79             ];
80         }
0c85a7 81         ksort($this->gtfsrtTrips);
4673cc 82     }
JK 83     
9afb6b 84     public function getGTFSRTDate() {
JK 85         return $this->gtfsrtDate;
86     }
87     
4673cc 88     public function findOffset() {
0c85a7 89         if(count($this->ttssTrips) == 0 || count($this->gtfsrtTrips) == 0) {
4673cc 90             return NULL;
JK 91         }
92         
0c85a7 93         $ttssTripIds = array_keys($this->ttssTrips);
JK 94         $gtfsTripIds = array_keys($this->gtfsrtTrips);
4673cc 95         
JK 96         $possibleOffsets = [];
0c85a7 97         for($i = 0; $i < count($this->ttssTrips); $i++) {
JK 98             for($j = 0; $j < count($this->gtfsrtTrips); $j++) {
99                 $possibleOffsets[$ttssTripIds[$i] - $gtfsTripIds[$j]] = TRUE;
4673cc 100             }
JK 101         }
102         $possibleOffsets = array_keys($possibleOffsets);
103         
104         $bestOffset = 0;
105         $maxMatched = 0;
106         $options = 0;
107         
108         foreach($possibleOffsets as $offset) {
109             $matched = 0;
110             
111             foreach($gtfsTripIds as $tripId) {
112                 $tripId += $offset;
0c85a7 113                 if(isset($this->ttssTrips[$tripId])) {
4673cc 114                     $matched++;
JK 115                 }
116             }
117             
118             if($matched > $maxMatched) {
119                 $bestOffset = $offset;
120                 $maxMatched = $matched;
121                 $options = 1;
122             } elseif($matched == $maxMatched) {
123                 $options++;
124             }
125         }
126         
127         if($options != 1) {
128             throw new Exception('Found '.$options.' possible mappings!');
129         }
130         return $bestOffset;
131     }
132     
7b7026 133     public function mapUsingOffset($offset) {
4673cc 134         $result = [];
0c85a7 135         foreach($this->gtfsrtTrips as $gtfsTripId => $gtfsTrip) {
JK 136             $ttssTripId = $gtfsTripId + $offset;
137             if(isset($this->ttssTrips[$ttssTripId])) {
7b7026 138                 $result[$this->ttssTrips[$ttssTripId]['id']] = $gtfsTrip['id'];
4673cc 139             }
JK 140         }
141         return $result;
142     }
143 }