/
GPXIngest.class.php
1305 lines (957 loc) · 34.1 KB
/
GPXIngest.class.php
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/** GPXIngest Class
*
* Class to Ingest a basic GPX file, generate stats, convert it into an object and allow some basic manipulation.
* After ingest, the object can be output as JSON for easy storage with stats intact.
*
* @copyright (C) 2013 B Tasker (http://www.bentasker.co.uk). All rights reserved
* @license GNU GPL V2 - See LICENSE
*
* @version 1.2
*
* Where issue keys are included (GPXIN-[0-9]+), the relevant issue can be viewed at http://projects.bentasker.co.uk/jira_projects/browse/GPXIN.html
*/
class GPXIngest{
private $file;
private $xml;
private $journey;
private $tracks = array();
private $highspeeds;
private $lowspeeds;
private $journeyspeeds;
private $totaltimes;
private $ftimes;
private $trackduration;
private $smarttrack=true;
private $smarttrackthreshold = 3600;
private $suppresslocation = false;
private $suppressspeed = false;
private $suppresselevation = false;
private $suppressdate = false;
private $lastspeed = false;
private $lastspeedm = false;
private $ingest_version = 1.02;
private $entryperiod = 0;
private $experimentalFeatures = array('calcDistance'); // See GPXIN-17
private $featuretoggle = array();
/** Standard constructor
*
*/
function __construct(){
$this->journey = new stdClass();
}
/** Reset all relevant class variables - saves unloading the class if processing multiple files
*
*/
public function reset(){
$this->journey = new stdClass();
$this->tracks = array();
}
/** Load XML from a file
*
* @arg $file - string to XML file, can be relative or full path
*
*/
public function loadFile($file){
$this->xml = simplexml_load_file($file);
if ($this->xml){
return true;
}else{
$this->xml = false;
return false;
}
}
/** Load an XML string
*
* @arg $str - XML String
*
*/
public function loadString($str){
$this->xml = simplexml_load_string($str);
if ($this->xml){
return true;
}else{
$this->xml = false;
return false;
}
}
/** Load a JSON string into the object
*
* Useful if you've previously parsed a GPX file with this class and want to manipulate it at a later date
*
* @arg $json - JSON encapsulated string
*
*/
public function loadJSON($json){
$this->journey = json_decode($json);
if (!is_object($this->journey)){
$this->journey = false;
return false;
}
// We need to update our internal stats as well
foreach ($this->journey->journeys as $k => $v){
$this->tracks[$k]['name'] = $this->journey->journeys->$k->name;
$this->tracks[$k]['segments'] = array();
foreach ($v->segments as $seg => $segv){
$this->tracks[$k]['segments'][$seg] = count((array)$v->segments->$seg->points);
}
}
}
/** Load a JSON string from a file
*
*/
public function loadJSONFile($file){
return $this->loadJSON(file_get_contents($file));
}
/** Toggle SmartTrack on/off
*
*/
public function toggleSmartTrack(){
$this->smarttrack = ($this->smarttrack)? false : true;
return $this->smarttrack;
}
/** Get the current Smart Track Status
*
*/
public function smartTrackStatus(){
return $this->smarttrack;
}
/** Get the smartTrackThreshold
*
*/
public function smartTrackThreshold(){
return $this->smarttrackthreshold;
}
/** Set the smart Track trigger threshold
*
*/
public function setSmartTrackThreshold($thresh){
$this->smarttrackthreshold = $thresh;
}
/** Ingest the XML and convert into an object
*
* Also updates our reference arrays
*
*/
public function ingest(){
if (!is_object($this->xml)){
return false;
}
if (!is_object($this->journey)){
$this->journey = new stdClass();
}
// Initialise the object
$this->journey->created = new stdClass();
$this->journey->stats = new stdClass();
$this->journey->stats->trackpoints = 0;
$this->journey->stats->recordedDuration = 0;
$this->journey->stats->segments = 0;
$this->journey->stats->tracks = 0;
$this->journey->stats->maxacceleration = 0;
$this->journey->stats->maxdeceleration = 0;
$this->journey->stats->minacceleration = 0;
$this->journey->stats->mindeceleration = 0;
$this->journey->stats->avgacceleration = 0;
$this->journey->stats->avgdeceleration = 0;
$this->journey->stats->speedUoM = array();
$this->journey->stats->timeMoving = 0;
$this->journey->stats->timeStationary = 0;
$this->journey->stats->timeAccelerating = 0;
$this->journey->stats->timeDecelerating = 0;
$this->journey->stats->distanceTravelled = 0;
// Initialise the stats array
$this->totaltimes = array();
$this->highspeeds = array();
$this->journeyspeeds = array();
$this->lowspeeds = array();
$this->accels = array();
$this->decels = array();
$this->jeles = array();
$this->jeledevs = array();
$this->jdist = array(); //GPXIN-6
$unit = null;
// Add the metadata
$this->journey->created->creator = (string) $this->xml['creator'];
$this->journey->created->version = (string) $this->xml['version'];
$this->journey->created->format = 'GPX';
$this->journey->created->namespaces = $this->xml->getNamespaces(true);
if (!$this->suppressdate && isset($this->xml->time)){
$this->journey->created->time = strtotime($this->xml->time);
}
$this->journey->timezone = date_default_timezone_get();
// Create the GPXIngest Metadata object
$this->journey->metadata = new stdClass();
$this->journey->metadata->AutoCalc = array('speed'=>false);
$a = 0;
// There may be multiple tracks in one file
foreach ($this->xml->trk as $trk){
// Initialise the stats variables
$this->resetTrackStats();
$b = 0;
// Set the object key
//$jkey = "journey$a";
$jkey = $this->genTrackKey($a);
$this->initTrack($jkey,$trk->name);
// There may be multiple segments if GPS connectivity was lost - process each seperately
foreach ($trk->trkseg as $trkseg){
// Initialise the sub-stats variable
/*
$speed = 0;
$sspeed = array();
*/
$this->resetSegmentStats();
$x = 0;
$times = array();
$lastele = false;
$timemoving = 0;
$timestationary = 0;
// Set the segment key
$segkey = $this->genSegKey($b);
// Initialise the segment object
$this->initSegment($jkey,$segkey);
// Trackpoint details in trk - Push them into our object
foreach ($trkseg->trkpt as $trkpt){
// Initialise some variables
$key = "trackpt$x";
$ptspeed = (int)filter_var($trkpt->desc, FILTER_SANITIZE_NUMBER_INT);
$speed_string = (string) $trkpt->desc;
// If speed is not available (and we're not calculating anything which can be used) suppress speed
// Will need updating in GPXIN-20
if (!$trkpt->desc && !$this->expisenabled('calcDistance')){
$this->suppress('speed'); // Prevent warnings if speed is not available - See GPXIN-16
}
$time = strtotime($trkpt->time);
// If smarttrack is enabled, check the trackpt time difference
if ($this->smarttrack && $this->lasttimestamp && ($this->entryperiod > $this->smarttrackthreshold)){
// We need to start a new track, but first we have to finalise the stats
// We use unit, timemoving and stationary from the previous iteration
$this->writeSegmentStats($jkey,$segkey,$times,$x,$unit,$timemoving,$timestationary);
$this->writeTrackStats($jkey);
// Reset the segment counter
$b=0;
// Update the track counter
$a++;
// Reset the Key
$x=0;
$key = "trackpt$x";
$times = array();
// Get a new track key
$jkey = $this->genTrackKey($a);
$this->initTrack($jkey,$trk->name.$a);
// Get a new segment key
$segkey = $this->genSegKey($b);
$this->initSegment($jkey,$segkey);
// Reset stats
$this->resetTrackStats();
$this->resetSegmentStats();
}
if (!isset($this->journey->journeys->$jkey->segments->$segkey->points)){
$this->journey->journeys->$jkey->segments->$segkey->points = new stdClass();
}
$this->journey->journeys->$jkey->segments->$segkey->points->$key = new stdClass();
// Handle Extensions (GPXIN-20)
$this->journey->journeys->$jkey->segments->$segkey->points->$key->extensions = new stdClass();
if (isset($trkpt->extensions)){
foreach ($this->journey->created->namespaces as $ns=>$nsuri){
if (empty($ns)){
continue;
}
$ext = array();
foreach ($trkpt->extensions->children($nsuri) as $t){
$ext[$t->getName()] = (string)$t;
}
$this->journey->journeys->$jkey->segments->$segkey->points->$key->extensions->$ns = $ext;
}
}
// Calculate the period to which this trackpoint relates
if ($this->lasttimestamp){
$this->entryperiod = $time - $this->lasttimestamp;
}
// Write the track data - take into account whether we've suppressed any data elements
if (!$this->suppresslocation){
$lat = (string) $trkpt['lat']; // let's only caste once
$lon = (string) $trkpt['lon'];
$this->journey->journeys->$jkey->segments->$segkey->points->$key->lat = $lat;
$this->journey->journeys->$jkey->segments->$segkey->points->$key->lon = $lon;
/** Implemented in GPXIN-6 - currently experimental so will generally be 0 */
$dist = ($this->lastpos)? $this->calculateTravelledDistance($this->lastpos,array($lat,$lon)) : 0;
// Added for GPXIN-13
if (!$trkpt->desc || empty($speed_string)){
// Calculate the speed based on distance travelled and time
// distance is in feet
// Avoid div by 0
if ($this->entryperiod == 0){
$speed_string = "0 MPH";
$ptspeed = 0;
}else{
$fps = $dist / $this->entryperiod; // Feet per second
$mph = round(($fps * 0.681818),0);
$speed_string = "$mph MPH";
$ptspeed = (int)$mph;
}
// Make sure the metadata shows we did this calculation
$this->journey->metadata->AutoCalc['speed'] = true;
}
$this->lastpos = array($lat,$lon); // update the reference array
// Update the stats arrays
$this->fdist[] = $dist;
$this->sdist[] = $dist;
$this->jdist[] = $dist;
}
if ($this->lasttimestamp){
// Calculate time moving/stationary etc
if ($this->lastspeed){
if ($ptspeed > 0){
$timemoving = $timemoving + $this->entryperiod;
}else{
$timestationary = $timestationary + $this->entryperiod;
}
}
}
if (!$this->suppresselevation){
$ele = (string) $trkpt->ele;
$this->journey->journeys->$jkey->segments->$segkey->points->$key->elevation = $ele;
$change = 0;
if ($lastele){
$change = $ele - $lastele;
}
$this->journey->journeys->$jkey->segments->$segkey->points->$key->elevationChange = $change;
// Update the stats arrays - should be able to make this more efficient later
$this->jeles[] = $ele;
$this->seles[] = $ele;
$this->feles[] = $ele;
$this->jeledevs[] = $change;
$this->seledevs[] = $change;
$this->feledevs[] = $change;
// Update the elevation for the next time round the block
$lastele = $ele;
}
if (!$this->suppressdate){
$this->journey->journeys->$jkey->segments->$segkey->points->$key->time = $time;
// Update the times arrays
$times[] = $time;
// update lasttime - used by SmartTrack
$lasttime = $time;
}
if (!$this->suppressspeed){
// What is the speed recorded in?
$unit = strtolower(substr(rtrim($speed_string),strlen($speed_string)-3));
$this->journey->journeys->$jkey->segments->$segkey->points->$key->speed = $speed_string;
$this->journey->journeys->$jkey->segments->$segkey->points->$key->speedint = $ptspeed;
// Calculate speed stats
$this->speed = $this->speed + $ptspeed;
$this->fspeed[] = $ptspeed;
$this->sspeed[] = $ptspeed;
// Calculate acceleration
list($acc,$decc) = $this->calculateAcceleration($ptspeed,$time,$unit);
$this->journey->journeys->$jkey->segments->$segkey->points->$key->acceleration = $acc;
$this->journey->journeys->$jkey->segments->$segkey->points->$key->deceleration = $decc;
// There shouldn't usually be more than one UoM per track file, but you never know - that's why it's an array
if (!in_array($unit,$this->journey->stats->speedUoM)){
$this->journey->stats->speedUoM[] = $unit;
}
// Tracks may also, plausibly, contain more than one measurement
if (!in_array($unit,$this->journey->journeys->$jkey->stats->speedUoM)){
$this->journey->journeys->$jkey->stats->speedUoM[] = $unit;
}
// If there's more than one unit per segment on the other hand, something's wrong!
}else{
// We also use the speed array to identify the number of trackpoints
$this->fspeed[] = 1;
$unit = null;
}
// Set our values for the next run
$this->lasttimestamp = $time;
$this->lastspeed = $ptspeed;
// Up the counters
$x++;
}
$this->writeSegmentStats($jkey,$segkey,$times,$x,$unit,$timemoving,$timestationary);
$b++;
}
$this->writeTrackStats($jkey);
}
$modesearch = array_count_values($this->journeyspeeds);
// Finalise the object stats - again take suppression into account
if (!$this->suppressdate){
$this->journey->stats->start = min($this->totaltimes);
$this->journey->stats->end = max($this->totaltimes);
}
if (!$this->suppressspeed){
$this->journey->stats->maxSpeed = max($this->highspeeds);
$this->journey->stats->minSpeed = min($this->lowspeeds);
$this->journey->stats->modalSpeed = array_search(max($modesearch),$modesearch);
$this->journey->stats->avgspeed = round(array_sum($this->journeyspeeds) / $this->journey->stats->trackpoints,2);
$this->journey->stats->maxacceleration = max($this->accels);
$this->journey->stats->maxdeceleration = max($this->decels);
$this->journey->stats->minacceleration = min($this->accels);
$this->journey->stats->mindeceleration = min($this->decels);
$this->journey->stats->avgacceleration = round(array_sum($this->accels)/count($this->accels),2);
$this->journey->stats->avgdeceleration = round(array_sum($this->decels)/count($this->accels),2);
}
if (!$this->suppresselevation){
$this->journey->journeys->$jkey->segments->$segkey->stats->elevation = new stdClass();
$this->journey->journeys->$jkey->segments->$segkey->stats->elevation->max = max($this->jeles);
$this->journey->journeys->$jkey->segments->$segkey->stats->elevation->min = min($this->jeles);
$this->journey->journeys->$jkey->segments->$segkey->stats->elevation->avgChange = round(array_sum($this->jeledevs)/count($this->jeledevs),2);
}
if (!$this->suppresslocation){
$this->journey->stats->distanceTravelled = array_sum($this->jdist); // See GPXIN-6
}
// Add any relevant metadata
$this->journey->metadata->smartTrackStatus = ($this->smartTrackStatus())? 'enabled' : 'disabled';
$this->journey->metadata->smartTrackThreshold = $this->smartTrackThreshold();
$this->journey->metadata->suppression = array();
// Add a version number so the object can be used to identify which stats will/won't be present
$this->journey->metadata->GPXIngestVersion = $this->ingest_version;
// Add a list of the supported experimental features and whether they were enabled
$this->journey->metadata->experimentalFeatureState = $this->listExperimental();
$this->writeSuppressionMetadata();
// XML Ingest and conversion done!
}
/** Calculate the rate of (ac|de)celeration and update the relevant stats arrays.
* Also returns the values
*
* All returned values should be considered m/s^2 (i.e. the standard instrument)
*
* @arg - Speed (not including Unit -i.e. 1 not 1 KPH or MPH)
* @arg - timestamp of the speed recording
* @arg - Unit of measurement (i.e. kph)
*
* @return array - acceleration and deceleration.
*
*/
private function calculateAcceleration($speed,$timestamp,$unit){
$acceleration = 0;
$deceleration = 0;
// We need to convert the speed into metres per sec
if ($unit == 'kph'){
// I'm screwed if my logic is wrong here
// KPH -> m/s = x kph * 1000 = x metres per hour / 3600 = x metres per second
$speed = ((int)filter_var($speed, FILTER_SANITIZE_NUMBER_INT)* 1000)/3600;
}else{
// MPH.
// There are 1609.344 metres to a mile, suspect we may need some rounding done on this one
$speed = ((int)filter_var($speed, FILTER_SANITIZE_NUMBER_INT)* 1609.344)/3600;
}
// Can't calculate acceleration if we don't have a previous timestamp or speed. Also don't want to falsely record acc/dec if the speed is the same.
if (!$this->lasttimestamp || !$this->lastspeedm || $speed == $this->lastspeed){
$this->lastspeedm = $speed;
return array($acceleration,$deceleration);
}
// We use the formula
// (fV - iV)/t
// We'll worry about whether it's accel or decel after doing the maths
$velocity_change = ($speed - $this->lastspeedm) / $this->entryperiod;
if ($velocity_change < 0){
// It's deceleration
$deceleration = round(($velocity_change*-1),4);
$this->fdecel[] = $deceleration;
$this->decels[] = $deceleration;
$this->timedecel = $this->timedecel + $this->entryperiod;
}else{
// It's acceleration
$acceleration = round($velocity_change,4);
$this->faccel[] = $acceleration;
$this->accels[] = $acceleration;
if ($velocity_change != 0){
$this->timeaccel = $this->timeaccel + $this->entryperiod;
}
}
$this->lastspeedm = $speed;
$this->lasttimestamp = $timestamp;
return array($acceleration,$deceleration);
}
/** Update the Journey object's metadata to include details of what information (if any) was suppressed at ingest
*
*/
private function writeSuppressionMetadata(){
if ($this->suppresslocation){
$this->journey->metadata->suppression[] = 'location';
}
if ($this->suppressspeed){
$this->journey->metadata->suppression[] = 'speed';
}
if ($this->suppresselevation){
$this->journey->metadata->suppression[] = 'elevation';
}
if ($this->suppressdate){
$this->journey->metadata->suppression[] = 'dates';
}
}
/** Generate a track identifier
*
*/
private function genTrackKey($i){
return "journey$i";
}
/** Generate a segment identifier
*
*/
private function genSegKey($i){
return "seg$i";
}
/** Initialise a Segment object
*
*/
private function initSegment($jkey,$segkey){
$this->journey->journeys->$jkey->segments->$segkey = new stdClass();
$this->lasttimestamp = false;
$this->lastpos = false;
$this->entryperiod = 0;
}
/** Initialise a track object
*
*/
private function initTrack($jkey,$trk){
if (!isset($this->journey->journeys)){
$this->journey->journeys = new stdClass();
}
$this->journey->journeys->$jkey = new stdClass();
$this->journey->journeys->$jkey->segments = new stdClass();
$this->journey->journeys->$jkey->stats = new stdClass();
$this->journey->journeys->$jkey->name = (string) $trk;
$this->journey->journeys->$jkey->stats->journeyDuration = 0;
$this->journey->journeys->$jkey->stats->maxacceleration = 0;
$this->journey->journeys->$jkey->stats->maxdeceleration = 0;
$this->journey->journeys->$jkey->stats->minacceleration = 0;
$this->journey->journeys->$jkey->stats->mindeceleration = 0;
$this->journey->journeys->$jkey->stats->avgacceleration = 0;
$this->journey->journeys->$jkey->stats->avgdeceleration = 0;
$this->journey->journeys->$jkey->stats->speedUoM = array();
$this->journey->journeys->$jkey->stats->timeMoving = 0;
$this->journey->journeys->$jkey->stats->timeStationary = 0;
$this->journey->journeys->$jkey->stats->timeAccelerating = 0;
$this->journey->journeys->$jkey->stats->timeDecelerating = 0;
$this->journey->journeys->$jkey->stats->distanceTravelled = 0;
$this->tracks[$jkey]['name'] = $this->journey->journeys->$jkey->name;
$this->tracks[$jkey]['segments'] = array();
// Used by the Acceleration calculation method
$this->lasttimestamp = false;
}
/** Write stats for the current segment
*
*/
private function writeSegmentStats($jkey,$segkey,$times,$x,$uom,$timemoving,$timestationary){
if (!isset($this->journey->journeys->$jkey->segments->$segkey->stats)){
$this->journey->journeys->$jkey->segments->$segkey->stats = new stdClass();
}
if (!$this->suppressspeed){
$modesearch = array_count_values($this->sspeed);
$this->journey->journeys->$jkey->segments->$segkey->stats->avgspeed = round($this->speed/$x,2);
$this->journey->journeys->$jkey->segments->$segkey->stats->modalSpeed = array_search(max($modesearch), $modesearch);
$this->journey->journeys->$jkey->segments->$segkey->stats->minSpeed = min($this->sspeed);
$this->journey->journeys->$jkey->segments->$segkey->stats->maxSpeed = max($this->sspeed);
$this->journey->journeys->$jkey->segments->$segkey->stats->speedUoM = $uom;
}
// Calculate the total distance travelled (feet)
if (!$this->suppresslocation){
$this->journey->journeys->$jkey->segments->$segkey->stats->distanceTravelled = array_sum($this->sdist);
}
if (!$this->suppressdate){
$start = min($times);
$end = max($times);
$duration = $end - $start;
$this->journey->journeys->$jkey->segments->$segkey->stats->start = $start;
$this->journey->journeys->$jkey->segments->$segkey->stats->end = $end;
$this->journey->journeys->$jkey->segments->$segkey->stats->journeyDuration = $duration;
// Increase the track duration by the time of our segment
$this->journey->journeys->$jkey->stats->journeyDuration = $this->journey->journeys->$jkey->stats->journeyDuration + $duration;
$this->trackduration = $this->trackduration + $this->journey->journeys->$jkey->stats->journeyDuration;
// We only need to add the min/max times to the track as we've already sorted the segment
$this->ftimes[] = $this->journey->journeys->$jkey->segments->$segkey->stats->start;
$this->ftimes[] = $this->journey->journeys->$jkey->segments->$segkey->stats->end;
}
if (!$this->suppresselevation){
$this->journey->journeys->$jkey->segments->$segkey->stats->elevation = new stdClass();
$this->journey->journeys->$jkey->segments->$segkey->stats->elevation->max = max($this->seles);
$this->journey->journeys->$jkey->segments->$segkey->stats->elevation->min = min($this->seles);
$this->journey->journeys->$jkey->segments->$segkey->stats->elevation->avgChange = round(array_sum($this->seledevs)/count($this->seledevs),2);
}
// Update the stationary/moving stats
$this->journey->journeys->$jkey->segments->$segkey->stats->timeMoving = $timemoving;
$this->journey->journeys->$jkey->segments->$segkey->stats->timeStationary = $timestationary;
$this->journey->journeys->$jkey->stats->timeMoving = $this->journey->journeys->$jkey->stats->timeMoving + $timemoving;
$this->journey->journeys->$jkey->stats->timeStationary = $this->journey->journeys->$jkey->stats->timeStationary + $timestationary;
$this->journey->stats->timeMoving = $this->journey->stats->timeMoving + $timemoving;
$this->journey->stats->timeStationary = $this->journey->stats->timeStationary + $timestationary;
// Update the accel stats - has to assume you spent the chunk accelerating so may be inaccurate
$this->journey->journeys->$jkey->segments->$segkey->stats->timeAccelerating = $this->timeaccel;
$this->journey->journeys->$jkey->segments->$segkey->stats->timeDecelerating = $this->timedecel;
$this->journey->journeys->$jkey->stats->timeAccelerating = $this->journey->journeys->$jkey->stats->timeAccelerating + $this->timeaccel;
$this->journey->journeys->$jkey->stats->timeDecelerating = $this->journey->journeys->$jkey->stats->timeDecelerating + $this->timedecel;
$this->journey->stats->timeAccelerating = $this->journey->stats->timeAccelerating + $this->timeaccel;
$this->journey->stats->timeDecelerating = $this->journey->stats->timeDecelerating + $this->timedecel;
// Update the indexes
$this->tracks[$jkey]['segments'][$segkey] = $x++;
$this->journey->stats->segments++;
}
/** Write stats for the current track
*
*/
private function writeTrackStats($jkey){
// If speed is suppressed we'll have pushed 1 into the array for each trackpart.
$ptcount = count($this->fspeed);
if (!$this->suppressspeed){
$sumspeed = array_sum($this->fspeed);
$modesearch = array_count_values($this->fspeed);
$this->journeyspeeds = array_merge($this->journeyspeeds,$this->fspeed);
$this->journey->journeys->$jkey->stats->maxSpeed = max($this->fspeed);
$this->journey->journeys->$jkey->stats->minSpeed = min($this->fspeed);
$this->journey->journeys->$jkey->stats->modalSpeed = array_search(max($modesearch), $modesearch);
$this->journey->journeys->$jkey->stats->avgspeed = round($sumspeed/$ptcount,2);
$this->journey->journeys->$jkey->stats->maxacceleration = max($this->faccel);
$this->journey->journeys->$jkey->stats->maxdeceleration = max($this->fdecel);
$this->journey->journeys->$jkey->stats->minacceleration = min($this->faccel);
$this->journey->journeys->$jkey->stats->mindeceleration = min($this->fdecel);
$this->journey->journeys->$jkey->stats->avgacceleration = round(array_sum($this->faccel)/count($this->faccel),2);
$this->journey->journeys->$jkey->stats->avgdeceleration = round(array_sum($this->fdecel)/count($this->fdecel),2);
// Add the calculated max/min speeds to the Journey wide stats
$this->highspeeds[] = $this->journey->journeys->$jkey->stats->maxSpeed;
$this->lowspeeds[] = $this->journey->journeys->$jkey->stats->minSpeed;
}
if (!$this->suppresslocation){
$this->journey->journeys->$jkey->stats->distanceTravelled = array_sum($this->fdist);
}
if (!$this->suppressdate){
// Finalise the track stats
$this->journey->journeys->$jkey->stats->start = min($this->ftimes);
$this->journey->journeys->$jkey->stats->end = max($this->ftimes);
$this->journey->journeys->$jkey->stats->recordedDuration = $this->trackduration;
// Update the object times
$this->totaltimes[] = $this->journey->journeys->$jkey->stats->start;
$this->totaltimes[] = $this->journey->journeys->$jkey->stats->end;
$this->journey->stats->recordedDuration = $this->journey->stats->recordedDuration + $this->trackduration;
}
if (!$this->suppresselevation){
$this->journey->journeys->$jkey->stats->elevation = new stdClass();
$this->journey->journeys->$jkey->stats->elevation->max = max($this->feles);
$this->journey->journeys->$jkey->stats->elevation->min = min($this->feles);
$this->journey->journeys->$jkey->stats->elevation->avgChange = round(array_sum($this->feledevs)/count($this->feledevs),2);
}
// Update the object totals
$this->journey->stats->trackpoints = $this->journey->stats->trackpoints + $ptcount;
$this->journey->stats->tracks++;
}
/** Calculate the distance between two lat/lon points - see GPXIN-6
*
* @arg array - (lat,lon) - the previous position
* @arg array - (lat,lon) - the current position
*
* @return distance travelled (feet)
*/
protected function calculateTravelledDistance($old,$new){
if (!$this->expisenabled('calcDistance')){ // This functionality is currently considered experimental
return 0;
}
// Array mapping (for ease of reference
// lon1 - old[0]
// lon2 - new[0]
// lat1 - old[1]
// lat2 - new [1]
$theta = $old[0] - $new[0];
$dist = acos(sin(deg2rad($old[1])) * sin(deg2rad($new[1])) + cos(deg2rad($old[1])) * cos(deg2rad($new[1])) * cos(deg2rad($theta)));
$dist = rad2deg($dist);
$res = round(($dist * 60 * 1.1515) * 5280,3); // Convert to feet and round to 3 decimal places
return (is_nan($res))? 0 : $res;
}
/** Reset the track stats counter
*
*/
private function resetTrackStats(){
$this->ftimes = array();
$this->fspeed = array();
$this->trackduration = 0;
$this->faccel = array();
$this->fdecel = array();
$this->feles = array();
$this->feledevs = array();
$this->fdist = array();
}
/** Reset the segments stats counter
*
*/
private function resetSegmentStats(){
$this->speed = 0;
$this->sspeed = array();
$this->seles = array();
$this->seledevs = array();
$this->timeaccel = 0;
$this->timedecel = 0;
$this->sdist = array();
}
/** ---- Information Retrieval functions ---- **/
/** Get the Journey object's metadata
*
* @return stdClass
*
*/
public function getMetadata(){
return $this->journey->metadata;
}
/** Get the ID's of any tracks that have been ingested
*
* @return array
*
*/
public function getTrackIDs(){
return array_keys($this->tracks);
}
/** Get ID's and names of any ingested tracks
*
* @return array
*
*/
public function getTrackNames(){
$tracks = array();
foreach ($this->tracks as $k => $v){
$tracks[] = array('id'=>$k,'name'=>$v['name']);
}
return $tracks;
}
/** Get details of segments for a given track
*
* @arg track - the track ID
*
* @return array
*
*/
public function getTrackSegmentNames($track){
return array_keys($this->tracks[$track]['segments']);
}
/** Get the trackpoint ID's
*
* @arg track - the Track ID
* @arg segment - the segment ID
*
*/
public function getTrackPointNames($track,$segment){
return array_keys((array) $this->journey->journeys->$track->segments->$segment->points);
}
/** Get the XML name of a specific Track
*
* @return string
*/
public function getTrackName($jkey){
return $this->journey->journeys->$jkey->name;
}
/** Get the time the original GPX file was created
*
* @return INT - UNIX Epoch
*
*/
public function getGPXTime(){
return $this->journey->created->time;
}
/** Get the timezone used when the JSON object was encoded - previous user may not have set to UTC