计算数据框中两个长纬度坐标之间的距离 [英] Calculate distance between two long lat coordinates in a dataframe

查看:44
本文介绍了计算数据框中两个长纬度坐标之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算几个GPS点之间的距离.我尝试过

I want to calculate the distance between several GPS points. I tried

distm(c(lon1,lat1), c(lon2,lat2), fun = distHaversine)

仅适用于一点,但不适用于我的数据框中的列.

which worked for one point, but not for the columns in my data frame.

所以我按照这里的建议尝试了:

So I tried as recommended here:

计算2个纬度长点之间的距离

但是对于这两种计算,我确实得到了不同的结果:

But I do get different results for these two calculations:

df <- read.table(sep=",", col.names=c("lat1", "lon1", "lat2", "lon2"),text="
7.348687,53.36575,7.348940,53.36507 
7.348940, 53.36507,7.350939,53.36484")


# as recommended in the link above
distHaversine(df[,2:1], df[,4:3])

[1]  80.18433 223.97181

# with distm
distm(c(7.348687,53.36575), c(7.348940,53.36507), fun = distHaversine)

         [,1]
[1,] 77.54033

distm(c(7.348940, 53.36507), c(7.350939,53.36484), fun = distHaversine)

         [,1]
 [1,] 135.2317

那么如何计算数据帧列中两个GPS点之间的正确距离(即distm(c(lon1,lat1),c(lon2,lat2),fun = distHaversine))?我仔细检查了一下距离,以至于我知道以这种方式得到了正确的距离.

So how can I calculate the correct distances (which is distm(c(lon1,lat1), c(lon2,lat2), fun = distHaversine)) between two GPS points in the columns of my data frame? I double-checked with so distances that I know I get the right distances this way.

提前谢谢.

推荐答案

鉴于您要存储在新列中的输出是这样的:

Given that the output you want to store in the new column is this:

77.54033 135.23165

尝试一下

df$distance<-distHaversine(df[,1:2], df[,3:4])

哪个应该回来

> df
      lat1     lon1     lat2     lon2  distance
1 7.348687 53.36575 7.348940 53.36507  77.54033
2 7.348940 53.36507 7.350939 53.36484 135.23165

这篇关于计算数据框中两个长纬度坐标之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