Skip to main content
Layout: breaking the code into calculations and plotting.
Source Link
M--
  • 33.6k
  • 12
  • 74
  • 115
  BE <- raster::getData("GADM", country = "BEL", level = 1)
  Bruxelles <- BE[BE$NAME_1 == "Bruxelles", ]

  df_grid <- makegrid(Bruxelles, cellsize = 0.02) %>% 
        SpatialPoints() %>%
        ## I convert the SpatialPoints object into a simple data.frame 
        as.data.frame() %>% 
        ## create a unique id for each point in the data.frame
        rownames_to_column() %>% 
        ## rename variables of the data.frame with more explanatory names.
        rename(id = rowname, lat = x2, lon = x1) 

 ## I point osrm.server to the OpenStreet docker running in my Linux machine. ... 
 ### ... Do not run this if you are getting your data from OpenStreet public servers.
 options(osrm.server = "http://127.0.0.1:5000/") 

 ## I obtain a list with distances (Origin Destination Matrix in ...
 ### ... minutes, origins and destinations)
 Distance_Tables <- osrmTable(loc = df_grid) 

 OD_Matrix <- Distance_Tables$durations %>% ## subset the previous list
                ## convert the Origin Destination Matrix into a tibble
                as_data_frame() %>%  
                rownames_to_column() %>% 
                ## make sure we have an id column for the OD tibble
                rename(origin_id = rowname) %>% 
                ## transform the tibble into long/tidy format
                gather(key = destination_id, value = distance_time, -origin_id) %>% 
                left_join(df_grid, by = c("origin_id" = "id")) %>% 
                ## set origin coordinates
                rename(origin_lon = lon, origin_lat = lat) %>% 
                left_join(df_grid, by = c("destination_id" = "id")) %>% 
                ## set destination coordinates
                rename(destination_lat = lat, destination_lon = lon) 
 
 

 ## Obtain a nice looking road map of Brussels
 Brux_map <- get_map(location = "bruxelles, belgique", 
                     zoom = 11, 
                     source = "google", 
                     maptype = "roadmap")

 ggmap(Brux_map) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
              data = OD_Matrix %>% 
                ## Here I selected point_id 42 as the desired target, ...
                ## ... just because it is not far from the City Center.
                filter(destination_id == 42), 
                size = 0.5) + 
   ## Draw a diamond around point_id 42                                      
   geom_point(aes(x = origin_lon, y = origin_lat), 
              data = OD_Matrix %>% 
                filter(destination_id == 42, origin_id == 42),
              shape = 5, size = 3) +  
   ## Countour marking a distance of up to 8 minutes
   geom_convexhull(alpha = 0.2, 
                   fill = "blue", 
                   colour = "blue",
                   data = OD_Matrix %>% 
                            filter(destination_id == 42, 
                            distance_time <= 8), 
                   aes(x = origin_lon, y = origin_lat)) + 
   ## Countour marking a distance of up to 16 minutes
   geom_convexhull(alpha = 0.2, 
                   fill = "red",
                   colour = "red",
                   data = OD_Matrix %>% 
                            filter(destination_id == 42, 
                                   distance_time <= 15), 
                   aes(x = origin_lon, y = origin_lat))
  BE <- raster::getData("GADM", country = "BEL", level = 1)
  Bruxelles <- BE[BE$NAME_1 == "Bruxelles", ]

  df_grid <- makegrid(Bruxelles, cellsize = 0.02) %>% 
        SpatialPoints() %>%
        ## I convert the SpatialPoints object into a simple data.frame 
        as.data.frame() %>% 
        ## create a unique id for each point in the data.frame
        rownames_to_column() %>% 
        ## rename variables of the data.frame with more explanatory names.
        rename(id = rowname, lat = x2, lon = x1) 

 ## I point osrm.server to the OpenStreet docker running in my Linux machine. ... 
 ### ... Do not run this if you are getting your data from OpenStreet public servers.
 options(osrm.server = "http://127.0.0.1:5000/") 

 ## I obtain a list with distances (Origin Destination Matrix in ...
 ### ... minutes, origins and destinations)
 Distance_Tables <- osrmTable(loc = df_grid) 

 OD_Matrix <- Distance_Tables$durations %>% ## subset the previous list
                ## convert the Origin Destination Matrix into a tibble
                as_data_frame() %>%  
                rownames_to_column() %>% 
                ## make sure we have an id column for the OD tibble
                rename(origin_id = rowname) %>% 
                ## transform the tibble into long/tidy format
                gather(key = destination_id, value = distance_time, -origin_id) %>% 
                left_join(df_grid, by = c("origin_id" = "id")) %>% 
                ## set origin coordinates
                rename(origin_lon = lon, origin_lat = lat) %>% 
                left_join(df_grid, by = c("destination_id" = "id")) %>% 
                ## set destination coordinates
                rename(destination_lat = lat, destination_lon = lon) 
 
 ## Obtain a nice looking road map of Brussels
 Brux_map <- get_map(location = "bruxelles, belgique", 
                     zoom = 11, 
                     source = "google", 
                     maptype = "roadmap")

 ggmap(Brux_map) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
              data = OD_Matrix %>% 
                ## Here I selected point_id 42 as the desired target, ...
                ## ... just because it is not far from the City Center.
                filter(destination_id == 42), 
                size = 0.5) + 
   ## Draw a diamond around point_id 42                                      
   geom_point(aes(x = origin_lon, y = origin_lat), 
              data = OD_Matrix %>% 
                filter(destination_id == 42, origin_id == 42),
              shape = 5, size = 3) +  
   ## Countour marking a distance of up to 8 minutes
   geom_convexhull(alpha = 0.2, 
                   fill = "blue", 
                   colour = "blue",
                   data = OD_Matrix %>% 
                            filter(destination_id == 42, 
                            distance_time <= 8), 
                   aes(x = origin_lon, y = origin_lat)) + 
   ## Countour marking a distance of up to 16 minutes
   geom_convexhull(alpha = 0.2, 
                   fill = "red",
                   colour = "red",
                   data = OD_Matrix %>% 
                            filter(destination_id == 42, 
                                   distance_time <= 15), 
                   aes(x = origin_lon, y = origin_lat))
  BE <- raster::getData("GADM", country = "BEL", level = 1)
  Bruxelles <- BE[BE$NAME_1 == "Bruxelles", ]

  df_grid <- makegrid(Bruxelles, cellsize = 0.02) %>% 
        SpatialPoints() %>%
        ## I convert the SpatialPoints object into a simple data.frame 
        as.data.frame() %>% 
        ## create a unique id for each point in the data.frame
        rownames_to_column() %>% 
        ## rename variables of the data.frame with more explanatory names.
        rename(id = rowname, lat = x2, lon = x1) 

 ## I point osrm.server to the OpenStreet docker running in my Linux machine. ... 
 ### ... Do not run this if you are getting your data from OpenStreet public servers.
 options(osrm.server = "http://127.0.0.1:5000/") 

 ## I obtain a list with distances (Origin Destination Matrix in ...
 ### ... minutes, origins and destinations)
 Distance_Tables <- osrmTable(loc = df_grid) 

 OD_Matrix <- Distance_Tables$durations %>% ## subset the previous list
                ## convert the Origin Destination Matrix into a tibble
                as_data_frame() %>%  
                rownames_to_column() %>% 
                ## make sure we have an id column for the OD tibble
                rename(origin_id = rowname) %>% 
                ## transform the tibble into long/tidy format
                gather(key = destination_id, value = distance_time, -origin_id) %>% 
                left_join(df_grid, by = c("origin_id" = "id")) %>% 
                ## set origin coordinates
                rename(origin_lon = lon, origin_lat = lat) %>% 
                left_join(df_grid, by = c("destination_id" = "id")) %>% 
                ## set destination coordinates
                rename(destination_lat = lat, destination_lon = lon) 
 

 ## Obtain a nice looking road map of Brussels
 Brux_map <- get_map(location = "bruxelles, belgique", 
                     zoom = 11, 
                     source = "google", 
                     maptype = "roadmap")

 ggmap(Brux_map) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
              data = OD_Matrix %>% 
                ## Here I selected point_id 42 as the desired target, ...
                ## ... just because it is not far from the City Center.
                filter(destination_id == 42), 
                size = 0.5) + 
   ## Draw a diamond around point_id 42                                      
   geom_point(aes(x = origin_lon, y = origin_lat), 
              data = OD_Matrix %>% 
                filter(destination_id == 42, origin_id == 42),
              shape = 5, size = 3) +  
   ## Countour marking a distance of up to 8 minutes
   geom_convexhull(alpha = 0.2, 
                   fill = "blue", 
                   colour = "blue",
                   data = OD_Matrix %>% 
                            filter(destination_id == 42, 
                            distance_time <= 8), 
                   aes(x = origin_lon, y = origin_lat)) + 
   ## Countour marking a distance of up to 16 minutes
   geom_convexhull(alpha = 0.2, 
                   fill = "red",
                   colour = "red",
                   data = OD_Matrix %>% 
                            filter(destination_id == 42, 
                                   distance_time <= 15), 
                   aes(x = origin_lon, y = origin_lat))
