Search code examples
rr-sfr-sposrm

Interpolate position 5 minutes after starting from point A to B


below is an example of finding route, travel time and travel distance from 'One World Trade Center, NYC' to 'Madison Square Park, NYC' using osrm package in R. (I learnt it from Road Routing in R). The travel time here is 10.37 minutes.

Q. How can I interpolate and find location after 5 minutes.

enter image description here

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")


Solution

  • Use the osrm::osrmIsochrone() function to find the five minute travel distance polygon, and then find the point that the route intersects the polygon.

    It looks like its on Clarkson Street between Hudson & Varick.

    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')
    
    

    enter image description here