Question

I have been trying to create a new raster object that contains only a couple of values from an existing raster. I am using the class raster found here: https://www.ga.gov.au/products/servlet/controller?event=FILE_SELECTION&catno=71071.

class       : RasterLayer  dimensions  : 14902, 19161, 285537222 (nrow, ncol, ncell) 
resolution  : 0.002349, 0.002349  (x, y) 
extent      : 110, 155.0092, -45.0048, -9.999999  (xmin, xmax, ymin, ymax) 
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0  
values      : G:\Spatial data\environmental_layers\Australian data\Land cover\Class\DLCDv1_Class.tif  
min value   : 1  
max value   : 34

I have tried:

pr <- rasterToPoints(r) # but the file is to big

and

s <- r[r>30 & r<33] # but the file is to big

and

rc <- reclass(r, c(-Inf,30,NA, 31,32, 1, 33,Inf,NA))

which produces a raster with properties:

class       : RasterLayer 
dimensions  : 14902, 19161, 285537222  (nrow, ncol, ncell)
resolution  : 0.002349, 0.002349  (x, y)
extent      : 110, 155.0092, -45.0048, -9.999999  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
values      : C:\Users\Adam\AppData\Local\Temp\R_raster_tmp\raster_tmp_61931056968.grd 
min value   : 1 
max value   : 33

I thought this would produced a raster layer with values of NA and 1, but it has 33 values. I have been struggling to find a way to 'extract by attribute' using R on such a large file. Does anyone have suggestions of how I could do this?

Was it helpful?

Solution

reclassify() may work for you with a very large raster, but you need to specify the "is" "becomes" matrix correctly. Though I am not exactly sure from your question whether this is in fact your goal when you say "raster extract."

However, here is how to do the reclassification:

For example:

## Create sample raster with values from 0 to 9
r <- raster(nrow=100, ncol=100)
r[] <- trunc(runif(ncell(r))*10)

## Create reclassification table
## Set values 0 to 4 equal to 1
## Set values 5 to 9 equal to NA

isBecomes <- cbind(c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
                   c(1, 1, 1, 1, 1, NA, NA, NA, NA, NA))

r2 <- reclassify(r, rcl=isBecomes)

I have not tested this in a raster too large to fit in memory, however I believe that reclassify() may be able to handle this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top