LANDFIRE R Toolkit
  • Home
  • Download data
  • Prepare rasters
  • Make charts
  • Make hex map
  • Process events data
  • Compiled code
  • About
  • LANDFIRE Logo

On this page

  • Skills learned
  • Datasets used
  • Set up
    • Code to load packages and inputs
  • Download manage and load data
  • Next steps
  • More resources

Download LANDFIRE data into R

Skills learned

Use this page to:

  • Download and prepare LANDFIRE data using the rlandfire package (developed by Mark Buckner), including setting up a project environment and managing spatial data files

  • Define an area of interest, interact with the LANDFIRE API, and organize downloaded data for future analysis

Datasets used

  • LANDFIRE Biophysical Settings (BpS)
  • Existing Vegetation Type (EVT)

Set up

To run the code below you will need to:

  1. Set up a new R-Studio project, i.e. “r_landfire_demos”
  2. Create two directories, “inputs” and “outputs”
  3. Download “bps_demo.zip” (clicking will initiate the download that will likely land in your “C:” directory) which contains:
    • LANDFIRE Map Zone 35 shapefile
    • BpS attributes file as a .csv
  4. Extract the “bps_demo.zip” files into the “inputs” sub-directory of your r-studio project.
  5. Create and save a new r-script with a name such as “bps_demo_code”.

Once this set up is complete, you should be able to copy/paste the code below into the r script you created above.

Code to load packages and inputs

# install packages if needed
  # install.packages("foreign")
  # install.packages("rlandfire")
  # install.packages("sf")
  # install.packages("terra")
  # install.packages("tidyverse")

# load packages

library(foreign)
library(rlandfire)
library(sf)
library(terra)
library(tidyverse)


# read in Area of Interest (aoi) shapefile, plot to check

shp <- st_read("inputs/map_zone_35.shp", quiet = TRUE) %>% 
  st_transform(crs = 5070) %>%
  st_union() %>%
  st_sf()




# plot the shape for fun (not much to look at on it's own!)
plot(shp)

Download manage and load data

To obtain and manage LANDFIRE data we need to:

  • Define the Area of Interest (AOI): The code gets the area of interest from a shapefile.
  • Set Parameters for the API Call: It specifies the product code, coordinate system (EPSG:4326), resolution (30 meters), and your email address for the API.
  • Create a Temporary File: A temporary file is created to save the downloaded data.
  • Call the LANDFIRE API: The API is called with the specified parameters, and the data is saved to the temporary file.
  • Make sure the downloaded data lands in a place where you can find and use it easily
    • Define the Destination Path: The destination path for the downloaded file is set.
    • Move and Rename the File: The temporary file is moved to the destination path and renamed.
    • Create a Temporary Directory for Unzipping: A temporary directory is created to unzip the downloaded file.
    • Unzip the File: The file is unzipped into the temporary directory.
    • Get the List of Unzipped Files: A list of unzipped files is obtained.
    • Rename Each Unzipped File: Each unzipped file is renamed to “landfire_data” followed by its original extension.
    • Clean Up the Temporary Directory: The temporary directory and its contents are deleted.

The code below does that in roughly 2 minutes with an internet download speed of ~450mbs. Your times may vary.

aoi <- getAOI(shp)

products <-  c("200BPS", "240EVT")  # note, you can download multiple datasets at once
projection <- 5070  
resolution <- 30
email <- "your_email@wah.org" # REPLACE WITH YOUR E-MAIL ADDRESS PLEASE! 

# R specific arguments
save_file <- tempfile(fileext = ".zip")

# call API
ncal <- landfireAPIv2(
  products, 
  aoi, 
  projection, 
  resolution, 
  path = save_file,
  email = email)


# define the destination path
dest_file <- file.path("inputs", "landfire_data.zip")

# move and rename the file
file.rename(save_file, dest_file)

# create a temporary directory for unzipping
temp_dir <- tempfile()
dir.create(temp_dir)

# unzip the file into the temporary directory
unzip(dest_file, exdir = temp_dir)

# get the list of unzipped files
unzipped_files <- list.files(temp_dir, full.names = TRUE)

# rename each unzipped file to "landfire_data" with its full original extension
for (file in unzipped_files) {
  file_name <- basename(file)
  file_extension <- sub("^[^.]*", "", file_name)  # Extract the full extension
  new_file_path <- file.path("inputs", paste0("landfire_data", file_extension))
  file.rename(file, new_file_path)
}

# clean up the temporary directory
unlink(temp_dir, recursive = TRUE)

Next steps

You should see a set of files with “landfire” in the name. Next go to the Prepare rasters page to learn how to load them into R, clip/mask and view them.

More resources

  • Mark Buckner’s rlandfire site also has a useful tutorial.
  • There is also a Python package for downloading LANDFIRE data, landfire-python. If you try this please let us know!



Logo 3 Logo 2 Logo 1 Logo 3