Acronym capitalization; grammar; noise reduction; layout.
Source Link
M--
  • 33.6k
  • 12
  • 74
  • 115

This answer is based on obtaining an origin-destination matrix between a grid of (roughly) equally distant points. This is a computer intensive operation not only because it requires a good number of apiAPI calls to mapping services, but also because the servers must calculate a matrix for each call. The number of required calls grows exponentially along the number of points in the grid.

To tackle this problem, I would suggest that you consider running on your local machine or on a local server a mapping server. Project OSRM offers a relatively simple, free, and open-source solution, enabling you to run an OpenStreetMap server into a Linux docker (https://github.com/Project-OSRM/osrm-backend). Having your own local mapping server will allow you to make as many API calls as you desire. R's osrm package allows you to interact with OpenStreetMaps' apis. includingAPIs, Including those placed to a local server.

  BE <- raster::getData("GADM", country = "BEL", level = 1)
  Bruxelles <- BE[BE$NAME_1 == "Bruxelles", ]

  df_grid <- makegrid(Bruxelles, cellsize = 0.02) %>% 
        SpatialPoints() %>% 
        as.data.frame() %>% ## I convert the SpatialPoints object into a simple data.frame 
        rownames_to_columnas.data.frame() %>% 
        ## create a unique id for each point in the data.frame
        renamerownames_to_column(id) =%>% rowname, 
 lat = x2, lon = x1) # ## rename variables of the data.frame with more explanatory names.
 
 options       rename(osrm.serverid = "http://127.0.0.1:5000/"rowname, lat = x2, lon = x1) 

 ## I point osrm.server to the OpenStreet docker running in my Linux machine. ... 
 ### ... Do not run this if you are getting your data from OpenStreet public servers.
 
 Distance_Tables <- osrmTableoptions(locosrm.server = df_grid"http://127.0.0.1:5000/")  

 ## I obtain a list with distances (Origin Destination Matrix in ...
 ### ... minutes, origins and destinations)
 Distance_Tables <- osrmTable(loc = df_grid) 

 OD_Matrix <- Distance_Tables$durations %>% ## Subsetsubset the previous list 
 and 
    as_data_frame() %>%          ## ...convert the Origin Destination Matrix into a tibble
   rownames_to_column             as_data_frame() %>%  
   rename(origin_id = rowname           rownames_to_column() %>% 
                ## make sure we have an id column for the OD tibble
                rename(origin_id = rowname) %>% 
                ## transform the tibble into long/tidy format
                gather(key = destination_id, value = distance_time, -origin_id) %>% # 
 transform the tibble into long/tidy format
          left_join(df_grid, by = c("origin_id" = "id")) %>% 
                ## set origin coordinates
                rename(origin_lon = lon, origin_lat = lat) %>% ## 
 set origin coordinates
             left_join(df_grid, by = c("destination_id" = "id")) %>% 
                ## set destination coordinates
                rename(destination_lat = lat, destination_lon = lon) ## set destination coordinates

 ## Obtain a nice looking road map of Brussels
 
 Brux_map <- get_map(location = "bruxelles, belgique", 
                     zoom = 11, 
                     source = "google", 
                     maptype = "roadmap")

 ggmap(Brux_map) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
              data = OD_Matrix %>% 
                filter(destination_id == 42), ## Here I selected point_id 42 as the desired target, ...
                ## ... just because it is not far from the City Center.
                filter(destination_id == 42), 
                size = 0.5) + 
   ## Draw a diamond around point_id 42                                      
   geom_point(aes(x = origin_lon, y = origin_lat), 
              data = OD_Matrix %>% 
                filter(destination_id == 42, origin_id == 42),
              shape = 5, size = 3) +  ## 
 Draw a diamond## aroundCountour point_idmarking 42a distance of up to 8 minutes
   geom_convexhull(alpha = 0.2,  
                   fill = "blue",  
    
    geom_convexhull(alpha = 0.2, 
          fillcolour = "blue", 
         colour = "blue",
         data = OD_Matrix %>% 
                            filter(destination_id == 42, 
                            distance_time <= 8), ## 
 Countour marking a distance of up to 8 minutes
          aes(x = origin_lon, y = origin_lat)) + 
   ## Countour marking a distance of up to 16 minutes
   geom_convexhull(alpha = 0.2, 
                   fill = "red",
                   colour = "red",
                   data = OD_Matrix %>% 
                            filter(destination_id == 42, 
                                   distance_time <= 15), ## 
 Countour marking a distance of up to 16 minutes
          aes(x = origin_lon, y = origin_lat))

enter image description here

I hope this help you to get your reverse Isochrones.

This answer is based on obtaining an origin-destination matrix between a grid of (roughly) equally distant points. This is a computer intensive operation not only because it requires a good number of api calls to mapping services, but also because the servers must calculate a matrix for each call. The number of required calls grows exponentially along the number of points in the grid.

To tackle this problem, I would suggest that you consider running on your local machine or on a local server a mapping server. Project OSRM offers a relatively simple, free and open-source solution, enabling you to run an OpenStreetMap server into a Linux docker (https://github.com/Project-OSRM/osrm-backend). Having your own local mapping server will allow you to make as many API calls as you desire. R's osrm package allows you to interact with OpenStreetMaps' apis. including those placed to a local server.

  BE <- raster::getData("GADM", country = "BEL", level = 1)
  Bruxelles <- BE[BE$NAME_1 == "Bruxelles", ]

  df_grid <- makegrid(Bruxelles, cellsize = 0.02) %>% 
        SpatialPoints() %>% 
        as.data.frame() %>% ## I convert the SpatialPoints object into a simple data.frame
        rownames_to_column() %>% ## create a unique id for each point in the data.frame
        rename(id = rowname, lat = x2, lon = x1) # rename variables of the data.frame with more explanatory names.
 
 options(osrm.server = "http://127.0.0.1:5000/") ## I point osrm.server to the OpenStreet docker running in my Linux machine. Do not run this if you are getting your data from OpenStreet public servers.
 
 Distance_Tables <- osrmTable(loc = df_grid)  ## I obtain a list with distances (Origin Destination Matrix in minutes, origins and destinations)

 OD_Matrix <- Distance_Tables$durations %>% ## Subset the previous list and 
    as_data_frame() %>%  ## ...convert the Origin Destination Matrix into a tibble
   rownames_to_column() %>% 
   rename(origin_id = rowname) %>% ## make sure we have an id column for the OD tibble
   gather(key = destination_id, value = distance_time, -origin_id) %>% # transform the tibble into long/tidy format
   left_join(df_grid, by = c("origin_id" = "id")) %>% 
   rename(origin_lon = lon, origin_lat = lat) %>% ## set origin coordinates
   left_join(df_grid, by = c("destination_id" = "id")) %>% 
   rename(destination_lat = lat, destination_lon = lon) ## set destination coordinates

 ## Obtain a nice looking road map of Brussels
 
 Brux_map <- get_map(location = "bruxelles, belgique", 
                     zoom = 11, 
                     source = "google", 
                     maptype = "roadmap")

 ggmap(Brux_map) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
         data = OD_Matrix %>% 
                filter(destination_id == 42), ## Here I selected point_id 42 as the desired target, just because it is not far from the City Center.
                size = 0.5) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
        data = OD_Matrix %>% 
        filter(destination_id == 42, origin_id == 42),
          shape = 5, size = 3) +  ## Draw a diamond around point_id 42                                      
    geom_convexhull(alpha = 0.2, 
          fill = "blue", 
         colour = "blue",
         data = OD_Matrix %>% 
                filter(destination_id == 42, 
                       distance_time <= 8), ## Countour marking a distance of up to 8 minutes
         aes(x = origin_lon, y = origin_lat)) + 
   geom_convexhull(alpha = 0.2, 
         fill = "red",
         colour = "red",
         data = OD_Matrix %>% 
         filter(destination_id == 42, 
                distance_time <= 15), ## Countour marking a distance of up to 16 minutes
         aes(x = origin_lon, y = origin_lat))

