Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some GeoTiff rasters with over 100 bands in total, but they are not huge: resolution is about 1200x600 and total size is less than 7 mb. I'm trying to do a simple NDVI calculation:

raster <- stack('path_to_raster_1', 'path_to_raster_2')
red <- 60 # red band number
NIR <- 75 # NIR band number
f_NDVI <- function(x){
NDVI <- calc(raster, f_NDVI)

The calculation runs extremely slow and takes about 10 minuets. I wonder if there is a way to optimise it? And why calculation is so slow in my case?

share|improve this question
are you going to do any more calculations in R? R is not the best tool for imagery. if not, you should make the stack operation in gdal. – A.R Apr 28 '13 at 14:41
@A.R, of course I use R for a reason and I'm aware of its limitations. But in this particular case much better performance is expected. – SS_Rebelious Apr 28 '13 at 14:49
up vote 2 down vote accepted

I expect it will be much quicker this way (using a vectorized approach in the NDVI function)

f_NDVI <- function(x) {

# subset the two layers you need
s <- raster[[c(60, 75)]]

NDVI <- calc(s, f_NDVI)
share|improve this answer
Thank you, it works much quicker now! By the way, are you a developer of the 'raster' package - Robert J. Hijmans? – SS_Rebelious Apr 29 '13 at 7:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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