RajatChopra
RajatChopra

Reputation: 3

Reclassify Raster in R

I have a raster image which contains the value of NDVI ranging from -0.2 to 0.6

NDVI  
#class      : RasterLayer 
#dimensions : 706, 953, 672818  (nrow, ncol, ncell)
#resolution : 30, 30  (x, y)
#extent     : 368085, 396675, 2038125, 2059305  (xmin, xmax, ymin, ymax)
#crs        : +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
#source     : memory
#names      : layer 
#values     : -0.08728454, 0.5357124  (min, max)

I need to reclassify this raster such as

Where lsat8_pv is another raster which has been calculated in below format

 lsat8_pv <- ((ndvi - maxValue(ndvi)) / (maxValue(ndvi) - minValue(ndvi)))^2

 lsat8_pv
 #class      : RasterLayer 
 #dimensions : 706, 953, 672818  (nrow, ncol, ncell)
 #resolution : 30, 30  (x, y)
 #extent     : 368085, 396675, 2038125, 2059305  (xmin, xmax, ymin, ymax)
 #crs        : +proj=utm +zone=43 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
  #source     : memory
  #names      : layer 
  #values     : 0, 1  (min, max)

I have gone through some of the similar questions but was not able to spot any question wherein formula is specified under reclassification rules.

Below is what I tried

lsat_e[ndvi < 0] = 0.991
lsat_e[ndvi > 0 & ndvi < 0.2] = 0.966

e <-  ({0.004 * lsat8_PV} + 0.986)

m = cbind(from = c(-1, 0.2, 0.5), to = c(0.2, 0.5, 1), becomes = c(0.984, e , 0.99))
m
#     from to  becomes
#[1,] -1   0.2 0.984  
#[2,] 0.2  0.5 ?      
#[3,] 0.5  1   0.99 

Upvotes: 0

Views: 3266

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47706

Here is a self-contained minimal reproducible example

library(raster)
ndvi <- raster(nrow=7, ncol=9, ext=extent(368085, 398085, 2038305, 2059305), crs="+proj=utm +zone=43 +datum=WGS84")
values(ndvi) <- seq(-1, 1, 2/ncell(ndvi))[-1]

maxVal <- cellStats(ndvi, max)
minVal <- cellStats(ndvi, min)
lsat8_pv <- ((ndvi - maxVal) / (maxVal - minVal))^2

And you should also be more clear about what you need. For example, what about the values above 0.5? Why does what you tried not use the same values as in your problem description?

Anyway, you can do something like this. Use, reclassify, but set the cells that need to be replaced by the values of another raster to NA. Then use cover for the replacement.

lst <- 0.004 * lsat8_pv + 0.986
m <- cbind(from = c(-Inf, 0, 0.2, 0.5), to = c(0, 0.2, 0.5, Inf), becomes = c(0.99, 0.96, NA , 1))
x <- reclassify(ndvi, m)
y <- cover(x, lsat8_pv)

Upvotes: 1

Related Questions