enter image description here

I hope this help you to get your reverse Isochrones.

This answer is based on obtaining an origin-destination matrix between a grid of (roughly) equally distant points. This is a computer intensive operation not only because it requires a good number of API calls to mapping services, but also because the servers must calculate a matrix for each call. The number of required calls grows exponentially along the number of points in the grid.

To tackle this problem, I would suggest that you consider running on your local machine or on a local server a mapping server. Project OSRM offers a relatively simple, free, and open-source solution, enabling you to run an OpenStreetMap server into a Linux docker (https://github.com/Project-OSRM/osrm-backend). Having your own local mapping server will allow you to make as many API calls as you desire. R's osrm package allows you to interact with OpenStreetMaps' APIs, Including those placed to a local server.

  BE <- raster::getData("GADM", country = "BEL", level = 1)
  Bruxelles <- BE[BE$NAME_1 == "Bruxelles", ]

  df_grid <- makegrid(Bruxelles, cellsize = 0.02) %>% 
        SpatialPoints() %>%
        ## I convert the SpatialPoints object into a simple data.frame 
        as.data.frame() %>% 
        ## create a unique id for each point in the data.frame
        rownames_to_column() %>%  
        ## rename variables of the data.frame with more explanatory names.
        rename(id = rowname, lat = x2, lon = x1) 

 ## I point osrm.server to the OpenStreet docker running in my Linux machine. ... 
 ### ... Do not run this if you are getting your data from OpenStreet public servers.
 options(osrm.server = "http://127.0.0.1:5000/")  

 ## I obtain a list with distances (Origin Destination Matrix in ...
 ### ... minutes, origins and destinations)
 Distance_Tables <- osrmTable(loc = df_grid) 

 OD_Matrix <- Distance_Tables$durations %>% ## subset the previous list 
                ## convert the Origin Destination Matrix into a tibble
                as_data_frame() %>%  
                rownames_to_column() %>% 
                ## make sure we have an id column for the OD tibble
                rename(origin_id = rowname) %>% 
                ## transform the tibble into long/tidy format
                gather(key = destination_id, value = distance_time, -origin_id) %>%  
                left_join(df_grid, by = c("origin_id" = "id")) %>% 
                ## set origin coordinates
                rename(origin_lon = lon, origin_lat = lat) %>%  
                left_join(df_grid, by = c("destination_id" = "id")) %>% 
                ## set destination coordinates
                rename(destination_lat = lat, destination_lon = lon) 

 ## Obtain a nice looking road map of Brussels
 Brux_map <- get_map(location = "bruxelles, belgique", 
                     zoom = 11, 
                     source = "google", 
                     maptype = "roadmap")

 ggmap(Brux_map) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
              data = OD_Matrix %>% 
                ## Here I selected point_id 42 as the desired target, ...
                ## ... just because it is not far from the City Center.
                filter(destination_id == 42), 
                size = 0.5) + 
   ## Draw a diamond around point_id 42                                      
   geom_point(aes(x = origin_lon, y = origin_lat), 
              data = OD_Matrix %>% 
                filter(destination_id == 42, origin_id == 42),
              shape = 5, size = 3) +   
   ## Countour marking a distance of up to 8 minutes
   geom_convexhull(alpha = 0.2,  
                   fill = "blue",  
                   colour = "blue",
                   data = OD_Matrix %>% 
                            filter(destination_id == 42, 
                            distance_time <= 8),  
                   aes(x = origin_lon, y = origin_lat)) + 
   ## Countour marking a distance of up to 16 minutes
   geom_convexhull(alpha = 0.2, 
                   fill = "red",
                   colour = "red",
                   data = OD_Matrix %>% 
                            filter(destination_id == 42, 
                                   distance_time <= 15),  
                   aes(x = origin_lon, y = origin_lat))

