我想通过gpx文件循环并计算总上升和下降.我有一个函数可以计算两组lat长点之间的高程差异,我设置了simplexml来读取&循环遍历gpx文件trkseg点.
问题是,这不准确(实际上并不准确).
这两条线将导致不同的上升和下降,就像在现实生活中一样:
$total_ascent += ($val - $last_elevation); $total_descent += ($val - $last_elevation);
有人知道,如何更精确地计算轨道的总上升和下降?
这是我当前的代码片段来计算它($track_elevations是一个包含整个轨道高程的数组):
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; } }
$track_elevations数组的示例:
$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 )
实际上,我在平路上骑自行车.但是我的代码片段会计算,我已经上升并下降了几米.也许我应该在两个高程之间添加一些精度限制……
我想要实现的目标:
我会尝试更好地解释它 – 当我骑车时.在平坦的道路上20公里(几乎没有上升和下降),总上升和下降应该接近0.但是当我总结$track_elevations(就像在我的代码片段中)时,我将获得$total_ascent和$total_descent f.e. 500米…因为$track_elevations中的每个数组元素之间是差异几厘米,但我仍然骑在平坦的道路上…而在总和中它会聚集到大量的米…希望现在它更清楚了.
问题是如果你为ascentet计算$lastHeight – $track_elevations [$i],你会得到低于0的值,这就是为什么你得到$total_asc和$total_desc之间的高差异的原因.
原文链接:https://www.f2er.com/php/137084.html<?PHP $track_elevations = array( 327.46,333.62 ); $lastHeight = $track_elevations[0]; $total_ascent = 0; $total_descent = 0; for ($i=1;$i<count($track_elevations);$i++) { if ($track_elevations[$i] > $lastHeight) { $total_ascent += -1 * ($lastHeight - $track_elevations[$i]); } else { $total_descent += ($lastHeight - $track_elevations[$i]); } $lastHeight = $track_elevations[$i]; } echo $total_ascent ."\n"; echo $total_descent . "\n\n--\n"; echo $total_ascent - $total_descent . "\n";
–
$PHP -f gpx.PHP 8.1 1.94 -- 6.16
–
手动计算值:
Ascent: 0.81 1.05 0.79 0.29 1.36 2.19 0.23 0.11 1.27 --- 8.1 Descent: 0.65 0.07 0.49 0.09 0.24 0.01 0.39 --- 1.94