【问题标题】:How to calculate road network distances between a reference line(or point) and a dataframe of long/lat points with R?如何使用 R 计算参考线(或点)与长/纬点数据帧之间的道路网络距离?
【发布时间】:2019-05-06 23:23:37
【问题描述】:

我想计算参考线(或如果单个点有助于可能的解决方案,则为参考点)和长/纬点数据帧之间的道路网络距离。我有以下数据框:

   Latitude Longitude
1  40.66858 22.88713
2  40.66858 22.88713
3  40.66858 22.88713
4  40.66858 22.88713
5  40.66858 22.88714
6  40.66857 22.88715
7  40.66858 22.88716
8  40.66858 22.88717
9  40.66859 22.88718
10 40.66861 22.88719 

以及以下带有开始/结束坐标的参考线:

22.88600 40.66885
22.88609 40.66880 

(如果我们想要在线中间的单个参考点(而不是整条线),它的坐标是:22.88602844465866,40.66883357487465) 这是绘制点和线后来自谷歌地球的屏幕截图:

我尝试通过以下方式计算每个点与参考线的距离:

dist2Line(points, line, distfun=distHaversine) #from geosphere package

计算的距离(例如,对于第一个点)是以下屏幕截图中带有黄线的距离。想要的那个是红色的 线(路网距离)。我该如何解决这个问题?我想计算所有点的道路网络距离! 先感谢您!

【问题讨论】:

  • 您需要计算从一个点到下一个点的所有单独距离并将它们全部加起来。
  • 在我看来,您正在尝试评估曼哈顿距离而不是哈弗辛距离。后者计算两点之间的最短距离,而前者计算沿直角轴测量的距离。如果是这样,您是否尝试过使用stats::dist() 函数?示例:stats::dist(data.frame(Latitude = c(40.66858,40.66885), Longitude = c(22.88713,22.88600)), method = "manhattan")
  • 注意:使用 geosphere 包时,函数期望第一列是经度而不是纬度。

标签: r distance spatial


【解决方案1】:
library(sp)
library(rgeos)
library(geosphere)

让我们将你的线的中点连接到另一条线:

pt1 <- matrix(c(22.88600, 40.66885), ncol=2)
pt2 <- matrix(c(22.88609, 40.66880), ncol=2)

midpt <- as.data.frame(midPoint(pt1, pt2))

注意:前 4 个线点在您提供的数据中是相同的

read.csv(text="lat,lon
40.66858,22.88713
40.66858,22.88713
40.66858,22.88713
40.66858,22.88713
40.66858,22.88714
40.66857,22.88715
40.66858,22.88716
40.66858,22.88717
40.66859,22.88718
40.66861,22.88719", stringsAsFactors = FALSE) -> l

l <- rbind.data.frame(midpt, l)

使用线上的中点并不完美,因此您还可以使用空间相交操作来找到正确的相交点。

现在,让它成为一个空间对象,并给它一个无聊的longlat“投影”。

l <- SpatialLines(list(Lines(Line(l[,2:1]), "1")), proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

将所说的“投影”转换为有意义的东西(我选择了EPSG:3265,但选择你想要的任何东西,这样你就可以获得真正的距离):

l <- spTransform(l, CRS("+init=epsg:3265"))

从线上获取点:

pts <- as(l, "SpatialPoints")

关注How to calculate geographic distance between two points along a line in R? 获取点之间的距离,您可以从那里完成剩下的工作:

diff(sort(gProject(l, pts, normalized = FALSE)))
##  [1] 372.553928   0.000000   0.000000   0.000000   3.360954   4.581859
##  [7]   4.581860   3.360956   4.581862   7.077129

如果知道如何使用 sf 做到这一点的人也能做到这一点,那就太好了,因为我 couldn't find 相当于 gProject

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2015-11-01
    • 2015-10-24
    • 2023-03-23
    • 2018-07-14
    • 2018-05-21
    相关资源
    最近更新 更多