#Package OSRM for travel times and isoChrones

# One way to get travel time features is the OSRM package. The package will give you
    # the function 
    #
    # osrmTable - Get travel time matrices between points.
    #   osrmRoute - Get the shortest path between two points.
    #   osrmTrip - Get the travel geometry between multiple unordered points.
    #   osrmIsochrone - Get polygons of isochrones.
    
    # Example 1
  library(sf)
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
    library(osrm)
## Warning: package 'osrm' was built under R version 4.0.5
## Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright
## Routing: OSRM - http://project-osrm.org/
    data("berlin")
    
    # Dataset with the structure {id,lat,lon} defining points
    head(apotheke.df)
##                    id      lon      lat
## 440338666   440338666 13.43853 52.47728
## 538057637   538057637 13.57874 52.45823
## 977657079   977657079 13.49039 52.61719
## 3770254015 3770254015 13.51974 52.49737
## 364363337   364363337 13.45582 52.50112
## 3666540067 3666540067 13.33844 52.49479
    # Travel time matrix
    # points in df is treated as origin and destination
    distA <- osrmTable(loc = apotheke.sf[1:5,])
    str(distA)
## List of 3
##  $ durations   : num [1:5, 1:5] 0 22.7 32.9 19.4 9.5 21.3 0 40.4 15.3 20.2 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:5] "440338666" "538057637" "977657079" "3770254015" ...
##   .. ..$ : chr [1:5] "440338666" "538057637" "977657079" "3770254015" ...
##  $ sources     :'data.frame':    5 obs. of  2 variables:
##   ..$ lon: num [1:5] 13.4 13.6 13.5 13.5 13.5
##   ..$ lat: num [1:5] 52.5 52.5 52.6 52.5 52.5
##  $ destinations:'data.frame':    5 obs. of  2 variables:
##   ..$ lon: num [1:5] 13.4 13.6 13.5 13.5 13.5
##   ..$ lat: num [1:5] 52.5 52.5 52.6 52.5 52.5
    distA$durations
##            440338666 538057637 977657079 3770254015 364363337
## 440338666        0.0      21.3      33.4       19.9      12.0
## 538057637       22.7       0.0      41.7       16.1      20.2
## 977657079       32.9      40.4       0.0       30.2      27.2
## 3770254015      19.4      15.3      29.4        0.0      12.9
## 364363337        9.5      20.2      26.7       12.0       0.0
    # isoChrones around a point
    # returns multipolygones defining isochrones
    bks <- seq(from = 0, to = 14, by = 2)
    iso <- osrmIsochrone(loc = apotheke.sf[87,], returnclass="sf",
                     breaks = bks, res = 70)
    plot(iso)

    iso
## Simple feature collection with 7 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -21568.76 ymin: 5841782 xmax: -8773.475 ymax: 5854363
## Projected CRS: WGS 84 / UTM zone 34N
##   id min max                       geometry center
## 1  1   0   2 MULTIPOLYGON (((-15377.69 5...      1
## 2  2   2   4 MULTIPOLYGON (((-14438.22 5...      3
## 3  3   4   6 MULTIPOLYGON (((-15228.68 5...      5
## 4  4   6   8 MULTIPOLYGON (((-14045.78 5...      7
## 5  5   8  10 MULTIPOLYGON (((-19306.52 5...      9
## 6  6  10  12 MULTIPOLYGON (((-18565.1 58...     11
## 7  7  12  14 MULTIPOLYGON (((-16373.25 5...     13