Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the pixel-wise maximum of multiple rasters?

Tags:

r

spatial

raster

I have hundreds of rasters with same resolution and extent. It's a time series and each raster represent one point of time.

I know how to find the absolute maximum value in a raster.

But how can I find the maximum value in each cell in the entire time series of rasters?

If a1,a2,......a1000 are rasters, I want to create a raster x where each pixel is the maximum of all corresponding pixels of a1-a1000.

like image 250
maximusdooku Avatar asked Jan 23 '26 17:01

maximusdooku


1 Answers

If you first put the rasters in a stack, you can then simply apply min() or max() to the stack to get the summary RasterLayer you're after

## Example rasters and stack
r1 <- raster(matrix(1:4,ncol=4))
r2 <- -2*r1
r3 <- 2*r1
rr <- list(r1,r2,r3)
s <- stack(rr)

## Extract the pixel-wise min and max values
min(s)
max(s)

(To apply some other, more complicated function that returns a scalar for each pixel in the stack, you may want to use calc(), as demonstrated (for example) here.)

like image 199
Josh O'Brien Avatar answered Jan 25 '26 07:01

Josh O'Brien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!