[R-sig-Geo] summing rasters with a condition given by other rasters

Ben Tupper btupper at bigelow.org
Sun May 10 04:26:39 CEST 2015


On May 9, 2015, at 11:54 AM, Martin Brandt <martin.brandt at mailbox.org> wrote:

> I have the following issue to solve:
> There is a MODIS NDVI time series with 23 images for one year, named ndvi_01
> to ndvi_23, stored in a raster brick called MODIS.NDVI.2010. Then i have a
> raster with integer values from 1-23 representing the start of the growing
> season (SOS), and the end (EOS), e.g. SOS = 13 and EOS = 21 for one pixel.
> What i am trying to do is to create a raster (let's call it SEASON) which
> sums all NDVI values from ndvi_01 to ndvi_23 which are between SOS and EOS,
> for the example pixel that would be ndvi_13 to ndvi_21.
> 
> Is this possible with the raster package?
> 

On May 9, 2015, at 11:54 AM, Martin Brandt <martin.brandt at mailbox.org> wrote:

> I have the following issue to solve:
> There is a MODIS NDVI time series with 23 images for one year, named ndvi_01
> to ndvi_23, stored in a raster brick called MODIS.NDVI.2010. Then i have a
> raster with integer values from 1-23 representing the start of the growing
> season (SOS), and the end (EOS), e.g. SOS = 13 and EOS = 21 for one pixel.
> What i am trying to do is to create a raster (let's call it SEASON) which
> sums all NDVI values from ndvi_01 to ndvi_23 which are between SOS and EOS,
> for the example pixel that would be ndvi_13 to ndvi_21.
> 
> Is this possible with the raster package?
> 

Hi,

I think it is possible.  Here is one way using the calc() function in the raster package.

# create a multiple layer brick
b <- brick(system.file("external/rlogo.grd", package="raster"))
# add layers
b <- addLayer(b,b,b)
b
# class       : RasterStack 
# dimensions  : 77, 101, 7777, 9  (nrow, ncol, ncell, nlayers)
# resolution  : 1, 1  (x, y)
# extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=merc 
# names       : red.1, green.1, blue.1, red.2, green.2, blue.2, red.3, green.3, blue.3 
# min values  :     0,       0,      0,     0,       0,      0,     0,       0,      0 
# max values  :   255,     255,    255,   255,     255,    255,   255,     255,    255 

# define the sos and eos by indexed position
sos <- 3  # blue.1
eos <- 8  # green.3

# define a function to sum along that segment of pixels
# @param x a vector of pixels at a particular location
# @param from index of the first pixel of the segment to sum
# @param to index of the last pixel in the segment to sum
# @param ... other arguments for sum()
# @return the sum of pixels long the segment from:to
sum_segment <- function(x, from = sos, to = eos, ...) {
   sum(x[from:to],...)
}

# apply the function across the entire brick
s <- calc(b, sum_segment)
s
# class       : RasterLayer 
# dimensions  : 77, 101, 7777  (nrow, ncol, ncell)
# resolution  : 1, 1  (x, y)
# extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=merc 
# data source : in memory
# names       : layer 
# values      : 0, 1530  (min, max)

Does that do what you want?

Cheers,
Ben

> kind regards,
> Martin 
> 
> 
> 
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/summing-rasters-with-a-condition-given-by-other-rasters-tp7588222.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org



More information about the R-sig-Geo mailing list