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
Set up a new R-Studio project, i.e. “r_landfire_demos”
Create two directories, “inputs” and “outputs”
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
Extract the “bps_demo.zip” files into the “inputs” sub-directory of your r-studio project.
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 packageslibrary(foreign)library(rlandfire)library(sf)library(terra)library(tidyverse)# read in Area of Interest (aoi) shapefile, plot to checkshp <-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 onceprojection <-5070resolution <-30email <-"your_email@wah.org"# REPLACE WITH YOUR E-MAIL ADDRESS PLEASE! # R specific argumentssave_file <-tempfile(fileext =".zip")# call APIncal <-landfireAPIv2( products, aoi, projection, resolution, path = save_file,email = email)# define the destination pathdest_file <-file.path("inputs", "landfire_data.zip")# move and rename the filefile.rename(save_file, dest_file)# create a temporary directory for unzippingtemp_dir <-tempfile()dir.create(temp_dir)# unzip the file into the temporary directoryunzip(dest_file, exdir = temp_dir)# get the list of unzipped filesunzipped_files <-list.files(temp_dir, full.names =TRUE)# rename each unzipped file to "landfire_data" with its full original extensionfor (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 directoryunlink(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!