我有两个点A和B,它们有各自的经纬度。我也有从a点到B点所花费的时间,假设从a点到B点需要1小时,假设司机直接到B点并且在旅途中保持相同的速度。0.6小时后,我想知道司机当前的位置(通过经纬度)。在geosphere
包或其他包中是否有任何功能允许我这样做?由于
我认为你的问题更好,更简洁的表达是"我如何找到两个位置之间的大圆路线(定义为纬度/长坐标),然后找到沿该路线任意百分比的点的坐标?"
首先,让我们做一对任意的位置,叫做a和b:
df <- data.frame(locations = c("a","b"),
lon =runif(2,min = -180, max = 180),
lat = runif(2,min = -90, max = 90))
现在,我们看看它们之间的大圆路线。我们不需要路线本身,只需要整个路线的距离和初始方向。
require(geosphere)
# get the distance of a great circle route between these points
track.dist = distHaversine(p1 = df[1,c("lon","lat")],
p2 = df[2,c("lon","lat")])
然后得到初始方位,我们一会儿会用到它:
track.init.bearing = bearing(p1 = df[1,c("lon","lat")],
p2 = df[2,c("lon","lat")])
下一步是找出我们在经过的路线的任意分数处的位置:
# figure out where we are at an arbitrary time
current.location.fraction = runif(1,min = 0, max = 1)
# get the distance
current.location.dist = current.location.fraction * track.dist
current.location = as.data.frame(destPoint(p = df[1,c("lon","lat")],
b = track.init.bearing,
d = current.location.dist))
最后一步是检查我们是沿着路线的距离的正确分数:
check.dist = distHaversine(p1 = df[1,c("lon","lat")],
p2 = c(current.location$lon,
current.location$lat))
print(current.location.fraction)
print(check.dist / track.dist)
在我的测试中,这最后两个数字通常相差在1%以内,这表明这并不是太糟糕。
因此,您可以直接从current.location
数据帧中提取结果。