douliexu5623 2015-05-21 09:04
浏览 49

PHP通过GPX循环计算轨道的总上升/下降

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.

  • 写回答

3条回答 默认 最新

  • doulanli6146 2015-05-26 11:22
    关注

    The problem is that if you calculate $lastHeight - $track_elevations[$i] for ascentet you get values lower than 0 and thats why you get high differences between $total_asc and $total_desc.

    <?php
    $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
    );
    
    $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 ."
    ";
    echo $total_descent . "
    
    --
    ";
    echo $total_ascent  - $total_descent . "
    ";
    

    --

    $ php -f gpx.php
    8.1
    1.94
    
    --
    6.16
    

    -- manual calculates values:

    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    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?