Routes.Rmd
With TargomoR
you can access routing data all over the world in a convenient, nested structure, letting you focus on the start and end points. Visualising a route can add the finishing touch to a map or shiny application!
For the routing service, getTargomoRoutes()
returns a named nested list. The names are the travel modes given in the travelType
option (specifying multiple modes will make multiple calls to the API).
Within each travel mode is a list of the returned routes - one for each source/target combination where a route has been found. Each route is a data.frame, containing the source and target IDs, and a features
data.frame of a simple feature collection (class ‘sf’). This contains:
Each SFC also contains fields giving the start and end names of the LINESTRINGs, the length of time the section takes and the distance in metres.
For example, for two routes by bike, the output will look like:
# set source and target data
source <- data.frame(id = "Big Ben", lat = 51.5007, lng = -0.1246)
target <- data.frame(id = c("Tower Bridge", "Covent Garden"),
lat = c(51.5055, 51.5117),
lng = c(-0.0754, -0.1246))
# query the API
routes <- getTargomoRoutes(source_data = source, target_data = target,
source_lat = ~lat, source_lng = ~lng,
target_lat = ~lat, target_lng = ~lng,
source_id = ~id, target_id = ~id,
options = targomoOptions(travelType = "bike"))
# examine the structure
str(routes, max.level = 3, give.attr = FALSE)
#> List of 1
#> $ bike:List of 2
#> ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 3 obs. of 3 variables:
#> .. ..$ sourceId: chr [1:3] "Big Ben" "Big Ben" "Big Ben"
#> .. ..$ targetId: chr [1:3] "Tower Bridge" "Tower Bridge" "Tower Bridge"
#> .. ..$ features:Classes 'sf' and 'data.frame': 3 obs. of 9 variables:
#> ..$ :Classes 'tbl_df', 'tbl' and 'data.frame': 3 obs. of 3 variables:
#> .. ..$ sourceId: chr [1:3] "Big Ben" "Big Ben" "Big Ben"
#> .. ..$ targetId: chr [1:3] "Covent Garden" "Covent Garden" "Covent Garden"
#> .. ..$ features:Classes 'sf' and 'data.frame': 3 obs. of 9 variables:
The features
object for the first route will look like:
routes$bike[[1]]$features
#> Simple feature collection with 3 features and 8 fields
#> geometry type: GEOMETRY
#> dimension: XY
#> bbox: xmin: -0.1245714 ymin: 51.50074 xmax: -0.0743502 ymax: 51.51129
#> epsg (SRID): 4326
#> proj4string: +proj=longlat +datum=WGS84 +no_defs
#> startName length targetId travelTime geometry
#> 1 source 4453.667 <NA> 1371 LINESTRING (-0.0743917 51.5...
#> 2 <NA> NA Tower Bridge NA POINT (-0.0743917 51.50643)
#> 3 <NA> NA <NA> NA POINT (-0.1245714 51.50074)
#> endName sourceId travelType name
#> 1 target <NA> BIKE <NA>
#> 2 <NA> <NA> <NA> Target
#> 3 <NA> Big Ben <NA> Source
The TargomoR
package lets you easily visualise your route data. You can either:
drawTargomoRoutes()
, or;addTargomoRoutes()
, or;leaflet::addPolylines
In this example, we use addTargomoRoutes()
to show the cycling routes above.
leaflet() %>%
addTargomoTiles() %>%
addTargomoRoutes(source_data = source, source_id = ~id,
target_data = target, target_id = ~id,
options = targomoOptions(travelType = "bike"))
#> Assuming "lng" and "lat" are longitude and latitude, respectively
#> Assuming "lng" and "lat" are longitude and latitude, respectively
Using addTargomoRoutes()
or drawTargomoRoutes()
you can customise the way the routes appear on the map with a call to routeDrawOptions()
. The options available are:
Option | Default | Meaning |
---|---|---|
showMarkers | TRUE |
Should the source/target points be marked? |
showTransfers | TRUE |
Should the public transport changes be marked? |
{travel}Color | car = "blue" , bike = "orange" , walk = "green" , transit = "red"
|
The route line color |
{travel}Weight |
5 for all travel types |
The route line width |
{travel}DashArray | walk = "1,10" , other = NULL
|
The route dash pattern |
transferColor | "blue" |
The color for transfer circle markers |
transferRadius | 10 |
The radius for transfer circle markers |
Changing these, we could for instance make the map above but with purple dotted lines (and here we’ll use drawTargomoRoutes()
):