1

I am trying to re-classify to raster images which do no have continuous range intervals which normally have been used in examples for creating classification matrix. My original data has these values and classes it has 17 classes in total and i want to aggregate and reduce them for better visualization of data:

Needleaf Forest 1
Broadleaf Forest 2
Deciduous needleaf Forest 3
Deciduous broadleaf forest 4
mixed Forests 5
closed shrublands 6
open shrubland 7
woody savannas 8
savannas 9
grasslands 10
wetlands 11
croplands 12
built up 13
crop/natural veg 14
permanent snow 15
barren 16
water bodies 17
unclassified 255

Now I want to reclassify this according to following table:

1-5-> 1
6-7->2
8-9->3
10->4
11->5
12,14->6
13->7
15,17->8
16->9
255->NA

I am using the following code to built up the matrix and reclassify images in batch mode but the matrix is not able to classify when it's not given in a sequential order. How can i produce the above mentioned classification?

Here is the code i am working on:

library("raster")
grids <- list.files("/media/MyData/Data/MODIS/PROCESSED/MCD12Q1.006_20190728202245", pattern = "*.tif$")

reclass_df <- c(1, 5, 1,
                6, 7, 2,
                8, 9, 3,
                4,255, 10)
rclmat <- matrix(reclass_df, ncol=3, byrow=TRUE)
r <- raster("/media/MyData/Data/MODIS/PROCESSED/MCD12Q1.006_20190728202245/MCD12Q1.A2003001.LC_Type1.tif")
r
rc <- reclassify(r, rclmat)
rc
#function to reclassify rasters and write a new reclassified tif file for each
batch_reclass <- function(grids){
  for (i in 1:length(grids)) {
    #read in raster
    r <- raster(paste0("/media/MyData/Data/MODIS/PROCESSED/MCD12Q1.006_20190728202245/", grids[i]))
    #perform the reclassifcation
    rc <- reclassify(r, rclmat)
    rc
    #write each reclass to a new file 
    plot(rc)
    writeRaster(rc, filename = paste0("/media/MyData/Data/MODIS/PROCESSED/reclass/", "rc_", grids[i]), format=".tif")
  }
}
#run the function
batch_reclass(grids)

The current matrix in code works fine but something like:

c(1,5,1,
                6,7,2,
                8,9,3,
                10,4,
                11,5,
                12,14,6,
                13,7,
                15,17,8,
                16,9,
                9,255,NA)

It throws up error

Error in .local(x, rcl, ...) : all(rcl[, 2] >= rcl[, 1]) is not TRUE

which is because of incorrect matrix. How can i create the right matrix?

1

The reclassification matrix has to be three columns. The first two columns are the start and finish values. If you want to reclassify a single value then have it in the first and last column and do right=NA to specify open intervals on both sides.

For example this matrix will reclassify 4 as -2:

> rcl = matrix(c(1,3,-1,4,4,-2,5,20,-3),ncol=3,byrow=TRUE)
> rcl
     [,1] [,2] [,3]
[1,]    1    3   -1
[2,]    4    4   -2
[3,]    5   20   -3
> 

Make a test raster with your values in it to check what you are trying works:

> r = raster(matrix(1:20,1,20))
> as.matrix(r)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    1    2    3    4    5    6    7    8    9    10    11    12    13    14
     [,15] [,16] [,17] [,18] [,19] [,20]
[1,]    15    16    17    18    19    20

Then do the reclassifying and check:

> as.matrix(reclassify(r,rcl,right=NA))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]   -1   -1   -1   -2   -3   -3   -3   -3   -3    -3    -3    -3    -3    -3
     [,15] [,16] [,17] [,18] [,19] [,20]
[1,]    -3    -3    -3    -3    -3    -3

much easier to check this works than to run it on a big raster.

Also, you want to do this:

12,14->6
13->7

but 13 is between 12 and 14 so you probably have to have rows in your matrix:

12,12, 6,
13,13, 7,
14,14, 6,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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