Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/phpGPX/Models/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ public function recalculateStats()
$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];
Expand All @@ -111,6 +109,13 @@ public function recalculateStats()
$this->stats->realDistance = $calculator->getRealDistance();

for ($p = 0; $p < $pointCount; $p++) {
if ($this->points[$p]->time !== null) {
if ($this->stats->startedAt === null) {
$this->stats->startedAt = $this->points[$p]->time;
}
$this->stats->finishedAt = $this->points[$p]->time;
}

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];
Expand Down
9 changes: 7 additions & 2 deletions src/phpGPX/Models/Segment.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ public function recalculateStats()
$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];
Expand All @@ -106,6 +104,13 @@ public function recalculateStats()
$this->stats->realDistance = $calculator->getRealDistance();

for ($i = 0; $i < $count; $i++) {
if ($this->points[$i]->time !== null) {
if ($this->stats->startedAt === null) {
$this->stats->startedAt = $this->points[$i]->time;
}
$this->stats->finishedAt = $this->points[$i]->time;
}

if ($this->stats->maxAltitude < $this->points[$i]->elevation) {
$this->stats->maxAltitude = $this->points[$i]->elevation;
$this->stats->maxAltitudeCoords = ["lat" => $this->points[$i]->latitude, "lng" => $this->points[$i]->longitude];
Expand Down
9 changes: 7 additions & 2 deletions src/phpGPX/Models/Track.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,21 @@ public function recalculateStats()
$lastSegment = end($this->segments);
$lastPoint = end(end($this->segments)->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];

for ($s = 0; $s < $segmentsCount; $s++) {
$this->segments[$s]->recalculateStats();

if ($this->stats->startedAt === null) {
$this->stats->startedAt = $this->segments[$s]->stats->startedAt;
}
if ($this->segments[$s]->stats->finishedAt !== null) {
$this->stats->finishedAt = $this->segments[$s]->stats->finishedAt;
}

$this->stats->cumulativeElevationGain += $this->segments[$s]->stats->cumulativeElevationGain;
$this->stats->cumulativeElevationLoss += $this->segments[$s]->stats->cumulativeElevationLoss;

Expand Down
115 changes: 115 additions & 0 deletions tests/StatsTimestampsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace phpGPX\Tests;

use phpGPX\Models\Point;
use phpGPX\Models\Route;
use phpGPX\Models\Segment;
use phpGPX\Models\Track;
use phpGPX\phpGPX;
use PHPUnit\Framework\TestCase;

class StatsTimestampsTest extends TestCase
{

/**
* @dataProvider timestampCases
*/
public function testAvailableTimestamps($class, array $times, $start, $finish)
{
$points = [];
foreach ($times as $index => $time) {
$point = new Point($class === Route::class ? Point::ROUTEPOINT : Point::TRACKPOINT);
$point->latitude = 48 + $index / 1000;
$point->longitude = 17;
$point->time = $time === null ? null : new \DateTime($time);
$points[] = $point;
}

$collection = new $class();
if ($collection instanceof Track) {
$segment = new Segment();
$segment->points = $points;
$collection->segments[] = $segment;
} else {
$collection->points = $points;
}
$collection->recalculateStats();

$this->assertSame($start === null ? null : $points[$start]->time, $collection->stats->startedAt);
$this->assertSame($finish === null ? null : $points[$finish]->time, $collection->stats->finishedAt);
$this->assertSame($points, $collection->getPoints());
$this->assertSame([
"lat" => $points[0]->latitude,
"lng" => $points[0]->longitude,
], $collection->stats->startedAtCoords);
$this->assertSame([
"lat" => end($points)->latitude,
"lng" => end($points)->longitude,
], $collection->stats->finishedAtCoords);

// A partial time range does not measure the duration of the whole path.
if ($times[0] === null || end($times) === null) {
$this->assertNull($collection->stats->duration);
$this->assertNull($collection->stats->averageSpeed);
$this->assertNull($collection->stats->averagePace);
}

foreach ($points as $point) {
$point->time = null;
}
$collection->recalculateStats();
$this->assertNull($collection->stats->startedAt);
$this->assertNull($collection->stats->finishedAt);
}

public function timestampCases()
{
$first = '2020-01-02T10:00:00Z';
$last = '2020-01-02T10:05:00Z';
$cases = [
'untimed start' => [[null, null, $first, $last], 2, 3],
'untimed finish' => [[$first, $last, null, null], 0, 1],
'untimed boundaries and gap' => [[null, $first, null, $last, null], 1, 3],
'one timed point' => [[null, $first, null], 1, 1],
'no timestamps' => [[null, null], null, null],
'fully timed' => [[$first, $last], 0, 1],
'recorded order' => [[null, $last, $first, null], 1, 2],
];

foreach ([Route::class, Segment::class, Track::class] as $class) {
foreach ($cases as $name => $case) {
yield $class . ': ' . $name => [$class, $case[0], $case[1], $case[2]];
}
}
}

public function testLoadedTrackSkipsUntimedSegments()
{
$file = phpGPX::parse('<gpx version="1.1" creator="timestamp-test">
<trk>
<trkseg><trkpt lat="48" lon="17"/></trkseg>
<trkseg>
<trkpt lat="48.001" lon="17"><time>2020-01-02T10:00:00Z</time></trkpt>
<trkpt lat="48.002" lon="17"/>
</trkseg>
<trkseg>
<trkpt lat="48.003" lon="17"/>
<trkpt lat="48.004" lon="17"><time>2020-01-02T10:05:00Z</time></trkpt>
</trkseg>
<trkseg><trkpt lat="48.005" lon="17"/></trkseg>
</trk>
</gpx>');
$track = $file->tracks[0];
$this->assertEquals(new \DateTime('2020-01-02T10:00:00Z'), $track->stats->startedAt);
$this->assertEquals(new \DateTime('2020-01-02T10:05:00Z'), $track->stats->finishedAt);
$this->assertCount(4, $track->segments);
$this->assertCount(6, $track->getPoints());
$this->assertSame($track->segments[1]->stats->startedAt, $track->stats->startedAt);
$this->assertSame($track->segments[2]->stats->finishedAt, $track->stats->finishedAt);

$roundTrip = phpGPX::parse($file->toXML()->saveXML());
$this->assertEquals($track->stats->startedAt, $roundTrip->tracks[0]->stats->startedAt);
$this->assertEquals($track->stats->finishedAt, $roundTrip->tracks[0]->stats->finishedAt);
}
}
Loading