FIESTA
OverviewThe R package, FIESTA
(Forest Inventory ESTimation and
Analysis) is a research estimation tool for analysts that work with
sample-based inventory data from the U.S. Department of Agriculture,
Forest Service, Forest Inventory and Analysis (FIA) Program to
accommodate: unique population boundaries, different evaluation time
periods, customized stratification schemes, non-standard variance
equations, integration of multi-scale remotely-sensed data and other
ancillary information, and interaction with other modeling and
estimation tools from CRAN R’s library of packages. FIESTA
contains a collection of functions that can access FIA databases,
summarize and compile plot and spatial data, and generate estimates with
associated sampling errors.
Functions are organized by type or objective and are named with a corresponding prefix:
Core Functions
DB
) - functions for querying and
extracting data from FIA’s national database.dat
) - functions for summarizing and
exploring FIA data.sp
) - functions for manipulating and
summarizing spatial data.Estimation Modules
GB
) - functions for FIA’s standard
‘Green-Book’ estimators.PB
) - functions for supplementary
photo-based estimators.SA
) - functions for integration with
available small area estimators (SAE).MA
) - functions for integration with
available Model-Assisted estimators.Analysis Tools
an
) - wrapper functions for
stream-lining estimation processes.FIESTA
spatial (sp
) toolsFIESTA
’s spatial tools allow for summarizing and
manipulating spatial data for use in FIESTA
’s estimation
modules.
FUNCTION | DESCRIPTION |
---|---|
spImportSpatial() | Imports a spatial layer to a sf object. |
spExportSpatial() | Exports a sf object to a spatial layer. |
spMakeSpatialPoints() | Generates an S4 SpatialPoints object from X/Y coordinates. |
spReprojectSpatial() | Reprojects a sf object. |
spClipPoint() | Subset points with a Spatial polygon layer. |
spClipPoly() | Subset Spatial polygons layer with another Spatial polygons layer. |
spClipRast() | Subset raster layer with a Spatial polygons layer. |
spExtractPoly() | Extracts point attribute values from Spatial polygons layer(s). |
spExtractRast() | Extracts point attribute values from raster layer(s). |
spGetXY() | Wrapper: Extracts XY coordinates and subsets to boundary. |
spGetPlots() | Wrapper: Extracts plot data and subsets to boundary. |
spGetAuxiliary() | Wrapper: Extracts point attribute values, area for estimation unit(s), and zonal statitics for strata predictor layers. |
spGetEstUnit() | Wrapper: Extracts point attribute values and area for estimation unit(s). |
spGetStrata() | Wrapper: Extracts point attribute values, area for estimation unit(s), and pixel counts for strata spatial layer. |
spUnionPoly() | Generates one Spatial polygons object from two Spatial polygons layers. |
spZonalRast() | Extracts summary statistics by polygon (i.e., zone) for a raster. |
The objective of this tutorial is to demonstrate how to use
FIESTA
’s spatial tools for manipulating and summarizing
spatial data. The examples use data from three inventory years of field
measurements in the state of Wyoming, from FIADB_1.7.2.00, last updated
June 20, 2018, downloaded on June 25, 2018 and stored as internal data
objects in FIESTA
.
Data Frame | Description |
---|---|
WYplt | WY plot-level data |
WYcond | WY condition-level data |
WYtree | WY tree-level data |
External data | Description |
---|---|
WYbighorn_adminbnd.shp | Polygon shapefile of WY Bighorn National Forest Administrative boundary* |
WYbighorn_districtbnd.shp | Polygon shapefile of WY Bighorn National Forest District boundaries** |
WYbighorn_forest_nonforest_250m.tif | GeoTIFF raster of predicted forest/nonforest (1/0) for stratification*** |
WYbighorn_dem_250m.img | Erdas Imagine raster of elevation change, in meters**** |
*USDA Forest Service, Automated Lands Program (ALP). 2018. S_USA.AdministrativeForest (). Description: An area encompassing all the National Forest System lands administered by an administrative unit. The area encompasses private lands, other governmental agency lands, and may contain National Forest System lands within the proclaimed boundaries of another administrative unit. All National Forest System lands fall within one and only one Administrative Forest Area.
**USDA Forest Service, Automated Lands Program (ALP). 2018. S_USA.RangerDistrict (http://data.fs.usda.gov/geodata/edw). Description: A depiction of the boundary that encompasses a Ranger District.
***Based on MODIS-based classified map resampled from 250m to 500m resolution and reclassified from 3 to 2 classes: 1:forest; 2:nonforest. Projected in Albers Conical Equal Area, Datum NAD27 (Ruefenacht et al. 2008). Clipped to extent of WYbighorn_adminbnd.shp.
****USGS National Elevation Dataset (NED), resampled from 30m resolution to 250m. Projected in Albers Conical Equal Area, Datum NAD27 (U.S. Geological Survey 2017). Clipped to boundary of WYbighorn_adminbnd.shp.
First, you’ll need to load the FIESTA
library:
library(FIESTA)
Next, you’ll have to load some external data from the
FIESTA
package and set up objects in your R
global environment:
# File names for external spatial data
<- system.file("extdata",
WYbhfn "sp_data/WYbighorn_adminbnd.shp",
package = "FIESTA")
<- system.file("extdata",
WYbhdistfn "sp_data/WYbighorn_districtbnd.shp",
package = "FIESTA")
<- "DISTRICTNA"
WYbhdist.att
<- system.file("extdata",
fornffn "sp_data/WYbighorn_forest_nonforest_250m.tif",
package = "FIESTA")
<- system.file("extdata",
demfn "sp_data/WYbighorn_dem_250m.img",
package = "FIESTA")
# Other spatial layers used for examples, extracted using the raster package, getData function.
# County-level boundaries for USA and subset for Wyoming (Note: must have internet connection)
<- raster::getData("GADM", country = "USA", level = 2)
USAco <- USAco[USAco$NAME_1 == "Wyoming",]
WYco
head(USAco@data)
Finally, you’ll need to set up an “outfolder”. This is just a file
path to a folder where you’d like FIESTA
to send your data
output. For this vignette, we have saved our outfolder file path as the
outfolder
object.
In the remainder of this vignette, we provide examples of using the
functions in the sp
portion of FIESTA
.
spImportSpatial()
The spImportSpatial
function imports a spatial layer
(e.g., ESRI Shapefile) to a simple feature (sf
) object.
## Import external data shapefiles
<- spImportSpatial(WYbhfn)
WYbh <- spImportSpatial(WYbhdistfn)
WYbhdist
## Display boundary
plot(sf::st_geometry(WYbhdist), border="blue")
plot(sf::st_geometry(WYbh), add=TRUE)
spExportSpatial()
The spExportSpatial
function exports an sf
object.
## Export Spatial Polygons layer to a shapefile
spExportSpatial(WYbh,
savedata_opts = list(out_dsn = "WYbh.shp",
outfolder = outfolder,
overwrite_dsn = TRUE)
)
spMakeSpatialPoints()
The spMakeSpatialPoints
function generates an
sf
points object with a defined projection. Note: The
coordinate reference system (crs) is: prj = "longlat"
and
datum = "NAD83"
.
head(WYplt)
<- spMakeSpatialPoints(xyplt = WYplt,
WYspplt xy.uniqueid = "CN",
xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
prj = "longlat",
datum = "NAD83")
head(WYspplt)
We can also use EPSG code with spMakeSpatialPoints
.
<- spMakeSpatialPoints(xyplt = WYplt,
WYspplt xy.uniqueid = "CN",
xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
xy.crs = 4269
) WYspplt
To view our output we can run the following code.
## Display output
plot(sf::st_geometry(WYbhdist))
plot(sf::st_geometry(WYspplt), add=TRUE)
## NOTE: To display multiple layers, all layers must be in the same coordinate system.
lapply(list(WYbh, WYbhdist, WYspplt), sf::st_crs)
And finally we export the spatial points to an outfolder.
<- spMakeSpatialPoints(xyplt = WYplt,
WYspplt xy.uniqueid = "CN",
xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
xy.crs = 4269,
exportsp = TRUE,
savedata_opts = list(
out_dsn = "spplt",
out_fmt = "shp",
outfolder = outfolder,
out_layer = "WYplots",
overwrite_layer = TRUE)
)
spReprojectVector()
The spReprojectVector
function reprojects a spatial
vector layer. Note: The layer must have a defined coordinate reference
system (test using sf::st_crs
).
::st_crs(WYspplt)
sf<- "+proj=utm +zone=12 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
prj <- spReprojectVector(layer = WYspplt,
WYspplt.utm12 crs.new = prj)
## Check results
::st_crs(WYspplt.utm12) sf
spClipPoint()
The spClipPoint
function subsets
SpatialPoints
or point file with a Spatial polygon
boundary.
## Get points within Bighorn National Forest boundary (project on the fly)
<- spClipPoint(xyplt = WYplt,
WYbhptslst uniqueid = "CN",
clippolyv = WYbh,
spMakeSpatial_opts=list(xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
prj = "longlat",
datum = "NAD83")
)
<- spClipPoint(xyplt = WYplt,
WYbhptslst uniqueid = "CN",
clippolyv = WYbh,
savedata = TRUE,
exportsp = TRUE,
spMakeSpatial_opts=list(xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
prj = "longlat",
datum = "NAD83"),
savedata_opts = list(outfolder=outfolder,
out_layer = "WYbh")
)
names(WYbhptslst)
<- WYbhptslst$clip_xyplt
WYbhspplt <- WYbhptslst$clip_polyv WYbhprj
Next we check and display the output:
WYbhsppltplot(sf::st_geometry(WYbhprj), border="red", lwd=2)
plot(sf::st_geometry(WYbhspplt), add=TRUE)
Note: If the projection of spplt is not the same as the points layer,
the points layer will be reprojected to the same projection as
clippolyv
(See notes in help file for more info).
Now we generate a sf object first, then clip sf
points
with the spClipPoint
function:
<- spMakeSpatialPoints(xyplt = WYplt,
WYspplt xy.uniqueid = "CN",
xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
xy.crs = 4269)
<- spClipPoint(xyplt = WYspplt,
WYbhptslst uniqueid = "CN",
clippolyv = WYbh)
We can also subset other tales with clipped points.
<- spClipPoint(xyplt = WYspplt,
WYbhptslst uniqueid = "CN",
clippolyv = WYbh,
othertabnms = c("WYcond", "WYtree"))
names(WYbhptslst)
Next, we can export clipped points.
## Export clipped points
spExportSpatial(WYbhspplt,
savedata_opts = list(out_layer = "WYbhpts",
outfolder = outfolder,
overwrite_layer = TRUE)
)
Finally, we can get points within Bighorn National Forest boundary (project on the fly) and save to an outfolder.
<- spClipPoint(xyplt = WYplt,
WYbhptslst uniqueid = "CN",
clippolyv = WYbh,
othertabnms = c("WYcond", "WYtree"),
exportsp = TRUE,
spMakeSpatial_opts=list(xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
prj = "longlat",
datum = "NAD83"),
savedata_opts = list(
outfolder = outfolder,
overwrite_layer = TRUE,
outfn.pre = "clip")
)names(WYbhptslst)
spClipPoly()
spClipPoly
function clips (intersects) a polygon vector
layer with another polygon vector layer.
We first subsect the USAco SpatialPolygons
layer with
WYbighorn
. Note: you must download USAco from the
raster
package. See the “Set Up” section above.
<- spClipPoly(polyv = USAco,
WYbhco clippolyv = WYbh)
We can now check and display WYbighorn_co
with
labels.
head(WYbhco)
plot(sf::st_geometry(WYbhco['NAME_2']), col = sf.colors(nrow(WYbhco)))
<- st_coordinates(st_centroid(st_geometry(WYbhco)))
coords text(coords[,"X"], coords[,"Y"], WYbhco[["NAME_2"]])
spClipRast()
The spClipRast
function subsets a raster to a polygon
extent or boundary.
First, we subset the strata layer with the Medicine Wheel district boundary
WYbhdist<- WYbhdist[WYbhdist$DISTRICTNA == "Medicine Wheel Ranger District",]
WYbhMW
plot(st_geometry(WYbhdist))
plot(st_geometry(WYbhMW), border="red", add=TRUE)
Next, we can clip the raster. Note: If the projection of
polyv
is not the same as rast
,
polyv
will by reprojected to the same projection as
rast
before clipping (See note in the help file for more
details).
<- spClipRast(fornffn,
WYbhMW.fornf clippolyv = WYbhMW,
outfolder = outfolder)
Finally, we display the results:
<- crsCompare(WYbhMW,
WYbhMWprj rasterInfo(WYbhMW.fornf)$crs)$x
::plot(raster::raster(WYbhMW.fornf))
rasterplot(st_geometry(WYbhMWprj),
border = "red",
add = TRUE)
spExtractPoly()
The spExtractPoly
function subsets a
SpatialPolygons
layer with another
SpatialPolygons
layer.
First, we extract polygon attributes from WYbighorn
to
WYpts
, keeping NULL
values. Note: If the
projection of spplt
is not the same as the
SpatialPoints
, the SpatialPoints
layer will be
reprojected to the same projection as clippolyv
before the
evaluation points (See the note in help file for more details).
<- spExtractPoly(WYspplt,
extpolylst xy.uniqueid = "CN",
polyvlst = WYbhdist)
<- extpolylst$spxyext
WYspplt_bh
dim(WYspplt)
dim(WYspplt_bh)
head(WYspplt_bh)
plot(WYspplt_bh["DISTRICTNA"], pch = 8)
Next we can extract a subset of polygon attributes from
WYbighorn
to WYpts
, not keeping
NULL
values.
<- spExtractPoly(WYspplt,
extpolylst xy.uniqueid = "CN",
polyvlst = WYbh,
polyvarlst = c("FORESTNUMB", "FORESTNAME"),
keepNA = FALSE)
<- extpolylst$spxyext
WYspplt_bh2
dim(WYspplt)
dim(WYspplt_bh2)
head(WYspplt_bh2)
spExtractRast()
The spExtractRast
function subsets a
SpatialPolygons
layer with another
SpatialPolygons
layer.
We can extract raster values from WYdem
to
WYpts
, not keeping NULL
values.
<- spExtractRast(WYspplt,
extrastlst rastlst = c(fornffn, demfn),
xy.uniqueid = "CN",
keepNA = FALSE)
<- extrastlst$sppltext
WYspplt_dem
dim(WYspplt)
dim(WYspplt_dem)
head(WYspplt_dem)
spGetAuxiliary()
The spGetAuxiliary
function extracts data and zonal
statistics for model-assisted or model-based (small area) estimation.
The major steps are as follows:
dunit_layer
domlayer
(if
areacalc = TRUE
)First we generate an sf
object from the WY plot data,
public coordinates (xvar = "LON_PUBLIC"
,
yvar = "LAT_PUBLIC"
). Note: the public coordinate
projection information is: prj = "longlat"
,
datum = "NAD83"
.
<- spMakeSpatialPoints(xyplt = WYplt,
WYspplt xy.uniqueid = "CN",
xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
xy.crs = 4269)
Next, we derive new layers from dem
.
library(raster)
<- raster(demfn)
dem <- raster::terrain(dem,
slp opt = "slope",
unit = "degrees",
filename = paste0(outfolder, "/WYbh_slp.img"),
overwrite = TRUE)
<- raster::terrain(dem,
asp opt = "aspect",
unit = "degrees",
filename = paste0(outfolder, "/WYbh_asp.img"),
overwrite = TRUE)
Finally, we extract estimation unit and zonal raster statistics (i.e., mean).
<- c(demfn, slp, asp)
rastlst.cont <- c("dem", "slp", "asp")
rastlst.cont.name <- fornffn
rastlst.cat <- "fornf"
rastlst.cat.name
<- spGetAuxiliary(xyplt = WYspplt,
modeldat uniqueid = "CN",
unit_layer = WYbhfn,
unitvar = NULL,
rastlst.cont = rastlst.cont,
rastlst.cont.name = rastlst.cont.name,
rastlst.cat = rastlst.cat,
rastlst.cat.name = rastlst.cat.name,
rastlst.cont.stat = "mean",
asptransform = TRUE,
rast.asp = asp,
keepNA = FALSE,
showext = FALSE,
savedata = FALSE)
names(modeldat)
<- modeldat$pltassgn
pltassgn <- modeldat$unitzonal
unitzonal <- modeldat$dunitvar
unitvar <- modeldat$inputdf
inputdf <- modeldat$unitarea
unitarea <- modeldat$areavar
areavar <- modeldat$inputdf
inputdf <- modeldat$prednames
prednames <- modeldat$zonalnames
zonalnames
unitvar
areavar
unitzonal
unitareahead(pltassgn)
prednames zonalnames
spGetXY()
The spGetXY
function extracts XY data within a given
boundary.
We can extract public coordinates within WY Bighorn NF boundary, returning spatial (spxy) and nonspatial plot identifiers (pltids).
<- spGetXY(bnd = WYbhfn,
WYbhxy xy_datsource = "datamart",
evalCur = TRUE,
returnxy = TRUE)
names(WYbhxy)
<- WYbhxy$pltids
pltids head(pltids)
<- WYbhxy$spxy
spxy plot(st_geometry(spxy))
Or, returning only nonspatial plot identifiers (pltids).
<- spGetXY(bnd = WYbhfn,
WYbhxyids xy_datsource = "datamart",
evalCur = TRUE,
returnxy = FALSE)
names(WYbhxyids)
<- WYbhxyids$pltids
pltids head(pltids)
spGetPlots()
The spGetPlots
function extracts plot data within a
given boundary.
We can extract public coordinates within WY Bighorn NF boundary.
<- spGetPlots(bnd = WYbhfn,
WYbhdat states = "Wyoming",
datsource = "datamart",
evalCur = TRUE,
istree = FALSE)
names(WYbhdat)
spGetEstUnit()
The spGetEstUnit
function extracts estimation unit
values to plots and calculate area by estimation unit.
First, we create a SpatialPoints
object from
WYplt
<- spMakeSpatialPoints(xyplt = WYplt,
WYspplt xy.uniqueid = "CN",
xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
xy.crs = 4269)
<- WYspplt xyplt
We now get estimation unit acres for Bighorn National Forest.
<- spGetEstUnit(xyplt = WYplt,
unitdat.bh uniqueid = "CN",
unit_layer = WYbhfn,
spMakeSpatial_opts=list(xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
prj = "longlat",
datum = "NAD83")
)
names(unitdat.bh)
<- unitdat.bh$unitarea
unitarea.bh <- unitdat.bh$unitvar
unitvar.bh <- unitdat.bh$areavar
areavar.bh
unitarea.bh
unitvar.bh areavar.bh
We now get estimation unit acres and strata information for Bighorn National Forest.
<- spGetEstUnit(xyplt = WYplt,
unitdat.bhdist uniqueid = "CN",
unit_layer = WYbhdistfn,
unitvar = "DISTRICTNA",
spMakeSpatial_opts=list(xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
prj = "longlat",
datum = "NAD83")
)
names(unitdat.bhdist)
<- unitdat.bhdist$unitarea
unitarea.bhdist <- unitdat.bhdist$unitvar
unitvar.bhdist <- unitdat.bhdist$areavar
areavar.bhdist
unitarea.bhdist
unitvar.bhdist areavar.bhdist
spGetStrata()
The spGetStrata
function is a wrapper to extract
attribute and area from a polygon or raster estimation unit layer and a
polygon or raster layer with strata pixel categories.
First, we generate an sf
object from the WY plot data,
public coordinates (xvar = "LON_PUBLIC"
,
yvar = "LAT_PUBLIC"
). Note: The public coordinate
projection information is: prj = "longlat"
,
datum = "NAD83"
.
<- spMakeSpatialPoints(xyplt = WYplt,
WYspplt xy.uniqueid = "CN",
xvar = "LON_PUBLIC",
yvar = "LAT_PUBLIC",
xy.crs = 4269)
Next, we extract polygon attributes from WYbighorn
to
WYpts
, keeping NULL
values.
<- spGetStrata(WYspplt,
stratlst uniqueid = "CN",
unit_layer = WYbhfn,
strattype = "RASTER",
strat_layer = fornffn)
names(stratlst)
$stratalut stratlst
spUnionPoly()
The spUnionPoly
function generates a unioned
sf
object with polygons and attributes from two
sf
polygon objects.
First, we import spatial data with spImportSpatial
, and
then extract polygon attributes from WYbighorn
to
WYpts
, keeping NULL
values.
<- spImportSpatial(WYbhfn)
WYbh
<- spUnionPoly(polyv1 = USAco[USAco$NAME_1 == "Wyoming",],
polyUnion polyv2 = WYbh,
areacalc=TRUE)
plot(st_geometry(polyUnion))
head(polyUnion)
spZonalRast()
The spZonalRast
function extracts summary statistics by
polygon (i.e., zone).
First, we import spatial data with spImportSpatial
, and
then extract polygon attributes from WYbighorn
to
WYpts
, keeping NULL
values.
<- spImportSpatial(WYbhdistfn)
WYbhdist
<- spZonalRast(polyv = WYbhdist,
zonallst polyv.att = "DISTRICTNA",
rastfn = demfn,
zonalstat = c("mean", "sum"))
names(zonallst)
<- zonallst$zonalext
zonalext <- zonallst$outname
outname <- zonallst$rasterfile
rasterfile
head(zonalext)
outname rasterfile