Jacek Kowalski
2019-06-21 b6272077e75e77d7828ff5752e7ea7c9ec4942e8
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;
37             foreach($this->specialNames as $name) {
38                 if(substr($vehicle->name, -strlen($name)) == $name) {
39                     continue;
40                 }
41             }
7b7026 42             $this->ttssTrips[(string)$vehicle->tripId] = [
JK 43                 'id' => (string)$vehicle->id,
4673cc 44                 'latitude' => (float)$vehicle->latitude / 3600000.0,
JK 45                 'longitude' => (float)$vehicle->longitude / 3600000.0,
46             ];
47         }
0c85a7 48         ksort($this->ttssTrips);
4673cc 49     }
JK 50     
9afb6b 51     public function getTTSSDate() {
JK 52         return $this->ttssDate / 1000.0;
53     }
54     
0c85a7 55     public function loadGTFSRT($file) {
4673cc 56         $data = file_get_contents($file);
JK 57         $feed = new FeedMessage();
58         $feed->parse($data);
9afb6b 59         $this->gtfsrtDate = $feed->header->timestamp;
4673cc 60         foreach ($feed->getEntityList() as $entity) {
JK 61             $vehiclePosition = $entity->getVehicle();
62             $position = $vehiclePosition->getPosition();
63             $vehicle = $vehiclePosition->getVehicle();
64             $trip = $vehiclePosition->getTrip();
65             $tripId = $trip->getTripId();
0c85a7 66             $this->gtfsrtTrips[self::convertTripId($tripId)] = [
7b7026 67                 'id' => (string)$entity->getId(),
4673cc 68                 'num' => $vehicle->getLicensePlate(),
JK 69                 'tripId' => $tripId,
70                 'latitude' => $position->getLatitude(),
71                 'longitude' => $position->getLongitude(),
72             ];
73         }
0c85a7 74         ksort($this->gtfsrtTrips);
4673cc 75     }
JK 76     
9afb6b 77     public function getGTFSRTDate() {
JK 78         return $this->gtfsrtDate;
79     }
80     
4673cc 81     public function findOffset() {
0c85a7 82         if(count($this->ttssTrips) == 0 || count($this->gtfsrtTrips) == 0) {
4673cc 83             return NULL;
JK 84         }
85         
0c85a7 86         $ttssTripIds = array_keys($this->ttssTrips);
JK 87         $gtfsTripIds = array_keys($this->gtfsrtTrips);
4673cc 88         
JK 89         $possibleOffsets = [];
0c85a7 90         for($i = 0; $i < count($this->ttssTrips); $i++) {
JK 91             for($j = 0; $j < count($this->gtfsrtTrips); $j++) {
92                 $possibleOffsets[$ttssTripIds[$i] - $gtfsTripIds[$j]] = TRUE;
4673cc 93             }
JK 94         }
95         $possibleOffsets = array_keys($possibleOffsets);
96         
97         $bestOffset = 0;
98         $maxMatched = 0;
99         $options = 0;
100         
101         foreach($possibleOffsets as $offset) {
102             $matched = 0;
103             
104             foreach($gtfsTripIds as $tripId) {
105                 $tripId += $offset;
0c85a7 106                 if(isset($this->ttssTrips[$tripId])) {
4673cc 107                     $matched++;
JK 108                 }
109             }
110             
111             if($matched > $maxMatched) {
112                 $bestOffset = $offset;
113                 $maxMatched = $matched;
114                 $options = 1;
115             } elseif($matched == $maxMatched) {
116                 $options++;
117             }
118         }
119         
120         if($options != 1) {
121             throw new Exception('Found '.$options.' possible mappings!');
122         }
123         return $bestOffset;
124     }
125     
7b7026 126     public function mapUsingOffset($offset) {
4673cc 127         $result = [];
0c85a7 128         foreach($this->gtfsrtTrips as $gtfsTripId => $gtfsTrip) {
JK 129             $ttssTripId = $gtfsTripId + $offset;
130             if(isset($this->ttssTrips[$ttssTripId])) {
7b7026 131                 $result[$this->ttssTrips[$ttssTripId]['id']] = $gtfsTrip['id'];
4673cc 132             }
JK 133         }
134         return $result;
135     }
136 }