Terry B
Terry B

Reputation: 83

Running terra:app() function on geographically defined part of a spatRaster in R

enter image description hereI am familiar with the app() function in the R terra package. I currently have a large time series spatRaster created using terra and want to run a function across all pixel columns using app(). This works, but is slow. One way I can speed this up is to only run app() inside my area of interest (AOI). My AOI, has the same bounding rectangle as the spatRaster object, but because of its shape the AOI is only a fraction of the actual area of the spatRaster footprint (see example figure). As a result I dont really need the app() function to run outside the AOI.Is anyone aware of a way i might, for example, hand R both the spatRaster and a shape file for the AOI and only run app() within the AOI to save time? Many thanks in advance.

Upvotes: 0

Views: 295

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47491

You could do

m <- mask(r, aoi)
app(m, fun)

Or

m <- crop(r, aoi, mask=TRUE)
app(m, fun)

Upvotes: 0

Related Questions