ArcGIS REST API’s may be spatially queried using the get_layer_by_*
family of functions. These functions require a spatial object of class
sf
(i.e. of the R package sf: Simple Features for R)
and a Spatial
Relationship to be passed to the geometry and sp_rel
arguments respectively.
The package contains five functions that can be used to perform spatial queries:
get_layer_by_line
get_layer_by_point
get_layer_by_polygon
get_layer_by_multipoint
get_layer_by_envelope
#WDNR Server
server <- "https://dnrmaps.wi.gov/arcgis/rest/services/"
server2 <- "https://dnrmaps.wi.gov/arcgis2/rest/services/"
#River URL
layer <- "TS_AGOL_STAGING_SERVICES/EN_AGOL_STAGING_SurfaceWater_WTM/MapServer/2"
river_url <- paste0(server2,layer)
#Country URL
layer <- "DW_Map_Dynamic/EN_Basic_Basemap_WTM_Ext_Dynamic_L16/MapServer/3"
county_url <- paste0(server,layer)
#Trout URL
layer <- "FM_Trout/FM_TROUT_HAB_SITES_WTM_Ext/MapServer/0"
trout_url <- paste0(server,layer)
#Watershed URL
layer <- "WT_SWDV/WT_Federal_Hydrologic_Units_WTM_Ext/MapServer/0"
watershed_url <- paste0(server,layer)
#get layers for queries
mke_river <- get_spatial_layer(
river_url,
where = "RIVER_SYS_NAME = 'Milwaukee River'"
)
trout_hab_project_pts <- get_spatial_layer(
trout_url,
where = "WATERBODYNAMECOMBINED = 'Sugar Creek' and FISCALYEAR = 2017"
)
trout_hab_project_pt <- trout_hab_project_pts[1, ]
# get watershed layer for Cook Creek
cook_creek_ws <- get_spatial_layer(
watershed_url,
where = "HUC12_NAME = 'Cook Creek'"
)
The get_layer_by_line
function uses A LINSESTRING or
MULTILINESTRING sf object to query an ArcGIS REST API. The below example
uses a MULTILINESTRING sf object of the Milwaukee River to query the
Wisconsin County polygon layer.
mke_river_counties <- get_layer_by_line(url = county_url, geometry = mke_river)
plot_layer(mke_river, outline_poly = mke_river_counties)