I want loop though a gpx file and calculate the total ascent and descent. I have a function that can calc the difference in elevation between two sets of lat long points and I've set up simplexml to read & loop through the gpx file trkseg points.
The problem is, that this is not accurate (really not accurate as it is in real).
This two lines will result in different ascent and descent, as it is in the real life:
$total_ascent += ($val - $last_elevation);
$total_descent += ($val - $last_elevation);
Does somebody know, how to calculate more accurate the total ascent and descent of a track?
This is my current code snippet to calculate it ($track_elevations
is an array with elevations of whole track):
if (!empty($track_elevations)) {
$total_ascent = $total_descent = 0
$lowest_elevation = $highest_elevation = $last_elevation = null;
foreach ($track_elevations as &$val) {
if (!is_null($last_elevation)) {
if ($last_elevation < $val) {
$total_ascent += ($val - $last_elevation);
}
elseif ($last_elevation > $val) {
$total_descent += ($last_elevation - $val);
}
}
if (is_null($lowest_elevation) or $lowest_elevation > $val) {
$lowest_elevation = $val;
}
if (is_null($highest_elevation) or $highest_elevation < $val) {
$highest_elevation = $val;
}
$last_elevation = $val;
}
}
Example of $track_elevations
array:
$track_elevations = array(
327.46,
328.27,
329.32,
330.11,
329.46,
329.39,
329.68,
331.04,
333.23,
333.46,
332.97,
332.88,
332.99,
332.75,
332.74,
334.01,
333.62
)
In real, I was riding bike on the flat road. But my snippet of code will calculate, I have ascended and descended couple meters. Maybe I should add there some limitation of precision between two elevations in a row...
What I want to achieve:
I will try to explain it more better - when I ride f.e. 20 km on flat road (almost with no ascent and descent), the total ascent and descent should be close to 0. But when I sum $track_elevations
(like in my snippet of code), I will get in $total_ascent
and $total_descent
f.e. 500 meters... Its because of between each array element in $track_elevations
is difference couple centimeters, but I am still riding on the flat road... And in the total sum it will gather to a large number of meters... Hope now it is more clear.
track_elevations
array? – Insignificance$track_elevations
(like in my snippet of code), I will get in$total_ascent
and$total_descent
f.e. 500 meters... Its because of between each array element in$track_elevations
is difference couple centimeters, but I am still riding on the flat road... And in the total sum it will gather to a large number of meters... Hope now it is more clear. – Famulus