-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Route.php
141 lines (116 loc) · 4.13 KB
/
Route.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
<?php
/**
* Created 17/02/2017 18:21
* @author Jakub Dubec <jakub.dubec@gmail.com>
*/
namespace phpGPX\Models;
use phpGPX\Helpers\DistanceCalculator;
use phpGPX\Helpers\ElevationGainLossCalculator;
use phpGPX\Helpers\GeoHelper;
use phpGPX\Helpers\SerializationHelper;
use phpGPX\phpGPX;
/**
* Class Route
* @package phpGPX\Models
*/
class Route extends Collection
{
/**
* A list of route points.
* An original GPX 1.1 attribute.
* @var Point[]
*/
public $points;
/**
* Route constructor.
*/
public function __construct()
{
parent::__construct();
$this->points = [];
}
/**
* Return all points in collection.
* @return Point[]
*/
public function getPoints()
{
/** @var Point[] $points */
$points = [];
$points = array_merge($points, $this->points);
if (phpGPX::$SORT_BY_TIMESTAMP && !empty($points) && $points[0]->time !== null) {
usort($points, array('phpGPX\Helpers\DateTimeHelper', 'comparePointsByTimestamp'));
}
return $points;
}
/**
* Serialize object to array
* @return array
*/
public function toArray()
{
return [
'name' => SerializationHelper::stringOrNull($this->name),
'cmt' => SerializationHelper::stringOrNull($this->comment),
'desc' => SerializationHelper::stringOrNull($this->description),
'src' => SerializationHelper::stringOrNull($this->source),
'link' => SerializationHelper::serialize($this->links),
'number' => SerializationHelper::integerOrNull($this->number),
'type' => SerializationHelper::stringOrNull($this->type),
'extensions' => SerializationHelper::serialize($this->extensions),
'rtep' => SerializationHelper::serialize($this->points),
'stats' => SerializationHelper::serialize($this->stats)
];
}
/**
* Recalculate stats objects.
* @return void
*/
public function recalculateStats()
{
if (empty($this->stats)) {
$this->stats = new Stats();
}
$this->stats->reset();
if (empty($this->points)) {
return;
}
$pointCount = count($this->points);
$firstPoint = &$this->points[0];
$lastPoint = end($this->points);
$this->stats->startedAt = $firstPoint->time;
$this->stats->startedAtCoords = ["lat" => $firstPoint->latitude, "lng" => $firstPoint->longitude];
$this->stats->finishedAt = $lastPoint->time;
$this->stats->finishedAtCoords = ["lat" => $lastPoint->latitude, "lng" => $lastPoint->longitude];
$this->stats->minAltitude = $firstPoint->elevation;
$this->stats->minAltitudeCoords = ["lat" => $firstPoint->latitude, "lng" => $firstPoint->longitude];
list($this->stats->cumulativeElevationGain, $this->stats->cumulativeElevationLoss) =
ElevationGainLossCalculator::calculate($this->getPoints());
$calculator = new DistanceCalculator($this->getPoints());
$this->stats->distance = $calculator->getRawDistance();
$this->stats->realDistance = $calculator->getRealDistance();
for ($p = 0; $p < $pointCount; $p++) {
if ((phpGPX::$IGNORE_ELEVATION_0 === false || $this->points[$p]->elevation > 0) && $this->stats->minAltitude > $this->points[$p]->elevation) {
$this->stats->minAltitude = $this->points[$p]->elevation;
$this->stats->minAltitudeCoords = ["lat" => $this->points[$p]->latitude, "lng" => $this->points[$p]->longitude];
}
if ($this->stats->maxAltitude < $this->points[$p]->elevation) {
$this->stats->maxAltitude = $this->points[$p]->elevation;
$this->stats->maxAltitudeCoords = ["lat" => $this->points[$p]->latitude, "lng" => $this->points[$p]->longitude];
}
if ($this->stats->minAltitude > $this->points[$p]->elevation) {
$this->stats->minAltitude = $this->points[$p]->elevation;
$this->stats->minAltitudeCoords = ["lat" => $this->points[$p]->latitude, "lng" => $this->points[$p]->longitude];
}
}
if (($firstPoint->time instanceof \DateTime) && ($lastPoint->time instanceof \DateTime)) {
$this->stats->duration = $lastPoint->time->getTimestamp() - $firstPoint->time->getTimestamp();
if ($this->stats->duration != 0) {
$this->stats->averageSpeed = $this->stats->distance / $this->stats->duration;
}
if ($this->stats->distance != 0) {
$this->stats->averagePace = $this->stats->duration / ($this->stats->distance / 1000);
}
}
}
}