diff --git a/docs/phpGPX-Models-Stats.md b/docs/phpGPX-Models-Stats.md index fc185b5..8973f30 100644 --- a/docs/phpGPX-Models-Stats.md +++ b/docs/phpGPX-Models-Stats.md @@ -69,6 +69,16 @@ Maximal altitude in meters (m) +* Visibility: **public** + +### $cumulativeElevationGain + + public integer $cumulativeElevationGain = null + +Cumulative positive elevation gain in meters (m) + + + * Visibility: **public** diff --git a/src/phpGPX/Models/Route.php b/src/phpGPX/Models/Route.php index f8bccd2..000c519 100644 --- a/src/phpGPX/Models/Route.php +++ b/src/phpGPX/Models/Route.php @@ -111,6 +111,18 @@ function recalculateStats() $this->stats->distance += $this->points[$p]->difference; $this->points[$p] = $this->stats->distance; } + + if($this->stats->cumulativeElevationGain === null) + { + $lastElevation = $firstPoint->elevation; + $this->stats->cumulativeElevationGain = 0; + } + else + { + $elevationDelta = $this->points[$p]->elevation - $lastElevation; + $this->stats->cumulativeElevationGain += ($elevationDelta > 0) ? $elevationDelta : 0; + $lastElevation = $this->points[$p]->elevation; + } if ($this->stats->minAltitude == null) { @@ -142,4 +154,4 @@ function recalculateStats() } } } -} \ No newline at end of file +} diff --git a/src/phpGPX/Models/Stats.php b/src/phpGPX/Models/Stats.php index 708ee3b..daac348 100644 --- a/src/phpGPX/Models/Stats.php +++ b/src/phpGPX/Models/Stats.php @@ -46,6 +46,12 @@ class Stats implements Summarizable * @var int */ public $maxAltitude = null; + + /** + * Cumulative elevation gain in meters (m) + * @var int + */ + public $cumulativeElevationGain = null; /** * Started time @@ -75,6 +81,7 @@ public function reset() $this->averagePace = null; $this->minAltitude = null; $this->maxAltitude = null; + $this->cumulativeElevationGain = null; $this->startedAt = null; $this->finishedAt = null; } @@ -91,9 +98,10 @@ function toArray() 'avgPace' => (double) $this->averagePace, 'minAltitude' => (double) $this->minAltitude, 'maxAltitude' => (double) $this->maxAltitude, + 'cumulativeElevationGain' => (double) $this->cumulativeElevationGain, 'startedAt' => DateTimeHelper::formatDateTime($this->startedAt, phpGPX::$DATETIME_FORMAT, phpGPX::$DATETIME_TIMEZONE_OUTPUT), 'finishedAt' => DateTimeHelper::formatDateTime($this->finishedAt, phpGPX::$DATETIME_FORMAT, phpGPX::$DATETIME_TIMEZONE_OUTPUT), 'duration' => (double) $this->duration ]; } -} \ No newline at end of file +} diff --git a/src/phpGPX/Models/Track.php b/src/phpGPX/Models/Track.php index cd2c2be..33f1bcb 100644 --- a/src/phpGPX/Models/Track.php +++ b/src/phpGPX/Models/Track.php @@ -118,6 +118,17 @@ function recalculateStats() } $this->stats->distance += $this->segments[$s]->points[$p]->difference; $this->segments[$s]->points[$p]->distance = $this->stats->distance; + + if($this->stats->cumulativeElevationGain === null) + { + $lastElevation = $firstPoint->elevation; + $this->stats->cumulativeElevationGain = 0; + } + else + { + $elevationDelta = $this->segments[$s]->points[$p]->elevation - $lastElevation; + $this->stats->cumulativeElevationGain += ($elevationDelta > 0) ? $elevationDelta : 0; $lastElevation = $this->segments[$s]->points[$p]->elevation; + } } if ($this->stats->minAltitude == null) { @@ -148,4 +159,4 @@ function recalculateStats() } } } -} \ No newline at end of file +}