The following is a helpful section of python code to assist with calculating NDVI (Normalised Difference Vegetation Index) using the arcpy site package. The Image Analysis Window allows you to see this visually, but if you want the values in the raster itself, then you will need to use this code.
Thanks go to the Esri team for explaining this new notation in the Spatial Analyst module of arcpy (arcpy.sa). The arcpy.sa module calculates rasters more effectively than the Raster Calculator at 9.x, as it uses the number crunching power of Python. Also, dot notation for multiple bands no longer works in the Raster Calculator at 10, so this code is a optimised replacement for this loss in functionality.
In this example, Band 3 is Red, Band 5 is Near IR.
To run this script, open IDLE or another IDE (like PythonWin, PyChecker, or WingIDE), and create a new script. Copy and paste the below script and paste into that new script. Then you need to read the green comments, and replace the CAPITALISED sections. It will create a few outputs, like NDVI and Red band rasters. These can be deleted once you are happy with the result.
____________________copy below into IDE__________________
#import modules arcpy and string
import arcpy, string
#import the environment settings and spatial analyst extension
from arcpy import env
from arcpy.sa import *
#Check out the Spatial Analyst extension (must be available to check out!)
#Add your workspace here
env.workspace = r”C:YOURWORKSPACE”
#set the input raster here (will need to be inside the workspace shown above)
input = r”YOURRASTERFILE.tif”
#Create raster directory and specify inputs. It references the input you specified above
result = “FINALNDVIOUTPUTNAME.tif”
NIR = input + “\Band_5”
Red = input + “\Band_3”
#Define outputs – Note: These will need to be deleted if you need to run this script again
NIR_out = “NIR.tif”
Red_out = “Red.tif”
#Copy raster and map algebra – makes new rasters of Red and NearIR
print “Copied NIR band as raster”
print “Copied Red band as raster”
#Create Numerator and Denominator rasters as variables and NDVI output (note that arcpy.sa.Float returns a floating point raster)
Num = arcpy.sa.Float(Raster(NIR_out) – Raster(Red_out))
Denom = arcpy.sa.Float(Raster(NIR_out) + Raster(Red_out))
NIR_eq = arcpy.sa.Divide(Num, Denom)
#Saving output to result output you specified above
If you don’t think you are using Spatial Analyst to it’s full extent, perhaps have a look at the Spatial Analyst training course?