enter image description here

Bounty Awarded with 500 reputation awarded by M--
Bounty Awarded with 150 reputation awarded by M--
added 108 characters in body
Source Link

To tackle this problem, I would suggest that you consider running on your local machine or on a local server a mapping server. Project OSRM offers a relatively simple, free and open-source solution, enabling you to run an OpenStreetMap server into a Linux docker (https://github.com/Project-OSRM/osrm-backend). Having your own local mapping server will allow you to make as many API calls as you desire. R's osrm package allows you to interact with OpenStreetMaps' apis. including those placed to a local server.

library(raster) # Optional
library(sp)
library(ggmap)
library(tidyverse)
library(osrm)
devtools::install_github("cmartin/ggConvexHull") # Needed to quickly draw the countourscontours
library(ggConvexHull)
  BE <- raster::getData("GADM", country = "BEL", level = 1)
  Bruxelles <- BE[BE$NAME_1 == "Bruxelles", ]

  df_grid <- makegrid(Bruxelles, cellsize = 0.02) %>% 
        SpatialPoints() %>% 
        as.data.frame() %>% ## I convert the SpatialPoints object into a simple data.frame
        rownames_to_column() %>% ## create a unique id for each point in the data.frame
        rename(id = rowname, lat = x2, lon = x1) # rename varaiblesvariables of the data.frame with more explanatory names.

 options(osrm.server = "http://127.0.0.1:5000/") ## I point osrm.server to the OpenStreet docker running in my Linux machine. Do not run this if you are getting your data from OpenStreet public servers.

 Distance_Tables <- osrmTable(loc = df_grid)  ## I obtain a list with distances (Origin Destination Matrix in minutes, origins and destinations)

 OD_Matrix <- Distance_Tables$durations %>% ## Subset the previous list and 
   as_data_frame() %>%  ## ...convert the Origin Destination Matrix into a tibble
   rownames_to_column() %>% 
   rename(origin_id = rowname) %>% ## make sure we have an id column for the OD tibble
   gather(key = destination_id, value = distance_time, -origin_id) %>% # transform the tibble into long/tidy format
   left_join(df_grid, by = c("origin_id" = "id")) %>% 
   rename(origin_lon = lon, origin_lat = lat) %>% ## set origin coordinates
   left_join(df_grid, by = c("destination_id" = "id")) %>% 
   rename(destination_lat = lat, destination_lon = lon) ## set destination coordinates

 ## Obtain a nice looking road map of Brussels

 Brux_map <- get_map(location = "bruxelles, belgique", 
                     zoom = 11, 
                     source = "google", 
                     maptype = "roadmap")

 ggmap(Brux_map) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
         data = OD_Matrix %>% 
                filter(destination_id == 42), ## Here I selected point_id 42 as the desired target, just because it is not far from the City Center.
                size = 0.5) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
        data = OD_Matrix %>% 
        filter(destination_id == 42, origin_id == 42),
          shape = 5, size = 3) +  ## Draw a diamond around point_id 42                                      
   geom_convexhull(alpha = 0.2, 
         fill = "blue", 
         colour = "blue",
         data = OD_Matrix %>% 
                filter(destination_id == 42, 
                       distance_time <= 8), ## Countour marking a distance of up to 8 minutes
         aes(x = origin_lon, y = origin_lat)) + 
   geom_convexhull(alpha = 0.2, 
         fill = "red",
         colour = "red",
         data = OD_Matrix %>% 
         filter(destination_id == 42, 
                distance_time <= 15), ## Countour marking a distance of up to 16 minutes
         aes(x = origin_lon, y = origin_lat))

