terra::classify 保留切点并将其用作 terra::as.polygon 中的类名

发布于 2025-01-08 23:45:20 字数 1507 浏览 2 评论 0原文

下面是一些显示问题的代码:

1 -- 重新分类栅格

建立类限制向量

class.limits

#[1] 45.16490 50.12986 51.40991 52.68021 54.00000 55.42731 57.71864 60.14900 69.57393

使用这些进行分类

class(sg.crop)
#[1] "SpatRaster"
#attr(,"package")
#[1] "terra"

sg.class <- terra::classify(sg.crop, rcl=class.limits)

查看重新分类的值:

table(values(sg.class))

#  0    1    2    3    4    5    6    7
#747 1164 1054  829  607  772  770  937

到目前为止,一切都很好...但请注意:

levels(sg.class$class)

#[[1]]
#[1] "(45.164902–50.12986]"  "(50.12986–51.409912]"  "(51.409912–52.68021]"  "(52.68021–54]"         "(54–55.427315]" 
#[6] "(55.427315–57.718636]" "(57.718636–60.148998]" "(60.148998–69.573929]"

类限制保持为 并且,尝试通过分配给该向量来更改名称是行不通的:

levels(sg.class$class)[[1]] <- 1:8
#Error: [subset] undefined layer(s) selected: deepcopy

我不知道这意味着什么。但问题来了:

2 - 多边形化分类地图

sg.poly <- terra::as.polygons(sg.class, values = TRUE, dissolve=TRUE)
values(sg.poly)
#                 class    
#1  (45.164902–50.12986]
#2  (50.12986–51.409912]
#3  (51.409912–52.68021]
#4         (52.68021–54]
#5        (54–55.427315]
#6 (55.427315–57.718636]
#7 (57.718636–60.148998]
#8 (60.148998–69.573929]

所以现在多边形栅格的标签是类限制,而不是类,这是返回的值

terra::classify.

我不知道如何改变这些。在我看来,类标签,而不是切点值,应该是多边形的值。我相当肯定,大约一年前,当我第一次进行此分析时,情况就是这样。

Here is some code to show the issue:

1 -- reclassify a raster

establish a vector of class limits

class.limits

#[1] 45.16490 50.12986 51.40991 52.68021 54.00000 55.42731 57.71864 60.14900 69.57393

use these to classify

class(sg.crop)
#[1] "SpatRaster"
#attr(,"package")
#[1] "terra"

sg.class <- terra::classify(sg.crop, rcl=class.limits)

see the reclassified values:

table(values(sg.class))

#  0    1    2    3    4    5    6    7
#747 1164 1054  829  607  772  770  937

So far, so good... but notice:

levels(sg.class$class)

#[[1]]
#[1] "(45.164902–50.12986]"  "(50.12986–51.409912]"  "(51.409912–52.68021]"  "(52.68021–54]"         "(54–55.427315]" 
#[6] "(55.427315–57.718636]" "(57.718636–60.148998]" "(60.148998–69.573929]"

The class limits are kept as reference. And, trying to change the names by assigning to this vector does not work:

levels(sg.class$class)[[1]] <- 1:8
#Error: [subset] undefined layer(s) selected: deepcopy

I don't know what this means. But here comes the problem:

2 - Polygonize the classified map

sg.poly <- terra::as.polygons(sg.class, values = TRUE, dissolve=TRUE)
values(sg.poly)
#                 class    
#1  (45.164902–50.12986]
#2  (50.12986–51.409912]
#3  (51.409912–52.68021]
#4         (52.68021–54]
#5        (54–55.427315]
#6 (55.427315–57.718636]
#7 (57.718636–60.148998]
#8 (60.148998–69.573929]

So now the polygonized raster has labels that are the class limits, not the classes, which were the values returned by

terra::classify.

And I can not figure out how to change these. It seems to me that the class labels, not the cutpoint values, should be the values of the polygons. And I am fairly sure it was this way about a year ago when I first ran this analysis.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2025-01-15 23:45:20

以下是一些示例数据(来自 ?classify

library(terra)
r <- rast(ncols=10, nrows=10, names="test")
values(r) <- (0:99)/99
breaks <- c(0, 0.25, 0.5, 1)
x <- classify(r, breaks, include.lowest=TRUE)

您可以创建多边形并添加感兴趣的变量

p <- as.polygons(x) 
p$breaks <- breaks[-1]
p$ID <- 1:nrow(p)

values(p)
#        test breaks ID
#1   [0–0.25]   0.25  1
#2 (0.25–0.5]   0.50  2
#3    (0.5–1]   1.00  3
 

您还可以从 SpatRaster 中删除类别,如下所示:

 levels(x) <- NULL
 as.polygons(x)
 #class       : SpatVector 
 #geometry    : polygons 
 #dimensions  : 3, 1  (geometries, attributes)
 #extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
 #coord. ref. : lon/lat WGS 84 
 #names       :  test
 #type        : <int>
 #values      :     0
 #                  1
 #                  2

Here is some example data (from ?classify)

library(terra)
r <- rast(ncols=10, nrows=10, names="test")
values(r) <- (0:99)/99
breaks <- c(0, 0.25, 0.5, 1)
x <- classify(r, breaks, include.lowest=TRUE)

You can create polygons and add variables of interest

p <- as.polygons(x) 
p$breaks <- breaks[-1]
p$ID <- 1:nrow(p)

values(p)
#        test breaks ID
#1   [0–0.25]   0.25  1
#2 (0.25–0.5]   0.50  2
#3    (0.5–1]   1.00  3
 

You can also remove the categories from the SpatRaster like this:

 levels(x) <- NULL
 as.polygons(x)
 #class       : SpatVector 
 #geometry    : polygons 
 #dimensions  : 3, 1  (geometries, attributes)
 #extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
 #coord. ref. : lon/lat WGS 84 
 #names       :  test
 #type        : <int>
 #values      :     0
 #                  1
 #                  2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文