Introduction to arcpullr



The arcpullr package provides functions for pulling spatial data from an ArcGIS REST API and formatting those layers into either sf or Raster* objects (depending on the layer being requested). These functions provide the basis for retrieving spatial data housed in an ArcGIS REST API using either spatial or relational queries. The output from these querying functions is intended to work seamlessly with other spatial packages already implemented and established in R.



The get_spatial_layer() function

This is one of the core functions of the package, and allows users to pull Feature Service data from an ArcGIS REST API.

# url <- some url from an ArcGIS REST API layer
layer <- get_spatial_layer(url)

The URL should be a specific layer from an ArcGIS REST API, such as the Wisconsin Dep’t. of Natural Resources Musky Streams layer, for example.

The get_spatial_layer() function will retrieve data from this layer, and format it to an object of class (i.e. of the R package sf: Simple Features for R).

This function is also query-able both using both SQL code and ArcGIS’s Query (Feature Service) functionality.

Querying Spatially via ArcGIS Feature Service

Along with SQL, layers from an ArcGIS REST API may be queried spatially. This is accomplished with the get_layer_by_* family of functions. These functions are essentially a wrapper around get_spatial_layer that removes the abstraction of the spatial query syntax used by ArcGIS. These functions require a spatial object of class sf to be passed to the geometry argument. To test this out you can also quickly create simple sf objects using sf_lines(), sf_points(), or sf_polygons() to test out the service feature.

wdnr_base_url <- "https://dnrmaps.wi.gov/arcgis/rest/services"
streams_layer_url <- "WT_SWDV/WT_Inland_Water_Resources_WTM_Ext_v2/MapServer/2"
streams_url <- paste(wdnr_base_url, streams_layer_url, sep = "/")
mke_waters <- get_layer_by_poly(url = streams_url, mke_county)

Spatial queries can be done with polygons, lines, or points…just use the respective get_layer_by_* function.

Querying via SQL

To query via SQL within the function the field name for the query of interest must be known. For example,

open_water_layer_url <- "WT_SWDV/WT_Inland_Water_Resources_WTM_Ext_v2/MapServer/3"
hydro_url <- paste(wdnr_base_url, open_water_layer_url, sep = "/")
wi_river <- get_spatial_layer(hydro_url, 
  where = "WATERBODY_ROW_NAME = 'Wisconsin River'"
)

For multiple WHERE clauses there is a helper function to string them together properly. This can be called to the where argument above in place of a text string.

sql_where(ROW_WATERBODY_NAME = 'Wisconsin River', HYDROCODE = 7011)
#> [1] "ROW_WATERBODY_NAME = 'Wisconsin River' AND HYDROCODE = 7011"



The get_map_layer() function

This function retrieves map service layers from an ArcGIS REST services API and returns them as a RasterLayer object (i.e. of the R package raster: Geographic Data Analysis and Modeling).

These layers require a bounding box to query the map layer, which is either taken from the sf_object argument or optionally can be passed via the bbox argument.

# url <- some url from an ArcGIS REST API layer
# sf_object <- some sf_object
raster_layer<- get_map_layer(url,sf_object)

The URL should be a specific raster layer from an ArcGIS REST API, such as the Wisconsin Dep’t. of Natural Resources Wiscland Layer.



The get_image_layer function

This function retrieves image service layers from an ArcGIS REST services API and returns them as a RasterStack object.

These layers require a bounding box to query the map layer, which is either taken from the sf_object argument or optionally can be passed via the bbox argument.

# url <- some url from an ArcGIS REST API layer
# sf_object <- some sf_object
image_layer<- get_image_layer(url,sf_object)

The URL should be a specific image layer from an ArcGIS REST API, such as the Wisconsin Dep’t. of Natural Resources Leaf Off Basemap.



Plotting Layers

Layers retrieved via any of the get_*_layer functions may be plotted using an appropriate method which corresponds to the object type. For quick plotting we’ve also included the plot_layer function. This function will plot layers using ggplot2 by default or can be switched to base graphics using the plot_pkg argument.

wdnr_base_url <- "https://dnrmaps.wi.gov/arcgis/rest/services/"
wolf_zone_layer_url <- "DW_Map_Dynamic/EN_Hunting_Zones_WTM_Ext/MapServer/0"
wolf_zone_url <- paste0(wdnr_base_url,wolf_zone_layer_url)
wi_wolf_zones <- get_spatial_layer(url = wolf_zone_url)
plot_layer(wi_wolf_zones)