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