首页 > 解决方案 > 从 A 点开始到 B 点后 5 分钟内插位置

问题描述

下面是使用 R 中的包查找从“纽约世贸中心一号”到“纽约麦迪逊广场公园”的路线、旅行时间和旅行距离的示例osrm。(我从 R 中的 Road Routing中学到了它)。这里的旅行时间是10.37分钟。

问:如何在 5 分钟后进行插值并找到位置。

在此处输入图像描述

library(sf)
library(dplyr)
library(tidygeocoder)
library(osrm)

# 1. One World Trade Center, NYC
# 2. Madison Square Park, NYC
adresses <- c("285 Fulton St, New York, NY 10007", 
            "11 Madison Ave, New York, NY 10010")

# geocode the two addresses & transform to {sf} data structure
data <- tidygeocoder::geo(adresses, method = "osm") %>% 
  st_as_sf(coords = c("long", "lat"), crs = 4326)

osroute <- osrm::osrmRoute(loc = data,
                           returnclass = "sf")

summary(osroute)



library(leaflet)

leaflet(data = data) %>% 
  addProviderTiles("CartoDB.Positron") %>% 
  addMarkers(label = ~address) %>% 
  addPolylines(data = osroute,
               label = "OSRM engine",
               color = "red")

标签: rsfsposrm

解决方案


使用该osrm::osrmIsochrone()函数找到五分钟行驶距离多边形,然后找到路线与多边形相交的点。

它看起来像在哈德逊和瓦里克之间的克拉克森街。

library(sf)
library(dplyr)
library(tidygeocoder)
library(osrm)

# 1. One World Trade Center, NYC
# 2. Madison Square Park, NYC
adresses <- c("285 Fulton St, New York, NY 10007", 
              "11 Madison Ave, New York, NY 10010")

# geocode the two addresses & transform to {sf} data structure
data <- tidygeocoder::geo(adresses, method = "osm") %>% 
  st_as_sf(coords = c("long", "lat"), crs = 4326)

# get route from 285 fulton to 11 madison
osroute <- osrmRoute(src = data[1,], dst = data[2,], returnclass = 'sf')

# five minute isochrone from 285 fulton
five_min_isochrone <- osrmIsochrone(data[1,], breaks = 5, returnclass = 'sf')

# isochrone has to be cast to MULTILINESTRING to find intersection as a point
intersection <- five_min_isochrone %>% 
                  st_cast('MULTILINESTRING') %>%
                  st_intersection(osroute)


library(leaflet)

leaflet(data = data) %>% 
  addProviderTiles("CartoDB.Positron") %>% 
  addMarkers(label = ~address) %>% 
  addPolylines(data = osroute,
               label = "OSRM engine",
               color = "red") %>%
  addPolygons(data = five_min_isochrone) %>%
  addMarkers(data = intersection, 
             label = '5 minute distance')

在此处输入图像描述


推荐阅读