To tackle this problem, I would suggest that you consider running on your local machine or on a local server a mapping server. Project OSRM offers a relatively simple, free and open-source solution, enabling you to run an OpenStreetMap server into a Linux docker (https://github.com/Project-OSRM/osrm-backend). Having your own local mapping server will allow you to make as many API calls as you desire.

library(raster) # Optional
library(sp)
library(ggmap)
library(tidyverse)
library(osrm)
devtools::install_github("cmartin/ggConvexHull") # Needed to quickly draw the countours
library(ggConvexHull)
  BE <- raster::getData("GADM", country = "BEL", level = 1)
  Bruxelles <- BE[BE$NAME_1 == "Bruxelles", ]

  df_grid <- makegrid(Bruxelles, cellsize = 0.02) %>% 
        SpatialPoints() %>% 
        as.data.frame() %>% ## I convert the SpatialPoints object into a simple data.frame
        rownames_to_column() %>% ## create a unique id for each point in the data.frame
        rename(id = rowname, lat = x2, lon = x1) # rename varaibles of the data.frame with more explanatory names.

 options(osrm.server = "http://127.0.0.1:5000/") ## I point osrm.server to the OpenStreet docker running in my Linux machine. Do not run this if you are getting your data from OpenStreet public servers.

 Distance_Tables <- osrmTable(loc = df_grid)  ## I obtain a list with distances (Origin Destination Matrix in minutes, origins and destinations)

 OD_Matrix <- Distance_Tables$durations %>% ## Subset the previous list and 
   as_data_frame() %>%  ## ...convert the Origin Destination Matrix into a tibble
   rownames_to_column() %>% 
   rename(origin_id = rowname) %>% ## make sure we have an id column for the OD tibble
   gather(key = destination_id, value = distance_time, -origin_id) %>% # transform the tibble into long/tidy format
   left_join(df_grid, by = c("origin_id" = "id")) %>% 
   rename(origin_lon = lon, origin_lat = lat) %>% ## set origin coordinates
   left_join(df_grid, by = c("destination_id" = "id")) %>% 
   rename(destination_lat = lat, destination_lon = lon) ## set destination coordinates

 ## Obtain a nice looking road map of Brussels

 Brux_map <- get_map(location = "bruxelles, belgique", 
                     zoom = 11, 
                     source = "google", 
                     maptype = "roadmap")

 ggmap(Brux_map) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
         data = OD_Matrix %>% 
                filter(destination_id == 42), ## Here I selected point_id 42 as the desired target, just because it is not far from the City Center.
                size = 0.5) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
        data = OD_Matrix %>% 
        filter(destination_id == 42, origin_id == 42),
          shape = 5, size = 3) +  ## Draw a diamond around point_id 42                                      
   geom_convexhull(alpha = 0.2, 
         fill = "blue", 
         colour = "blue",
         data = OD_Matrix %>% 
                filter(destination_id == 42, 
                       distance_time <= 8), ## Countour marking a distance of up to 8 minutes
         aes(x = origin_lon, y = origin_lat)) + 
   geom_convexhull(alpha = 0.2, 
         fill = "red",
         colour = "red",
         data = OD_Matrix %>% 
         filter(destination_id == 42, 
                distance_time <= 15), ## Countour marking a distance of up to 16 minutes
         aes(x = origin_lon, y = origin_lat))

