Why won't writeRaster save a 4390x4390 raster?

蓝咒 提交于 2019-12-11 12:53:37

问题


I'm trying to save a raster in R, which was cropped from GLC2000. The resulting raster has 4390 rows and 4390 cols, covering Brazil and part of South America. The command to save it (as a GeoTIFF) is either

writeRaster(r1,'Raster1.tif')

or

writeRaster(r1,'Raster1.tif','GTiff')

Both keep trying to save the file for several hours, but they don't even create the file, much less finish the write process.

I doubt that's because of the size of the file (I think the file should at least start saving, with a temporary file appearing, visible or hidden -- none exists). Two other similar TIFF files (almost the same area, though all South America except Brazil is black) have 2.7 MB (indexed, gives errors when opening on Gimp: "Unknown field with tag 33550 (0x830e) encountered", and other tags) and 19.3 MB (also indexed, gives no errors on Gimp).

I don't think it's because of directory permissions, because R could save the script in the same directory (the permissions are drwxr-xr-x, and I'm the owner and the group owner).

There are 11.6 GB available in the specified file system.

I'm using Debian Jessie 64-bit with Gnome 3.14.1, and R 3.3.3 with RStudio 1.0.136.

My processor is a Intel® Core™ i3-6100 CPU @ 3.70GHz × 4, with 7.8 GiB RAM.

Should writeRaster take so long to save a raster which is not even that big? What may be going wrong?

EDIT

So I'm trying with smaller sizes.

ext <- extent(-67,-65,-1,1)
r1 <- crop(r,ext)
dim(r1) # 224 224   1
writeRaster(r1,'Raster1','GTiff') # works

ext <- extent(-68,-64,-2,2)
r1 <- crop(r,ext)
dim(r1) # 448 448   1
writeRaster(r1,'Raster2','GTiff') # works

ext <- extent(-69,-63,-3,3)
r1 <- crop(r,ext)
#Warning message:
#In .getRat(x, ratvalues, ratnames, rattypes) : NAs introduced by coercion
dim(r1) # 672 672   1
writeRaster(r1,'Raster3','GTiff') # takes forever

Looks like the error is the NAs being introduced by coercion, this prevents the file from being saved. I've tried

r1[is.na(r1)] <- 0

But still the file won't be saved. Why in the world would crop introduce NAs to a common raster, if I'm cropping inside the limits of the raster?

extent(r)
# class       : Extent
# xmin        : -180.0045
# xmax        : 179.9955
# ymin        : -56.01339
# ymax        : 89.99554

EDIT 2

> levels(r)
[[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

See also this related question.


回答1:


As by discussion in comments, the error message indicated something wrong happening with the Raster Attribute Table. The problem could be related with special characters or length of the labels. As soon as an "offending label" is brought in by the subsetting, you start having problems.

Simplifying the classnames using

levels(r)[[1]]$CLASSNAMES <- letters[1:23]

solved the issue.

Looking at this related question Why is crop introducing NAs to my raster? showed that the problem was in having semicolumns (:) in the names of the classes, which somehow led to "splitting" the class names while cropping.



来源:https://stackoverflow.com/questions/46823110/why-wont-writeraster-save-a-4390x4390-raster

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