[英]Terra extracting incorrect value from categorical raster
I am attempting to update old code I have where I previously used raster to terra.我正在尝试更新我以前使用栅格到 terra 的旧代码。 I encounter an issue extracting dominant vegetation type from a LANDFIRE raster layer.我在从 LANDFIRE 栅格层中提取主要植被类型时遇到问题。 In ArcGIS I created a new column titled "CONDENSED" for my condensed vegetation types (ie, 1="Aspen", 2="Other", etc.)and this is the active category I would like to extract from.在 ArcGIS 中,我为我的密集植被类型(即 1="Aspen"、2="Other" 等)创建了一个名为“CONDENSED”的新列,这是我想从中提取的活动类别。 I have 8 categories.我有8个类别。 For some reason, the extracted value is one category "higher" than it should be.出于某种原因,提取的值比应有的值“高”了一个类别。 For example, a point should have a value of 1 for aspen, but the extracted value is 2/Other.例如,对于 aspen,一个点的值应为 1,但提取的值为 2/Other。 This is consistent across all veg types and doesn't matter if my points are sf or SpatVector.这在所有蔬菜类型中都是一致的,无论我的点是 sf 还是 SpatVector。 I am using terra version 1.4.11.我正在使用 terra 版本 1.4.11。
I have tried creating a reproducible example from scratch and it works perfectly fine when compared to values from raster::extract()
, so I'm not sure if the issue has to do with how I'm specifying the active layer?我尝试从头开始创建一个可重现的示例,与来自raster::extract()
值相比,它工作得非常好,所以我不确定问题是否与我如何指定活动层有关? I have uploaded a small sample of the raster and points I am working with here: https://github.com/Cara-Thompson/Elk-resource-selection我已经上传了我在这里使用的栅格和点的小样本: https : //github.com/Cara-Thompson/Elk-resource-selection
Anyone know what might be going on?有谁知道可能会发生什么? Below is the description of my SpatRaster and my steps.下面是我的 SpatRaster 和我的步骤的描述。
BONUS: I noticed terra::extract()
works with sf objects in full form when a Raster* object is used.奖励:当使用 Raster* 对象时,我注意到terra::extract()
以完整形式处理 sf 对象。 So I can get it to extract correctly not using SpatVect/SpatRast formats, but is this any faster than using raster::extract()
?所以我可以让它在不使用 SpatVect/SpatRast 格式的情况下正确提取,但这比使用raster::extract()
更快吗?
# Read in raster and define crs
Veg <- terra::rast("./Vegetation/veg_cond.tif")
crs(Veg) <- "EPSG:4326"
#> Veg
# class : SpatRaster
# dimensions : 9401, 21368, 1 (nrow, ncol, nlyr)
# resolution : 0.0003398576, 0.0003398576 (x, y)
# extent : -113.7326, -106.4705, 32.44176, 35.63676 (xmin, xmax, ymin, ymax)
# coord. ref. : lon/lat WGS 84 (EPSG:4326)
# source : veg_cond.tif
# categories : COUNT, CONDENSED
# name : COUNT
# min value : 135890
# max value : 77959803
# Make the condensed category the active category so count isn't extracted
activeCat(Veg) <- is.factor("CONDENSED")
# Extract
Values <- terra::extract(Veg, st_coordinates(EP.sf))
# Verify using raster::extract()
Veg2 <- raster(Veg)
Values$raster <- raster::extract(Veg2, EP.sf)
This is a bug related to the difference between ESRI VAT and GDAL categories (see this issue ) that now seems fixed.这是一个与 ESRI VAT 和 GDAL 类别之间的差异相关的错误(请参阅此问题),现在似乎已修复。 I now get我现在得到
library(terra)
#terra version 1.4.12
elk <- vect("elk subset/elk subset.shp")
veg <- rast("veg_sample/veg sample.tif")
# note the way to use activeCat!
activeCat(veg) <- 2
veg
#class : SpatRaster
#dimensions : 933, 1272, 1 (nrow, ncol, nlyr)
#resolution : 0.0003398576, 0.0003398576 (x, y)
#extent : -109.554, -109.1217, 33.8277, 34.14478 (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 (EPSG:4326)
#source : veg sample.tif
#categories : COUNT, CONDENSED
#name : CONDENSED
#min value : ASPEN
#max value : PONDEROSA
plot(veg, mar=c(2,2,2,8))
points(elk)
head(extract(veg, elk))
# ID CONDENSED
#1 1 ASPEN
#2 2 OAK/SHRUBLAND
#3 3 OAK/SHRUBLAND
#4 4 PINYON-JUNIPER
#5 5 PINYON-JUNIPER
#6 6 PONDEROSA
e <- ext(-109.18688, -109.18483, 34.11579, 34.11798)
cveg <- crop(veg, e)
activeCat(cveg) <- 2
celk <- crop(elk, e)
plot(cveg, mar=c(2,2,2,8))
points(celk)
extract(veg, celk)
# ID CONDENSED
#1 1 OAK/SHRUBLAND
You can install the development version with您可以安装开发版本
install.packages('terra', repos='https://rspatial.r-universe.dev')
BONUS:奖金:
extract
is a generic function. extract
是一个通用函数。 This means that there is no difference between calling terra::extract(x)
or raster::extract(x)
because the version of the function you get depends on the class of x
.这意味着调用terra::extract(x)
或raster::extract(x)
之间没有区别,因为您获得的函数版本取决于x
的类。 So if you do terra::extract(x)
and x
is a RasterLayer, you get the function that is defined in the raster package.因此,如果您执行terra::extract(x)
并且x
是 RasterLayer,您将获得在 raster 包中定义的函数。 In this case it is therefore better just to use extract(x)
--- as that would not be misleading.在这种情况下,最好只使用extract(x)
--- 因为这不会产生误导。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.