本文介绍了为什么农作物有时会在分类栅格上引入NA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

另一个用户在与相关的问题中找到了问题. RAT(栅格属性表)已损坏,具体取决于原始栅格的哪一部分被裁剪.仍然不知道为什么会这样.

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

推荐答案

该错误消息指示栅格属性表发生了某些错误.该问题与标签上的特殊字符有关.

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标签导致RAT损坏,从而无法正确保存文件.

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

整理"类名,例如:

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

解决了问题.

这篇关于为什么农作物有时会在分类栅格上引入NA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 01:27