Provides an R interface to the graphhopper routing engine, an open source route planning service.
route_graphhopper(from, to, l = NULL, vehicle = "bike", silent = TRUE, pat = NULL, base_url = "https://graphhopper.com")
| from | Text string or coordinates (a numeric vector of
|
|---|---|
| to | Text string or coordinates (a numeric vector of
|
| l | Only needed if from and to are empty, in which case this should be a spatial object representing desire lines |
| vehicle | A text string representing the vehicle. Can be bike (default), car or foot. See https://graphhopper.com/api/1/docs/supported-vehicle-profiles/ for further details. |
| silent | Logical (default is FALSE). TRUE hides request sent. |
| pat | The API key used. By default this is set to NULL and this is usually aquired automatically through a helper, api_pat(). |
| base_url | The base url from which to construct API requests (with default set to main server) |
The function returns a SpatialLinesDataFrame object. See https://github.com/graphhopper for more information.
To test graphopper is working for you, try something like this, but with
your own API key:
To use this function you will need to obtain an API key from
https://graphhopper.com/#directions-api.
It is assumed that you have set your api key as a system environment
for security reasons (so you avoid typing the API key in your code).
Do this by adding the following to your .Renviron file (see ?.Renviron
or the 'api-packages' vignette at https://cran.r-project.org/package=httr
for more on this):
GRAPHHOPPER='FALSE-Key-eccbf612-214e-437d-8b73-06bdf9e6877d'.
(Note: key not real, use your own key.)
obj <- jsonlite::fromJSON(url)
Where url is an example api request from
https://github.com/graphhopper/directions-api/blob/master/routing.md.
route_cyclestreet
# NOT RUN { from <- c(-0.12, 51.5) to <- c(-0.14, 51.5) r1 <- route_graphhopper(from = from, to = to, silent = FALSE) r2 <- route_graphhopper("London Eye", "Westminster", vehicle = "foot") r3 <- route_graphhopper("London Eye", "Westminster", vehicle = "car") plot(r1) plot(r2, add = TRUE, col = "blue") # compare routes plot(r3, add = TRUE, col = "red") # }