Reputation: 170
I'm working with the Biophysical settings raster data set from landfire.gov. When I initially read in the data set with terra::rast(), everything appears to be working. However, when I attempt to crop the data using a polygon, all values outside of the range 0-255 are replaced with NA. Values outside of this range are also dropped if I attempt to project this raster to a new coordinate reference system. Could anyone explain why this raster is being limited to values of the data type INT1U and how I might bypass this?
Below I provide a reproducible code example of how I've attempted to process this data set. This example is dependent on two public data sets:
library(terra)
library(dplyr)
library(sf)
# Establish paths to required files.
# ** These will need to be replaced with your local paths
bpsDirPath <- "./dataRaw/envTerr/LF2020_BPS_220_CONUS/"
ncBoundaryPath <- "C:/Users/Eliot-KDV/Desktop/NCDOT_State_Boundary/NCDOT_State_Boundary.shp"
# Read in biophysicall setting raster data
bpsRaw <- terra::rast(paste0(bpsDirPath, "Tif/LC20_BPS_220.tif"))
# Read in codebook for bps categories
codeBook <- read.csv(paste0(bpsDirPath, "CSV_Data/LF20_BPS_220.csv"))
# Read in North Carolina state boundary
ncBoundary <- read_sf(ncBoundaryPath)
# Set levels of biophysical setting to category names provided in codebook instead
# of category codes. This step is unnecessary but makes plot more readable
levels(bpsRaw) <- dplyr::select(codeBook, VALUE, BPS_NAME)
# Take a look before any spatial operations, note that North Carolina countains
# numerous different levels
plot(bpsRaw)
# Transform ncBoundary to epsg:5070 so bps and ncBoundary share the same CRS
ncBoundary <- st_transform(ncBoundary, "epsg:5070")
# Crop bps to north carolina boundary
bpsNc <- terra::crop(bpsRaw, vect(ncBoundary), mask = TRUE)
# Look after cropping to NC boundary, now it only contains Open Water and
# Barren-Rock/Sand/Clay
plot(bpsNc)
After cropping the biophysical setting raster to the North Carolina boundary the warning "detected values outside of the limits of datatype INT1U" is displayed.
I've attempted using terraOptions() to set the default datatype to INT2S to no avail. If anyone could explain why this is happening and how I could correct it, that would be great!
Update:
Code for Existing Vegetation Height:
# This does not work as expected
library(terra)
evhRaw <- terra::rast("./dataRaw/envTerr/LF2022_EVH_220_CONUS/Tif/LC22_EVH_220.tif")
nc <- terra::vect("./dataTidy/cadastral/NCDOT_State_Boundary/NCDOT_State_Boundary.shp")
ncp <- project(nc, evhRaw)
evhNc <- terra::crop(evhRaw, ncp, mask = TRUE)
# This is where the issue occurs
evhNcPlane <- terra::project(evhNc, "epsg:2264")
Code for Biophysical Setting:
# This code works as expected
library(terra)
bpsRaw <- terra::rast("./dataRaw/envTerr/LF2020_BPS_220_CONUS/Tif/LC20_BPS_220.tif")
nc <- terra::vect("./dataTidy/cadastral/NCDOT_State_Boundary/NCDOT_State_Boundary.shp")
ncp <- project(nc, bpsRaw)
bpsNc <- terra::crop(bpsRaw, ncp, mask = TRUE)
bpsNcPlane <- terra::project(bpsNc, "epsg:2264")
Upvotes: 2
Views: 519
Reputation: 47536
Are you using the current version of "terra"? I am asking because this works well for me:
library(terra)
bpsRaw <- terra::rast("./LF2020_BPS_220_CONUS/Tif/LC20_BPS_220.tif"))
## this is how you change the category of interest
activeCat(bpsRaw) <- "BPS_NAME"
ncp <- project(nc, bpsRaw)
bpsNc <- terra::crop(bpsRaw, ncp, mask = TRUE)
Upvotes: 1