GPX 文件中点之间的距离变得太大

Distance between points in GPX file becomes too large

我想根据 GPS 轨迹分析行进的距离,但是当我计算距离时,结果总是太大。

我使用 python 制作一个 csv 文件,其中包含轨道中所有点的纬度和经度,然后我用 R 对其进行分析。数据框如下所示:

|      lat|      lon|   lat.p1|   lon.p1| dist_to_prev|
|--------:|--------:|--------:|--------:|------------:|
| 60.62061| 15.66640| 60.62045| 15.66660|    28.103099|
| 60.62045| 15.66660| 60.62037| 15.66662|     8.859034|
| 60.62037| 15.66662| 60.62026| 15.66636|    31.252373|
| 60.62026| 15.66636| 60.62018| 15.66636|     8.574722|
| 60.62018| 15.66636| 60.62010| 15.66650|    17.787905|
| 60.62001| 15.66672| 60.61996| 15.66684|    14.393267|
| 60.61996| 15.66684| 60.61989| 15.66685|     7.584996|
...

我可以post这里的整个数据框以实现可重现性,它只有 59 行,但我不确定 post 在这里处理大块数据的礼节?让我知道如何最好地分享它。

lat.next 和 lon.next 只是下一行的纬度和经度。 dist_to_prev 是用来自 geosphere 的 distm() 计算的:

library(geosphere)
library(dplyr)

df$dist_to_prev <- apply(df, 1 , FUN = function (row) { 
   distm(c(as.numeric(row["lat"]), as.numeric(row["lon"])), 
         c(as.numeric(row["lat.p1"]), as.numeric(row["lon.p1"])),
   fun = distHaversine)})

df %>% filter(dist_to_prev != "NA") %>% summarise(sum(dist_to_prev))

# A tibble: 1 x 1
`sum(dist_to_prev)`
            <dbl>
1           1266.

我以 Trailforks and if you look at their track description it should be 787m 中的这条赛道为例,而不是我得到的 1266m。这不是该曲目独有的,而是我看过的所有曲目所独有的。当我这样做时,它们都出来了 30-50% 太长了。

可能的原因之一是 lats/lons 只有 5 位小数。 csv 中有 6 位小数,但在 Rstudio 中打开它时我只能看到 5 位。我在想这只是格式化以使其更易于阅读并且 "whole" 数字在那里但也许不存在? lat/lons 的类型为:double。

为什么我的距离比我从中获取 gpx 文件的网站上显示的距离大得多?

上面的代码有几个问题。函数 distHaversine 是一个向量化函数,因此您可以避免循环/应用语句。这将显着提高性能。

最重要的是 geosphere 包的第一个坐标是 经度 而不是纬度。

df<- read.table(header =TRUE, text=" lat      lon   lat.p1   lon.p1
60.62061 15.66640 60.62045 15.66660
60.62045 15.66660 60.62037 15.66662
60.62037 15.66662 60.62026 15.66636
60.62026 15.66636 60.62018 15.66636
60.62018 15.66636 60.62010 15.66650
60.62001 15.66672 60.61996 15.66684
60.61996 15.66684 60.61989 15.66685")


library(geosphere)

#Lat is first column (incorrect)
distHaversine(df[,c("lat", "lon")], df[,c("lat.p1", "lon.p1")])
#incorrect
#[1] 28.103099  8.859034 31.252373  8.574722 17.787905 14.393267  7.584996

#Longitude is first (correct)
distHaversine(df[,c("lon", "lat")], df[,c("lon.p1", "lat.p1")])
#correct result.
#[1] 20.893456  8.972291 18.750046  8.905559 11.737448  8.598240  7.811479