【问题标题】:Select subset of a raster according to values根据值选择栅格的子集
【发布时间】:2022-01-26 20:11:53
【问题描述】:

我有一个栅格,它被分成几个块,其中一个具有特殊的 ID,因此每个单元格将由它们所属的块的 ID 标识。在下面的示例中,我有 1180 个补丁。我计算了所有这些补丁的面积,我只想选择那些大于 100 公顷的。 这是我的代码,但问题是 R 在最后给出了一个向量,而不是一个只包含符合条件的补丁的栅格。

# list of ID of patches (hors NA)
  num_patches <- unique(raster[!is.na(raster[])]) # list length= 1180
  area_patches <- c()
  # calculate area
  for (pixel in num_patches){
    area_patches <- c(area_patches ,sum(raster[raster == pixel]) * res(raster)[1]^2)
  }
  # conversion des m² en ha
  area_patches <- area_patches/ha
  # identify patches > 100
  pos_patches_100ha <- which(area_patches >= 100)
  # select only patches > 100 ha
  raster <- raster[raster %in% pos_parche_100ha] --> selection doesn't work, the out isn't a raster but a vector
  

【问题讨论】:

    标签: r subset raster minimum


    【解决方案1】:

    如果单元格值是 ID,并且您知道哪些 ID 对应于 100 公顷或更少的斑块,则可以简单地将它们设置为 NA 或在这种情况下为背景值:

    r[r %in% ids_to_delete] <- NA
    

    这假定要调用的栅格图层 rids_to_delete 是要设置为 NA 的 ID 向量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2021-05-17
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      相关资源
      最近更新 更多