---
title: "Introduction to rlandfire"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to rlandfire}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

<img src="../man/figures/rlandfire.png" align="left" width="125" height="125"/>

## rlandfire: Tools for Accessing and Working with LANDFIRE in R
\
\
`rlandfire` provides access to a diverse suite of spatial data layers via the LANDFIRE Product Services ([LFPS](https://lfps.usgs.gov/arcgis/rest/services/LandfireProductService/GPServer)) API. [LANDFIRE](https://landfire.gov) is a joint program of the USFS, DOI, and other major partners, which provides data layers for wildfire management, fuel modeling, ecology, natural resource management, climate, conservation, etc. The complete list of available layers and additional resources can be found on the LANDFIRE webpage.

## Installation

Install `rlandfire` from CRAN:

``` r
install.packages("rlandfire")
```

The development version of `rlandfire` can be installed from GitHub with:

``` r
# install.packages("devtools")
devtools::install_github("bcknr/rlandfire")
```

Set `build_vignettes = TRUE` to access this vignette in R:

``` r
devtools::install_github("bcknr/rlandfire", build_vignettes = TRUE)
```

This package is still in development, and users may encounter bugs or unexpected behavior. Please report any issues, feature requests, or suggestions in the package's [GitHub repo](https://github.com/bcknr/rlandfire/issues).

## `rlandfire` vs LANDFIRE Product Service (LFPS)

![Comparison of LFPS and `landfireAPI()`](../man/figures/lfps.png){width="600"}

## Using `rlandfire`

To demonstrate `rlandfire`, we will explore how ponderosa pine forest canopy cover changed after the 2020 Calwood fire near Boulder, Colorado.

```{r setup}
library(rlandfire)
library(sf)
library(terra)
```

First, we will load the Calwood Fire perimeter data which was downloaded from Boulder County's [geospatial data hub](https://opendata-bouldercounty.hub.arcgis.com/).

```{r, fig.align = "center", fig.height = 5, fig.width = 7}
boundary_file <- file.path(tempdir(), "Wildfire_History")
utils::unzip(system.file("extdata/Wildfire_History.zip", package = "rlandfire"),
             exdir = boundary_file)

boundary <- st_read(file.path(boundary_file, "Wildfire_History.shp")) %>% 
  sf::st_transform(crs = st_crs(32613))

plot(boundary$geometry, main = "Calwood Fire Boundary (2020)", 
     border = "red", lwd = 1.5)
```

### AOI

We can use the function `rlandfire::getAOI()` to create an area of interest (AOI) vector with the correct format for `landfireAPI()`. `getAOI()` handles several steps for us, it ensures that the AOI is returned in the correct order (`xmin`, `ymin`, `xmax`, `ymax`) and converts the AOI to latitude and longitude coordinates (as required by the API) if needed.

Using the `extend` argument, we will increase the AOI by 1 km in all directions to provide additional context surrounding the burned area. This argument takes an optional numeric vector of 1, 2, or 4 elements.

```{r}
aoi <- getAOI(boundary, extend = 1000)
aoi
```

Alternatively, you can supply a LANDFIRE map zone number in place of the AOI vector. The function `getZone()` returns the zone number containing an `sf` object or which corresponds to the supplied zone name. See `help("getZone")` for more information and an example.

### Products

For this example, we are interested in canopy cover data for two years, 2019 (`200CC_19`) and 2022 (`220CC_22`), and existing vegetation type (`200EVT`). All available data products, and their abbreviated names, can be found in the [products table](https://lfps.usgs.gov/helpdocs/productstable.html) which can be opened by calling `viewProducts()`.

```{r}
products <- c("200CC_19", "220CC_22", "200EVT")
```

### Projection and resolution

We can ask the API to project the data to the same CRS as our fire perimeter data by providing the `WKID` for our CRS of interest and a resolution of our choosing, in meters.

```{r}
projection <- 32613
resolution <- 90
```

### Edit rule

We will use the `edit_rule` argument to filter out canopy cover data that does not correspond to Ponderosa Pine Woodland. The `edit_rule` statement should tell the API that when existing vegetation cover is anything other than Ponderosa Pine Woodland (`7054`), the value of the canopy cover layers should be set to a specified value.

To do so, we specify that when `220EVT` is not equal (`ne`) to `7054`, the "condition," the canopy cover layers should be set equal (`st`) to `1`, the "change." The edit rule syntax is explained in more depth in the [LFPS guide](https://lfps.usgs.gov/helpdocs/LFProductsServiceUserGuide.pdf).

*(How the API applies edit rules can be unintuitive. For example, if we used 'clear value' [`cv`] or set the value outside of 0-100 the edits we want would not work. To work around this behavior, we set the values to `1` since it is not found in the original data set.*)

```{r}
edit_rule <- list(c("condition","200EVT","ne",7054),
                  c("change", "200CC_19", "st", 1),
                  c("change", "220CC_22", "st", 1))
```

Note: Edits are performed in the order that they are listed and are limited to fuel theme products (i.e., Fire Behavior Fuel Model 13, Fire Behavior Fuel Model 40, Forest Canopy Base Height, Forest Canopy Bulk Density, Forest Canopy Cover, and Forest Canopy Height).

### Path

Finally, we will provide a path to a temporary zip file. Setting the path as a temp file is not strictly necessary because if `path` is left blank `landfireAPI()` will save the data to a temporary folder by default.

```{r}
path <- tempfile(fileext = ".zip")
```

### Call `landfireAPI()`

Now we are able to submit a request to the LANDFIRE Product Services API with the `landfireAPI()` function.

```{r, eval=FALSE}
resp <- landfireAPI(products = products,
                    aoi = aoi, 
                    projection = projection, 
                    resolution = resolution,
                    edit_rule = edit_rule,
                    path = path,
                    verbose = FALSE)
```

`landfireAPI()` will download your requested data into the folder provided in the path argument. If you did not provide one, you can find the path to your data in the `$path` element of the `landfire_api` object.

```{r, eval=FALSE}
resp$path
```

### Load and process LF data

```{r,include=FALSE}
path <- system.file("extdata/LFPS_Return.zip", package = "rlandfire")
```


The files returned by the LFPS API are compressed `.zip` files. We need to unzip the directory before reading the `.tif` file. Note: all additional metadata is included in this same directory.

```{r}
lf_dir <- file.path(tempdir(), "lf")
utils::unzip(path, exdir = lf_dir)

lf <- terra::rast(list.files(lf_dir, pattern = ".tif$", 
                             full.names = TRUE, 
                             recursive = TRUE))
```

Now we can reclassify the canopy cover layers to remove any values which are not classified as Ponderosa Pine, calculate the change, and plot our results.

```{r, fig.align = "center", fig.height = 5, fig.width = 7}
lf$US_200CC_19[lf$US_200CC_19 == 1] <- NA
lf$US_220CC_22[lf$US_220CC_22 == 1] <- NA

change <- lf$US_220CC_22 - lf$US_200CC_19

plot(change, col = rev(terrain.colors(250)),
     main = "Canopy Cover Loss - Calwood Fire (2020)",
     xlab = "Easting",
     ylab = "Northing")
plot(boundary$geometry, add = TRUE, col = NA,
     border = "black", lwd = 2)
```

### Citation

Visit the [LANDFIRE webpage](https://landfire.gov/landfire_citation.php) for information on citing LANDFIRE data layers. The package citation information can be viewed with `citation("rlandfire")`.
