【问题标题】:Getting the point on a road that nearest to a giving point in google maps在最接近谷歌地图给定点的道路上获取点
【发布时间】:2016-12-21 03:02:50
【问题描述】:

如果我有 2 分 第一个“29.98671,31.21431”

第二点“29.97864,31.17557”

我把它们放在谷歌地图上,得到他们两个之间的路线,我还有一个 "29.987201, 31.188547" ,想得到道路上离 "29.987201, 31.188547" 最近的点。

是否可以使用 R 来执行此操作?请帮助。

【问题讨论】:

  • 看看here从A到B的路线。第二部分类似 - 看看osrm nearest函数。
  • @Christoph 我正在尝试从问题中获取任何信息,但对不起,我无法,我安装了 osrm 包但找不到最近的函数,但我找到了osrmRoute,它给出了我是道路点的坐标,但不能使用它,因为第一列是comm_id 不知道如何使用我的点得到它,这种方式不会回答我的问题,但会解决一半,谢谢你告诉我但是还有其他方法吗?

标签: r


【解决方案1】:

1) 获取两点之间的路线。

library(ggmap)
# output = 'all' so we get the polyline of the path along the road
my_route <- route(from = "29.98671,31.21431", 
                  to = "29.97864,31.17557",
                  structure = "route", 
                  output = "all")
my_polyline <- my_route$routes[[1]]$legs[[1]]$steps[[1]]$polyline$points

2) 使用此链接问题中的函数将折线解码为一系列点

How to decode encoded polylines from OSRM and plotting route geometry?

# DecodeLineR <- function(encoded) {... see linked question ...}
route_points <- DecodeLineR(my_polyline)

3) 绘制所有路线点以及我们的新点

new_point <- data.frame(lat=29.987201, lng=31.188547)

ggplot(route_points, aes(x=lng, y=lat)) + 
  geom_point(shape=21, alpha=0.5) +
  geom_point(data = new_point, color = 'red') +
  coord_quickmap() +
  theme_linedraw()

4) 找出最接近新点的“路线点”

# get each distance in miles (great circle distance in miles)
library(fields)
route_points$distance_to_new <- t(rdist.earth(new_point, route_points))
# it's this one:
route_points[which.min(route_points$distance_to_new), ]

答案:折线上的第 76 个点最近,在 ~0.19 英里外

        lat      lng distance_to_new
76 29.98688 31.18853      0.01903183

【讨论】:

  • 天哪,太棒了,太棒了:D,非常感谢
猜你喜欢
  • 2012-02-13
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-24
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多