To tackle this problem, I would suggest that you consider running on your local machine or on a local server a mapping server. Project OSRM offers a relatively simple, free and open-source solution, enabling you to run an OpenStreetMap server into a Linux docker (https://github.com/Project-OSRM/osrm-backend). Having your own local mapping server will allow you to make as many API calls as you desire. R's osrm package allows you to interact with OpenStreetMaps' apis. including those placed to a local server.

library(raster) # Optional
library(sp)
library(ggmap)
library(tidyverse)
library(osrm)
devtools::install_github("cmartin/ggConvexHull") # Needed to quickly draw the contours
library(ggConvexHull)
  BE <- raster::getData("GADM", country = "BEL", level = 1)
  Bruxelles <- BE[BE$NAME_1 == "Bruxelles", ]

  df_grid <- makegrid(Bruxelles, cellsize = 0.02) %>% 
        SpatialPoints() %>% 
        as.data.frame() %>% ## I convert the SpatialPoints object into a simple data.frame
        rownames_to_column() %>% ## create a unique id for each point in the data.frame
        rename(id = rowname, lat = x2, lon = x1) # rename variables of the data.frame with more explanatory names.

 options(osrm.server = "http://127.0.0.1:5000/") ## I point osrm.server to the OpenStreet docker running in my Linux machine. Do not run this if you are getting your data from OpenStreet public servers.

 Distance_Tables <- osrmTable(loc = df_grid)  ## I obtain a list with distances (Origin Destination Matrix in minutes, origins and destinations)

 OD_Matrix <- Distance_Tables$durations %>% ## Subset the previous list and 
   as_data_frame() %>%  ## ...convert the Origin Destination Matrix into a tibble
   rownames_to_column() %>% 
   rename(origin_id = rowname) %>% ## make sure we have an id column for the OD tibble
   gather(key = destination_id, value = distance_time, -origin_id) %>% # transform the tibble into long/tidy format
   left_join(df_grid, by = c("origin_id" = "id")) %>% 
   rename(origin_lon = lon, origin_lat = lat) %>% ## set origin coordinates
   left_join(df_grid, by = c("destination_id" = "id")) %>% 
   rename(destination_lat = lat, destination_lon = lon) ## set destination coordinates

 ## Obtain a nice looking road map of Brussels

 Brux_map <- get_map(location = "bruxelles, belgique", 
                     zoom = 11, 
                     source = "google", 
                     maptype = "roadmap")

 ggmap(Brux_map) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
         data = OD_Matrix %>% 
                filter(destination_id == 42), ## Here I selected point_id 42 as the desired target, just because it is not far from the City Center.
                size = 0.5) + 
   geom_point(aes(x = origin_lon, y = origin_lat), 
        data = OD_Matrix %>% 
        filter(destination_id == 42, origin_id == 42),
          shape = 5, size = 3) +  ## Draw a diamond around point_id 42                                      
   geom_convexhull(alpha = 0.2, 
         fill = "blue", 
         colour = "blue",
         data = OD_Matrix %>% 
                filter(destination_id == 42, 
                       distance_time <= 8), ## Countour marking a distance of up to 8 minutes
         aes(x = origin_lon, y = origin_lat)) + 
   geom_convexhull(alpha = 0.2, 
         fill = "red",
         colour = "red",
         data = OD_Matrix %>% 
         filter(destination_id == 42, 
                distance_time <= 15), ## Countour marking a distance of up to 16 minutes
         aes(x = origin_lon, y = origin_lat))
edited body
Source Link
Loading
edited body
Source Link
Loading
Source Link
Loading