Why is crop sometimes introducing NAs on a categorical raster?

偶尔善良 提交于 2019-12-22 18:40:18

问题


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

If I try to crop a smaller area, it works fine.

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

This error is preventing me from saving the file with writeRaster, but I have no idea of what's going on.

Another user found the problem in a related question. The RAT (Raster Attribute Table) is being corrupted, depending on which part of the original raster is cropped. Still no idea why this happens.

> 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

回答1:


The error message indicated something wrong happening with the Raster Attribute Table. The problem is related with special characters on the labels.

In particular, it appears that semicolumns (:) causes somehow a "splitting" of the class names in the cropped raster. Therefore, as soon as both

"Mosaic: Tree Cover / Other natural vegetation"

and

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

pixels were included in the cropped extent, the RAT got corrupted due to multiple Mosaic labels, preventing correct saving of the file.

"Tidying" the class names using for example:

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

solves the issue.



来源:https://stackoverflow.com/questions/46832976/why-is-crop-sometimes-introducing-nas-on-a-categorical-raster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!