Check whether an OSRM server answers a routing request
Source:R/isochrone-osrm.R
cacs_validate_osrm_endpoint.RdSends one small request to an Open Source Routing Machine (OSRM) server
and reports whether the server answered it with HTTP status 200.
cacs_isochrone() sends many requests for each site, so this shows
beforehand whether the server can be reached and is accepting requests.
Arguments
- server
A string giving the address of the OSRM server, or
NULL(the default). WithNULLor"", the address is the value of the optionosrm.serverat the time of the call, or the public OSRM demo serverhttps://routing.openstreetmap.de/when the option is not set. Loading the osrm package, for example withlibrary(osrm), sets that option to the demo server and replaces a value set before (see the "OSRM servers" section ofcacs_isochrone()). The optioncatchmentACS.osrm_docker_serveris not used, so the local server thatcacs_isochrone()uses withosrm_mode = "docker"is checked only when its address is given here.- timeout
A single positive number giving the number of seconds to wait for an answer; the default is 5.
Value
A tibble with one row and four columns:
endpointThe server address used.
quota_okTRUEwhen the server answered with HTTP status 200, andFALSEotherwise, including when there was no answer.response_msThe time taken, in milliseconds, including any time spent waiting for an answer that did not come.
http_statusThe HTTP status of the answer, as an integer, or
NAwhen there was no answer.
Details
When the server answers with status 429, meaning that its limit on
requests has been reached, the function gives a warning of class
catchmentACS_warning_provider_quota_exhausted and still returns its
result. Other statuses give no warning. A server that cannot be reached,
or does not answer within timeout seconds, gives quota_ok = FALSE and
http_status = NA without a warning or an error.
The request is for a route between two fixed points near Birmingham,
Alabama, so a server whose map data do not cover them may not answer with
status 200. It is sent to route/v1/driving/-86.8,33.5;-86.7,33.4 under
the server address; on the public demo server, routed-car/,
routed-bike/, or routed-foot/ comes first, following the option
osrm.profile.
See also
Other validation and conditions:
cacs_acs_validate(),
cacs_capture_conditions(),
cacs_validate_iso(),
catchmentACS-conditions
Examples
# A local port where no server is expected to answer: quota_ok is FALSE
# and http_status is NA, without an error
cacs_validate_osrm_endpoint("http://localhost:9", timeout = 1)
#> # A tibble: 1 × 4
#> endpoint quota_ok response_ms http_status
#> <chr> <lgl> <dbl> <int>
#> 1 http://localhost:9 FALSE 9.80 NA
# Contacts the public OSRM demo server, which limits the requests it
# accepts, and then builds a drive-time area there.
if (FALSE) { # \dontrun{
site_07 <- data.frame(site_id = "AL_SITE_07", lon = -85.365, lat = 31.655)
# Check the server before building the drive-time areas
check <- cacs_validate_osrm_endpoint()
check
if (check$quota_ok) {
iso <- cacs_isochrone(site_07, drive_times = 10)
} else if (identical(check$http_status, 429L)) {
# Request limit reached: build the areas with a local OSRM server
iso <- cacs_isochrone(site_07, drive_times = 10, osrm_mode = "docker")
} else {
stop("The OSRM server is not accepting requests; see check$http_status.")
}
} # }