1

I'm working with Corine Land cover data. I want to assess the number of pixels with any forest value (311, 312, 313, 321, 322, 323, 324) of two reference years. That's wat I did:

clc00 <- raster("CLC2000ACC_V2018_20.tif")
> clc00
class      : RasterLayer 
dimensions : 46000, 65000, 2.99e+09  (nrow, ncol, ncell)
resolution : 100, 100  (x, y)
extent     : 9e+05, 7400000, 9e+05, 5500000  (xmin, xmax, ymin, ymax)
crs        : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs 
source     : CLC2000ACC_V2018_20.tif 
names      : CLC2000ACC_V2018_20 
values     : 111, 999  (min, max)
attributes :
        ID  COUNT CLC_CODE              LABEL1       LABEL2                  LABEL3   R   G   B      RED GREEN     BLUE
 from: 111 777883      111 Artificial surfaces Urban fabric Continuous urban fabric 230   0  77 0.901961     0 0.301961
  to : 999  40471      999        999 (Nodata) 999 (Nodata)            999 (Nodata) 255 255 255 1.000000     1 1.000000

> forest_ID <- c(311, 312, 313, 321, 322, 323, 324)
> clc_forest00 <- clc00%in%forest_ID
> clc_forest00
class      : RasterLayer 
dimensions : 46000, 65000, 2.99e+09  (nrow, ncol, ncell)
resolution : 100, 100  (x, y)
extent     : 9e+05, 7400000, 9e+05, 5500000  (xmin, xmax, ymin, ymax)
crs        : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs 
source     : r_tmp_2022-02-02_152455_11840_94056.grd 
names      : layer 
values     : 0, 1  (min, max)

how can I get the amount of pixels of which value=1 is true?

1 Answer 1

2

If the raster values can fit into memory, then:

> sum(values(clc_forest00), na.rm=TRUE)
[1] 148558

Your raster is pretty large so this might fail. In which case it might be possible to loop over chunks of the raster using getValues or blocks using getValuesBlock.

> canProcessInMemory(clc_forest00)
[1] TRUE

might let you know if the raster is too big for all the values.

7
  • I got that: canProcessInMemory(clc_forest00) [1] FALSE :( Commented Feb 2, 2022 at 16:14
  • and that: sum(values(clc_forest00), na.rm=TRUE) Errore: non è possibile allocare un vettore di dimensione 22.3 Gb Commented Feb 2, 2022 at 16:15
  • I think I'll crop the raster.. Commented Feb 2, 2022 at 16:16
  • Some tips on processing large rasters block-wise here: strimas.com/post/processing-large-rasters-in-r
    – Spacedman
    Commented Feb 2, 2022 at 16:34
  • Why sum? If they want to know the number of pixels representing forest classes would not something like: length(which(values(clc_forest00) %in% c (311, 312, 313, 321, 322, 323, 324))) be more apt? Commented Feb 2, 2022 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.