This repository was archived by the owner on Dec 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.php
More file actions
371 lines (348 loc) · 10.6 KB
/
Copy pathdata.php
File metadata and controls
371 lines (348 loc) · 10.6 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/**
* Script to get info for Strasbourg
* in a format suitable for underground-live-map
* (https://github.com/dracos/underground-live-map)
*
* PHP version 5.4.6
*
* @category API
* @package API_CTS
* @author StrasWeb <contact@strasweb.fr>
* @license GPL http://www.gnu.org/licenses/gpl.html
* @link https://strasweb.fr/
* */
require 'config.php';
header('Content-Type: application/json; charset=utf-8');
$soap = new SoapClient(
'http://tr.cts-strasbourg.fr/HorTRwebserviceExtv3/Service.asmx?WSDL',
array('exceptions'=>false, 'compression'=>true)
);
$soap->__setSoapHeaders(
new SoapHeader(
'http://www.cts-strasbourg.fr/', 'CredentialHeader', new SoapVar(
array('ns1:ID'=>ID, 'ns1:MDP'=>PASS), SOAP_ENC_OBJECT
)
)
);
/**
* Conversion en radians
*
* @param float $num À convertir
*
* @return float Radians
* */
function toRad ($num)
{
return $num * M_PI / 180;
};
/**
* Permet d'obtenir la distance entre deux points
*
* @param array $coord1 Point 1
* @param array $coord2 Point 2
*
* @return int Distance
* */
function getDist ($coord1, $coord2)
{
$R = 6371; // km
$dLat = toRad($coord2[0] - $coord1[0]);
$dLon = toRad($coord2[1] - $coord1[1]);
$lat1 = toRad($coord1[0]);
$lat2 = toRad($coord2[0]);
$a = sin($dLat / 2) * sin($dLat / 2) +
sin($dLon / 2) * sin($dLon / 2) * cos($lat1) * cos($lat2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
return $R * $c;
};
/**
* Permet d'obtenir le prochain train à un arrêt
*
* @param string $code Code arrêt
*
* @return array Réponse SOAP
* */
function getNextTrain ($code)
{
global $soap;
$response = $soap->rechercheProchainesArriveesWeb(
array('Mode'=>'Tram', 'CodeArret'=>$code, 'NbHoraires'=>1)
);
return $response->rechercheProchainesArriveesWebResult;
}
/**
* Permet d'obtenir le code d'un arrêt
*
* @param string $name Nom de l'arrêt
*
* @return array Réponse SOAP
* */
function getCode ($name)
{
global $soap;
$response = $soap->rechercherCodesArretsDepuisLibelle(
array('NoPage'=>1, 'Saisie'=>$name)
);
return $response->rechercherCodesArretsDepuisLibelleResult;
}
/**
* Permet d'obtenir le point de départ d'un parcours
*
* @param array $line Parcours
*
* @return array
* */
function getPoint($line)
{
global $stations;
for ($index=0; $index<$stations->length; $index++) {
$name = $stations->item($index)
->getElementsByTagName('name')->item(0)->nodeValue;
if ($name == $line['next'][0]['name']) {
break;
}
}
$prev=$stations->item($index-(1*$line['dest']));
if (isset($prev)) {
$coords=explode(
',', $prev->getElementsByTagName('coordinates')->item(0)->nodeValue
);
$coords=array(floatval($coords[1]), floatval($coords[0]));
$name=$prev->getElementsByTagName('name')->item(0)->nodeValue;
return array('name'=>$name, 'coord'=>$coords);
} else {
return array(
'name'=>$line['next'][0]['name'],
'coord'=>$line['next'][0]['point']
);
}
}
/**
* Ajoute des points intermédiaires à un parcours
*
* @param array $line Parcours
*
* @return array
* */
function addPoints ($line)
{
global $stations;
for ($index=0; $index<$stations->length; $index++) {
$name=$stations->item($index)
->getElementsByTagName('name')->item(0)->nodeValue;
if ($name == $line['next'][0]['name']) {
break;
}
}
$i=1;
$steps=array();
$prev=$stations->item($index-($i*$line['dest']));
$diff=0;
if (isset($prev)) {
$prev=explode(
',', $prev->getElementsByTagName('coordinates')->item(0)->nodeValue
);
$prev=array(floatval($prev[1]), floatval($prev[0]));
$diff=$line['next'][0]['mins'] - getTime($line['next'][0]['point'], $prev);
$time=0;
while ($diff) {
if ($diff<=$time) {
break;
}
$num = $index-($i*$line['dest']);
if ($num>0 && $num<$stations->length) {
$next =explode(
',', $stations->item($num)
->getElementsByTagName('coordinates')->item(0)->nodeValue
);
$next=array(floatval($next[1]), floatval($next[0]));
$time= getTime($prev, $next);
$diff=$diff -$time;
$prev=$next;
array_push(
$steps, array('point'=>$prev, 'name'=>$stations
->item($num)->getElementsByTagName('name')
->item(0)->nodeValue,
'mins'=>round($diff, 1),
'dexp'=>'in '.round($diff, 1).' minutes')
);
$i++;
} else {
break;
}
}
/**
if (isset($num)) {
$prev=$stations->item($num);
if (isset($prev)) {
$prev =explode(
',', $stations->item($num)
->getElementsByTagName('coordinates')->item(0)->nodeValue
);
$prev=array(floatval($prev[1]), floatval($prev[0]));
$next=$stations->item($num-($i*$line['dest']));
if (isset($next)) {
$next =explode(
',',
$next->getElementsByTagName('coordinates')
->item(0)->nodeValue
);
$next=array(floatval($next[1]), floatval($next[0]));
$dist=($diff*SPEED)/60;
$x=$next[0]-$prev[0];
$y=$next[1]-$prev[1];
$ratio=$dist/sqrt($x*$x+$y*$y);
$x=$next[0]-($x*$ratio);
$y=$next[1]-($y*$ratio);
}
}
}
*/
}
if (isset($x)) {
return array($steps, array($x, $y));
} else {
return array($steps);
}
}
/**
* Permet d'obtenir le temps mis entre deux coordonnées
*
* @param array $coords Coordonnées
* @param array $prev Coordonnées précédents
*
* @return int Temps
* */
function getTime ($coords, $prev)
{
$dist=getDist($coords, $prev);
$time=($dist*60)/SPEED;
//$dist2=(($mins*$dist)/$time);
return $time;
}
/**
* Ajoute des arrêts au parcours
*
* @param array $next Prochain arrêt
* @param array $coords Coordonnées
* @param array $station Station de départ
* @param int $index Index de la station dans $stations
*
* @return void
* */
function addLines($next, $coords, $station, $index, $letter)
{
global $lines, $now, $stations;
if (substr($next->Destination, 0, 1)==$letter) {
$mins = $now->diff(new DateTime($next->Horaire))->i;
$id=$next->Destination.' '.uniqid();
$lines[$id]=array(
'title'=>$id, 'next'=>array()
);
if ($next->Destination == 'A Hautepierre') {
$prev=$stations->item($index+1);
$lines[$id]['dest']=-1;
} else {
$prev=$stations->item($index-1);
$lines[$id]['dest']=+1;
}
if (isset($prev)) {
$lines[$id]['left']=$prev->getElementsByTagName('name')->item(0)->nodeValue;
$lines[$id]['point']=explode(',', $prev->getElementsByTagName('coordinates')->item(0)->nodeValue);
$lines[$id]['point']=array(floatval($lines[$id]['point'][1]), floatval($lines[$id]['point'][0]));
}
array_push(
$lines[$id]['next'],
array('name'=>$station, 'point'=>$coords,
'mins'=>$mins, 'dexp'=>'in '.$mins.' minutes')
);
}
}
$letters=array('A'=>'#FF0000', 'B'=>'#00FFFF', 'C'=>'#FF7E00', 'D'=>'#17FF01', 'E'=>'#7F7FFF', 'F'=>'#99FF00');
//$letters=array('A'=>'#FF0000');
$output['stations']=array();
$output['trains']=array();
$output['polylines']=array();
$now = new DateTime();
header('Last-Modified: '.$now->format('r'));
header('Expires: '.$now->add(new DateInterval('PT2M'))->format('r'));
$lines=array();
foreach ($letters as $letter=>$color) {
$line = new DOMDocument();
$line->load('line'.$letter.'.xml');
$stations=$line->getElementsByTagName('Placemark');
$polylines=array($color, 0.8);
for ($i=0; $i<$stations->length; $i++) {
$station=$stations->item($i);
$name=$station->getElementsByTagName('name')->item(0)->nodeValue;
$coords=explode(
',', $station->getElementsByTagName('coordinates')->item(0)->nodeValue
);
$coords=array(floatval($coords[1]), floatval($coords[0]));
array_push($output['stations'], array('name'=>$name, 'point'=>$coords));
array_push($polylines, $coords);
$code=getCode($name);
if (isset($code->ListeArret->Arret)) {
if (is_array($code->ListeArret->Arret)) {
//
} else {
$name=$code->ListeArret->Arret->Libelle;
$code=$code->ListeArret->Arret->Code;
if (isset($code)) {
$next=(getNextTrain($code));
if (isset($next->ListeArrivee->Arrivee)) {
if (is_array($next->ListeArrivee->Arrivee)) {
foreach ($next->ListeArrivee->Arrivee as $next) {
$id=addLines($next, $coords, $name, $i, $letter);
}
} else {
$next = $next->ListeArrivee->Arrivee;
$id=addLines($next, $coords, $name, $i, $letter);
}
}
}
}
}
}
array_push($output['polylines'], $polylines);
}
/**
* Fonction de tri des segments
*
* @param array $a Arrêt 1
* @param array $b Arrêt 2
*
* @return int
* */
function sortMin ($a, $b)
{
if ($a['mins'] == $b['mins']) {
return 0;
}
return ($a['mins'] < $b['mins']) ? -1 : 1;
}
/*
foreach ($lines as &$line) {
$otherpoints=addPoints($line);
$line['next'] = array_merge($line['next'], $otherpoints[0]);
if (isset($otherpoints[1])) {
$line['point']=$otherpoints[1];
}
}
* */
foreach ($lines as &$line) {
usort($line['next'], 'sortMin');
if (!isset($line['point'])) {
$prev=getPoint($line);
$line['point']=$prev['coord'];
$line['left']=$prev['name'];
}
if ($line['next'][0]['mins']>0) {
array_push($output['trains'], $line);
}
}
$output['lastupdate']=$now->format('r');
$output['station']='Strasbourg';
print(json_encode($output));
?>