英文:
Reclassify a raster tif to unique values
问题
我有一个栅格tif文件,其中包含唯一的值:`0,2,4,5,9,111,114,115,116,124,125,126`。
我需要将我的栅格单元值重新分类为:
`0,2,4,5,9,4,4,4,4,4,4,4`。
我使用了以下代码:
is<-c(0,2,4,5,9,111,114,115,116,124,125,126)
become<-c(0,2,4,5,9,4,4,4,4,4,4,4)
recMat<-matrix(c(is,become),ncol=2,nrow=12)
LU_AOI_2 <- reclassify(LU_AOI, recMat)# 重新分类
重新分类完成后,当我绘制我的栅格时,我仍然看到浮点值。我需要一个只包含这些唯一值`(0,2,4,5,9,4,4,4,4,4,4,4)`的栅格。
我的代码有什么问题?为什么我不能创建一个只包含唯一整数值的栅格?
请帮帮我。
谢谢
英文:
I have a raster tif file with unique values: 0,2,4,5,9,111,114,115,116,124,125,126
. <br>
I need to reclassify my raster cell values to be:
0,2,4,5,9,4,4,4,4,4,4,4
. <br>
I used the following codes:
is<-c(0,2,4,5,9,111,114,115,116,124,125,126)
become<-c(0,2,4,5,9,4,4,4,4,4,4,4)
recMat<-matrix(c(is,become),ncol=2,nrow=12)
LU_AOI_2 <- reclassify(LU_AOI, recMat)# Reclassify
After the reclassification is done, when I plot my raster, I still see floating values. I need a raster with those unique values (0,2,4,5,9,4,4,4,4,4,4,4)
only.
What is wrong with my codes? Why can I not make a raster with unique integer values?
Please help me.
Thanks
答案1
得分: 0
示例数据
```R
library(terra)
is <- c(0,2,4,5,9,111,114,115,116,124,125,126)
become <- c(0,2,4,5,9,4,4,4,4,4,4,4)
set.seed(1)
r <- rast(nrow=10, ncol=10, vals=sample(is, 100, replace=TRUE))
两种解决方案:
x <- subst(r, is, become)
y <- classify(r, cbind(is, become))
unique(x)
# lyr.1
#1 0
#2 2
#3 4
#4 5
#5 9
plot(x)
如果这在您的数据中不起作用,那么可能输入值不是整数。在这种情况下,您可以使用“从-到-变成”矩阵,或者将输入数据四舍五入。
x <- subst(round(r), is, become)
<details>
<summary>英文:</summary>
Example data
library(terra)
is <- c(0,2,4,5,9,111,114,115,116,124,125,126)
become <- c(0,2,4,5,9,4,4,4,4,4,4,4)
set.seed(1)
r <- rast(nrow=10, ncol=10, vals=sample(is, 100, replace=TRUE))
Two solutions:
x <- subst(r, is, become)
y <- classify(r, cbind(is, become))
unique(x)
lyr.1
#1 0
#2 2
#3 4
#4 5
#5 9
plot(x)
[![enter image description here][1]][1]
If that does not work with your data, then perhaps the input values are not whole numbers. In that case, you can either use a "from-to-becomes" matrix, or round your input data.
x <- subst(round(r), is, become)
[1]: https://i.stack.imgur.com/dCFuw.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论