重新分类栅格tif为唯一数值

huangapple go评论66阅读模式
英文:

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&lt;-c(0,2,4,5,9,111,114,115,116,124,125,126)
become&lt;-c(0,2,4,5,9,4,4,4,4,4,4,4)
recMat&lt;-matrix(c(is,become),ncol=2,nrow=12)
LU_AOI_2 &lt;- 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)

重新分类栅格tif为唯一数值

如果这在您的数据中不起作用,那么可能输入值不是整数。在这种情况下,您可以使用“从-到-变成”矩阵,或者将输入数据四舍五入。

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 &quot;from-to-becomes&quot; matrix, or round your input data.  

x <- subst(round(r), is, become)



  [1]: https://i.stack.imgur.com/dCFuw.png

</details>



huangapple
  • 本文由 发表于 2023年2月27日 09:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定