r语言 - 在栅格上执行空间操作时,terra将INT1U范围以外的值替换为NA



我正在使用landfire.gov上的生物物理设置栅格数据集。当我最初使用terra::rast()读取数据集时,似乎一切正常。但是,当我尝试使用多边形裁剪数据时,范围0-255之外的所有值都将被NA替换。如果我尝试将这个栅格投影到一个新的坐标参考系,那么超出这个范围的值也会被丢弃。谁能解释一下为什么这个栅格被限制为数据类型INT1U的值,以及我如何绕过这一点?

下面我提供了一个可重复的代码示例,说明我是如何处理这个数据集的。这个例子依赖于两个公共数据集:

  1. 来自Landfire.gov的生物物理设置
  2. 北卡罗莱纳州的州边界从nconemap.gov(下载为shapefile)
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)

将生物物理设置栅格裁剪到北卡罗来纳边界后,警告"检测到数据类型int1u"限制之外的值"。显示。

我尝试使用terraOptions()将默认数据类型设置为INT2S,但无济于事。如果有人能解释为什么会发生这种情况,以及我如何纠正它,那将是伟大的!

更新:

现有植被高度代码:

# 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")

生物物理设置代码:

# 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")

您使用的是当前版本的" terra"吗?我问是因为这对我来说很好:

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)

最新更新