| Title: | Interface to the 'Azure Maps' API |
|---|---|
| Description: | Provides a wrapper for the Microsoft 'Azure Maps' REST APIs <https://learn.microsoft.com/en-us/rest/api/maps/route?view=rest-maps-2025-01-01>, enabling users to access mapping and geospatial services directly from R. This package simplifies authenticating, building, and sending requests for services like route directions. It handles conversions between R objects (such as 'sf' objects) and the GeoJSON+JSON format required by the API, making it easier to integrate 'Azure Maps' into R-based data analysis workflows. |
| Authors: | Juan P. Fonseca-Zamora [aut, cre] (ORCID: <https://orcid.org/0009-0006-7042-3828>) |
| Maintainer: | Juan P. Fonseca-Zamora <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.0.2.9000 |
| Built: | 2026-05-27 08:08:43 UTC |
| Source: | https://github.com/juanfonsecals1/azuremapsr |
check conformity of parameters for JSON section
check_params(test_params, template_params, tz)check_params(test_params, template_params, tz)
test_params |
list of parameters from input |
template_params |
list of parameters hardcoded in package |
tz |
timezone from input |
No return value, called for side effects
## Not run: check_params(params,template_params,"UTC") ## End(Not run)## Not run: check_params(params,template_params,"UTC") ## End(Not run)
Retrieves the Azure Maps API token from the environment.
get_azuremaps_token()get_azuremaps_token()
A character string containing the Azure Maps API token.
## Not run: get_azuremaps_token() ## End(Not run)## Not run: get_azuremaps_token() ## End(Not run)
Converts a pair of coordinates (numeric vector), a matrix of coordinates, or an sf/sfc POINT object into an sfc object for use in GeoJSON bodies. Only POINT geometries are supported. The output is always in EPSG:4326.
get_point(x, multiple = FALSE) ## Default S3 method: get_point(x, multiple = FALSE) ## S3 method for class 'numeric' get_point(x, multiple = FALSE) ## S3 method for class 'matrix' get_point(x, multiple = FALSE) ## S3 method for class 'sf' get_point(x, multiple = FALSE) ## S3 method for class 'sfc' get_point(x, multiple = FALSE)get_point(x, multiple = FALSE) ## Default S3 method: get_point(x, multiple = FALSE) ## S3 method for class 'numeric' get_point(x, multiple = FALSE) ## S3 method for class 'matrix' get_point(x, multiple = FALSE) ## S3 method for class 'sf' get_point(x, multiple = FALSE) ## S3 method for class 'sfc' get_point(x, multiple = FALSE)
x |
A numeric vector of length 2, a matrix with two columns (coordinates), or an sf/sfc object of POINT type. |
multiple |
Logical; if TRUE, allows handling of multiple features (e.g., when input is an sfc or sf object with more than one POINT). Default is FALSE. |
An sfc object with coordinates in EPSG:4326.
get_point(c(-122.201399, 47.608678)) get_point( matrix( c(-122.201399, 47.608678, -122.202, 47.609), ncol = 2, byrow = TRUE ), multiple = TRUE ) library(sf) pt <- st_sf( geometry = st_sfc(st_point(c(-122.201399, 47.608678)), crs = 4326) ) get_point(pt)get_point(c(-122.201399, 47.608678)) get_point( matrix( c(-122.201399, 47.608678, -122.202, 47.609), ncol = 2, byrow = TRUE ), multiple = TRUE ) library(sf) pt <- st_sf( geometry = st_sfc(st_point(c(-122.201399, 47.608678)), crs = 4326) ) get_point(pt)
This function takes a successful response object from the 'Azure Maps' API,
extracts the main route and any alternative routes, and combines them into a
single sf object.
get_routes(resp)get_routes(resp)
resp |
An |
An sf object containing the combined main and alternative routes.
If the request was not successful (status code is not 200), the function
will stop with an error.
## Not run: # Assuming 'response' is a successful response from req_route_directions all_routes_sf <- get_routes(response) plot(sf::st_geometry(all_routes_sf)) ## End(Not run)## Not run: # Assuming 'response' is a successful response from req_route_directions all_routes_sf <- get_routes(response) plot(sf::st_geometry(all_routes_sf)) ## End(Not run)
This function processes a JSON response body from the Azure Maps API,
extracts the route information, and converts it into a spatial (sf) object.
json_to_sf(body, main_route = TRUE, linestring = TRUE)json_to_sf(body, main_route = TRUE, linestring = TRUE)
body |
A list, typically the parsed JSON response from an httr2 request. |
main_route |
A logical value. If |
linestring |
A logical value. If |
An sf object containing the spatial features from the route response,
or NULL if no valid features are found.
## Not run: # Assuming 'resp' is an httr2 response object from req_route_directions body <- httr2::resp_body_json(resp) route_sf <- json_to_sf(body) plot(sf::st_geometry(route_sf)) ## End(Not run)## Not run: # Assuming 'resp' is an httr2 response object from req_route_directions body <- httr2::resp_body_json(resp) route_sf <- json_to_sf(body) plot(sf::st_geometry(route_sf)) ## End(Not run)
Constructs the GeoJSON part of the request body for the Azure Maps Route Directions API. This includes the origin, destination, and any waypoints.
POSTbody_builder_directions_geojson(origin, destination, waypoints = NULL)POSTbody_builder_directions_geojson(origin, destination, waypoints = NULL)
origin |
A numeric vector of coordinates (longitude, latitude) or an |
destination |
A numeric vector of coordinates (longitude, latitude) or an
|
waypoints |
Optional. A numeric vector, a matrix of coordinates, or an
|
A list formatted as a GeoJSON FeatureCollection, ready to be included in the API request body.
## Not run: origin <- c(-122.201399, 47.608678) destination <- c(-122.201669, 47.615076) waypoints <- c(-122.20687, 47.612002) geojson_part <- POSTbody_builder_directions_geojson(origin, destination, waypoints) ## End(Not run)## Not run: origin <- c(-122.201399, 47.608678) destination <- c(-122.201669, 47.615076) waypoints <- c(-122.20687, 47.612002) geojson_part <- POSTbody_builder_directions_geojson(origin, destination, waypoints) ## End(Not run)
Constructs the JSON part of the request body containing routing parameters for the Azure Maps Route Directions API.
POSTbody_builder_directions_json(params, tz)POSTbody_builder_directions_json(params, tz)
params |
A list of routing parameters, such as |
tz |
A string specifying the timezone for any date-time parameters. |
A list of routing parameters, with values formatted and unboxed as required for the JSON request.
## Not run: params <- list( travelMode = "car", routeType = "fastest" ) json_part <- POSTbody_builder_directions_json(params, "UTC") ## End(Not run)## Not run: params <- list( travelMode = "car", routeType = "fastest" ) json_part <- POSTbody_builder_directions_json(params, "UTC") ## End(Not run)
Requests route directions from 'Azure Maps' API using origin, destination, waypoints, and route parameters.
req_route_directions( origin, destination, waypoints = NULL, params, tz = Sys.timezone(), api_key = get_azuremaps_token(), api_version = "2025-01-01" )req_route_directions( origin, destination, waypoints = NULL, params, tz = Sys.timezone(), api_key = get_azuremaps_token(), api_version = "2025-01-01" )
origin |
A numeric vector of length 2 with origin coordinates (longitude, latitude), or an |
destination |
A numeric vector of length 2 with destination coordinates (longitude, latitude), or an |
waypoints |
Optional. A numeric vector, a matrix of coordinates, or an |
params |
A list of route parameters (e.g., |
tz |
A string specifying the timezone. Defaults to the system's timezone. |
api_key |
The 'Azure Maps' API key. Defaults to the value retrieved by |
api_version |
The API version to use. Defaults to "2025-01-01". |
An httr2_response object from the 'Azure Maps' API.
## Not run: origin <- c(-122.201399, 47.608678) destination <- c(-122.201669, 47.615076) waypoints <- c(-122.20687, 47.612002) params <- list( optimizeRoute = "fastestWithTraffic", routeOutputOptions = "routePath", maxRouteCount = 3, travelMode = "driving" ) response <- req_route_directions(origin, destination, waypoints, params) ## End(Not run)## Not run: origin <- c(-122.201399, 47.608678) destination <- c(-122.201669, 47.615076) waypoints <- c(-122.20687, 47.612002) params <- list( optimizeRoute = "fastestWithTraffic", routeOutputOptions = "routePath", maxRouteCount = 3, travelMode = "driving" ) response <- req_route_directions(origin, destination, waypoints, params) ## End(Not run)
get_route_directions functionA sample response obtained from the get_route_directions function
a httr2 response object
Sample of the call based on the API documentation in https://learn.microsoft.com/en-us/rest/api/maps/route/post-route-directions?view=rest-maps-2025-01-01&tabs=HTTP#examples
get_route_directions functionA sample response obtained from the get_route_directions function
a httr2 response object
Saves an authentication token for the Azure Maps API in the environment.
set_azuremaps_token(token)set_azuremaps_token(token)
token |
A character string containing the Azure Maps API token. |
Logical TRUE if the token is correctly set.
## Not run: set_azuremaps_token("your_token_here") ## End(Not run)## Not run: set_azuremaps_token("your_token_here") ## End(Not run)