# wdnr.gis-Introduction

wdnr.gis is a package for pulling data from the Wisconsin Department of Natural Resources (WDNR) ArcGIS REST API sites, which are located here and here. There are a wide variety of services and layers on these sites, and this package helps to make sense of and use these data.

    library(wdnr.gis)
    #> Loading required package: arcpullr
    #> Loading required package: sf
    #> Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE

Notice that dependency packages arcpullr and sf are loaded along with wdnr.gis. These are essential packages that are used to pull data from an ArcGIS REST API and convert them to an sf object in R. All other functions in wdnr.gis are just wrappers around functions within arcpullr and sf that are specific to WDNR’s ArcGIS REST API.

# Pulling Specific Feature Layers

There are a handful of functions that are written specifically to query certain commonly used layers by using either a spatial or a SQL query. The querying is mostly built in to these functions, but advanced querying functionality is also available.

    mke_cty_streams <- get_hydro_layer(county = "milwaukee")

You can also pass an sf object to these get_*_layer functions. This example just shows how to query the watershed that a particular point falls in and the streams within that watershed

    pt <- sf_point(c(-90.8, 43.4))
    watershed <- get_watershed_layer(sf_object = pt, huc_level = "HUC_8")
    streams <- get_hydro_layer(sf_object = watershed)

Current functions that query specific WDNR layers are `get_hydro_layer`, `get_watershed_layer`, `get_fmdb_site_layer`, and `get_roads_layer`. Others are available upon request. If you have a layer that you use a lot, email us with a request for a layer specific function and we can add it in (especially if it will be useful to many others as well).

# Pulling Specific Map and Image Layers

These functions work similarly to the other specific feature layer functions, but they query the WDNR’s map and image services instead and return a raster.

To get landcover data use the `get_wis_landcover` function. For aerial imagery use the `get_wis_imagery` function.

    portage_lc <- get_wis_landcover(county = "portage")
    plot_layer(portage_lc)

The specific landcover service to be queried can be altered using the `service` argument. A full list of available services can be found by running `list_services(section = "DW_Land_Cover")`.

    portage_imagery <- get_wis_imagery(county = "portage")
    plot_layer(portage_imagery)

Similar to `get_wis_landcover` the service to be queried can be altered using the `service` argument. A full list of available image services can be found by running `list_services(section = "DW_Image")`.

# Finding Sections, Services, and Layers

ArcGIS REST APIs are hierarchical in nature. There are one or more folders, each of which contain one or more services. Within services are one or more layers and perhaps sub-layers. The layers contain the data of interest, but we’ve built in functions to be able to find any of the folders, services, or layers using the following functions:

    match_sections("trout")
    #> [1] "FM_Trout"
    match_services("trout")
    #> [1] "FM_TROUT_ANNO_WTM_Ext"             "FM_TROUT_HAB_SITES_WTM_Ext"
    #> [3] "FM_TROUT_NONDNR_EASEMENTS_WTM_Ext" "FM_TROUT_REGS_WTM_Ext"
    match_layers("trout stream")
    #> [1] "Trout Stream Regulations" "PNW-ASNRI Trout Streams"
    #> [3] "Trout Stream Lines"

There are also functions to find specific sections, services, and layers when the name is known. These are most useful when trying to obtain a URL for a specific layer.

    list_services("FM_Trout")
    #> [1] "FM_TROUT_ANNO_WTM_Ext"             "FM_TROUT_HAB_SITES_WTM_Ext"
    #> [3] "FM_TROUT_NONDNR_EASEMENTS_WTM_Ext" "FM_TROUT_REGS_WTM_Ext"

These functions are most useful when combined for finding specific layers and the associated URLs.

    list_layers(services = match_services("trout.*stream"))
    #> character(0)
    list_urls(layers = match_layers("trout.*stream"))
    #> [1] "https://dnrmaps.wi.gov/arcgis/rest/services/FM_Trout/FM_TROUT_REGS_WTM_Ext/MapServer/0"
    #> [2] "https://dnrmaps.wi.gov/arcgis/rest/services/WT_SWDV/WY_DESIGNATED_WATERS/MapServer/5"
    #> [3] "https://dnrmaps.wi.gov/arcgis/rest/services/WT_SWDV/WY_FISHERIES_WATERS/MapServer/9"

Notice the use of regular expressions in the `list_urls` function.