【问题标题】:When performing spatial operations on raster, terra is replacing values outside of INT1U range with NA对栅格执行空间操作时,terra 会将 INT1U 范围之外的值替换为 NA
【发布时间】:2022-12-15 01:39:38
【问题描述】:

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

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

  1. Biophysical Settings from Landfire.gov
  2. North Carolina state boundary from 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")
    
    

【问题讨论】:

    标签: r spatial terra


    【解决方案1】:

    您使用的是当前版本的“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)
    

    【讨论】:

    • 啊,我想可能是这样。对于那个很抱歉。非常感谢您的回复!
    • 所以我继续进行了一些测试,发现这个问题仍然存在于 Landfire 的其他数据中。在本例中,它是 Existing Vegetation Height 栅格图层。我已尽力复制并应用您的代码示例。它适用于生物物理设置,但是当我尝试将现有植被高度投影到 NC State Plane (epsg:2264) 时,会出现警告消息,并且所有 >200 的值都将替换为 NA。 terra 版本:terra_1.6-47 R 版本:4.2.1 我将使用新的代码示例编辑原始问题。非常感谢!
    猜你喜欢
    • 2022-08-03
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 2022-07-13
    • 2022-01-23
    相关资源
    最近更新 更多