如何使用类别名称对 terra 中的分类栅格进行重新分类

发布于 2025-01-10 13:13:11 字数 481 浏览 6 评论 0原文

我希望能够合并分类栅格中的两个类别。到目前为止我找到的唯一解决方案使用级别索引号,而不是类别名称。我如何使用类别名称来做到这一点?

library(terra)
m <- matrix(rep(c("a", "b", "c"), each = 3), nrow = 3, ncol = 3)
x <- rast(m)

x[x$lyr.1 == "c"]

m2 <- matrix(c("a", "a", "b", "b", "c", "b"), nrow = 3, ncol = 2, byrow = TRUE)

test <- classify(x, m2)
#doesn't work with category names

test <- subst(x, "c", "b")
#doesn't work with category names

test <- subst(x, 2, 1)
#works with category index

I'd like to be able to merge two categories in a categorical raster. The only solution I've figured out so far uses the level index number, not the name of the category. How could I do this using the name of the category?

library(terra)
m <- matrix(rep(c("a", "b", "c"), each = 3), nrow = 3, ncol = 3)
x <- rast(m)

x[x$lyr.1 == "c"]

m2 <- matrix(c("a", "a", "b", "b", "c", "b"), nrow = 3, ncol = 2, byrow = TRUE)

test <- classify(x, m2)
#doesn't work with category names

test <- subst(x, "c", "b")
#doesn't work with category names

test <- subst(x, 2, 1)
#works with category index

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

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

发布评论

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

评论(2

铜锣湾横着走 2025-01-17 13:13:11

示例数据

library(terra)
m <- matrix(rep(c("a", "b", "c"), each = 3), nrow = 3, ncol = 3)
x <- rast(m)
m2 <- matrix(c("a", "a", "b", "b", "c", "b"), nrow = 3, ncol = 2, byrow = TRUE)

对于当前版本的 terra,您可以执行以下任一

test1 <- subst(x, "c", "b")

操作:

test2 <- subst(x, 2, 1, raw=TRUE)

Example data

library(terra)
m <- matrix(rep(c("a", "b", "c"), each = 3), nrow = 3, ncol = 3)
x <- rast(m)
m2 <- matrix(c("a", "a", "b", "b", "c", "b"), nrow = 3, ncol = 2, byrow = TRUE)

With the current version of terra you can do either

test1 <- subst(x, "c", "b")

or

test2 <- subst(x, 2, 1, raw=TRUE)
挥剑断情 2025-01-17 13:13:11
library(terra)
library(tidyverse)

m <- matrix(rep(c("a", "b", "c"), each = 3), nrow = 3, ncol = 3)
x <- rast(m)

plot(x)

plot( x) 重新分类后

reclassified <- cats(x)[[1]] %>%
  mutate(label_reclass = forcats::fct_collapse(cats(x)[[1]]$label,c="b"))

x <- categories(x, layer=1, value=reclassified, active=2)

图(x)

plot(x) 之后

levels(x)

[[1]]
  value label_reclass
1     1             a
2     2             c
3     3             c
library(terra)
library(tidyverse)

m <- matrix(rep(c("a", "b", "c"), each = 3), nrow = 3, ncol = 3)
x <- rast(m)

plot(x)

plot(x) after reclass

reclassified <- cats(x)[[1]] %>%
  mutate(label_reclass = forcats::fct_collapse(cats(x)[[1]]$label,c="b"))

x <- categories(x, layer=1, value=reclassified, active=2)

plot(x)

plot(x) after

levels(x)

[[1]]
  value label_reclass
1     1             a
2     2             c
3     3             c
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文