为什么作物有时会在分类栅格上引入 NA?

Why is crop sometimes introducing NAs on a categorical raster?

library(raster)
r <- raster('glc2000_v1_1') # http://forobs.jrc.ec.europa.eu/products/glc2000/products/glc2000_v1_1_Grid.zip
extent(r)
# class       : Extent 
# xmin        : -180.0045 
# xmax        : 179.9955 
# ymin        : -56.01339 
# ymax        : 89.99554 
ext <- extent(-69,-63,-3,3)
r1 <- crop(r,ext)
#Warning message:
#In .getRat(x, ratvalues, ratnames, rattypes) : NAs introduced by coercion

如果我尝试裁剪较小的区域,效果很好。

ext <- extent(-68,-64,-2,2)
r1 <- crop(r,ext) # works fine

此错误使我无法使用 writeRaster 保存文件,但我不知道发生了什么。

另一个用户在 中发现了问题。 RAT(栅格属性 Table)正在损坏,这取决于原始栅格的哪一部分被裁剪了。仍然不知道为什么会这样。

> ext <- extent(-68,-64,-2,2) # The RAT is copied from the original
> r1 <- crop(r,ext)
> levels(r1)
[[1]]
   ID     COUNT                                               CLASSNAMES
1   1  12875179                       Tree Cover, broadleaved, evergreen
2   2   8688097               Tree Cover, broadleaved, deciduous, closed
3   3   4099003                 Tree Cover, broadleaved, deciduous, open
4   4  15080165                     Tree Cover, needle-leaved, evergreen
5   5   8054159                     Tree Cover, needle-leaved, deciduous
6   6   5606446                              Tree Cover, mixed leaf type
7   7    579763               Tree Cover, regularly flooded, fresh water
8   8    115705              Tree Cover, regularly flooded, saline water
9   9   4269938            Mosaic: Tree Cover / Other natural vegetation
10 10    587270                                        Tree Cover, burnt
11 11   3195387                      Shrub Cover, closed-open, evergreen
12 12  15605651                      Shrub Cover, closed-open, deciduous
13 13  17560702                            Herbaceous Cover, closed-open
14 14  23573022                  Sparse herbaceous or sparse shrub cover
15 15   3089962          Regularly flooded shrub and/or herbaceous cover
16 16  21692769                             Cultivated and managed areas
17 17   4025653 Mosaic: Cropland / Tree Cover / Other natural vegetation
18 18   3921904              Mosaic: Cropland / Shrub and/or grass cover
19 19  24629888                                               Bare Areas
20 20 471034157                                             Water Bodies
21 21  10660085                                             Snow and Ice
22 22    378999                 Artificial surfaces and associated areas
23 23     29056                                                  No Data

> ext <- extent(-69,-63,-3,3) # The RAT is corrupted
> r1 <- crop(r,ext)
> levels(r1)
[[1]]
         ID     COUNT                                        CLASSNAMES
1         1   8688097          Tree Cover, broadleaved, deciduous, open
2         2   4099003              Tree Cover, needle-leaved, evergreen
3         3  15080165              Tree Cover, needle-leaved, deciduous
4         4   8054159                       Tree Cover, mixed leaf type
5         5   5606446        Tree Cover, regularly flooded, fresh water
6         6    579763       Tree Cover, regularly flooded, saline water
7         7    115705                                            Mosaic
8         8   4269938             Tree Cover / Other natural vegetation
9         9    587270                                 Tree Cover, burnt
10       10   3195387               Shrub Cover, closed-open, evergreen
11       11  15605651               Shrub Cover, closed-open, deciduous
12       12  17560702                     Herbaceous Cover, closed-open
13       13  23573022           Sparse herbaceous or sparse shrub cover
14       14   3089962   Regularly flooded shrub and/or herbaceous cover
15       15  21692769                      Cultivated and managed areas
16       16   4025653                                            Mosaic
17       17   3921904  Cropland / Tree Cover / Other natural vegetation
18       18  24629888                                            Mosaic
19       19 471034157               Cropland / Shrub and/or grass cover
20       20  10660085                                        Bare Areas
21       21    378999                                      Water Bodies
22       22     29056                                      Snow and Ice
23       23        NA          Artificial surfaces and associated areas
24 12875179        NA                                           No Data

错误消息表明栅格属性出现问题 Table。该问题与标签上的特殊字符有关。

特别是,半列 (:) 似乎以某种方式导致裁剪光栅中 class 名称的 "splitting"。因此,一旦

"Mosaic: Tree Cover / Other natural vegetation"

"Mosaic: Cropland / Tree Cover / Other natural vegetation"

像素包含在裁剪范围内,RAT 由于多个 Mosaic 标签而损坏,无法正确保存文件。

"Tidying" class 名称使用例如:

levels(r)[[1]]$CLASSNAMES <- stringr::str_replace(levels(r)[[1]]$CLASSNAMES , ":", "-")

问题已解决。