Calculate NDVI using Python

user2278713 picture user2278713 · May 7, 2013 · Viewed 7.8k times · Source

I know that the NDVI equation is

NDVI = (NIR — VIS)/(NIR + VIS)

I'm trying to calculate it using python. I've got this so far:

inRaster = ('Landsat.tif')
out_NDVI_file = ('NDVI.tif')

red = arcpy.Describe(inRaster+'/Band_3')
NIR = arcpy.Describe(inRaster+'/Band_4')

num = arcpy.sa.Float(NIR-red)
denom = arcpy.sa.Foat(NIR+red)
NDVI = arcpy.sa.Divide(num, denom)

NDVI.Save(out_NDVI_file)

but i get this error message,

Traceback (most recent call last):
  File "F:\abc\def.py", line 32, in <module>
    num = arcpy.sa.Float(NIR-red)
TypeError: unsupported operand type(s) for -: 'geoprocessing describe data object' and 'geoprocessing describe data object'

Any ideas on what I am doing wrong?

Answer

Jon Snow picture Jon Snow · Aug 27, 2013

If you replace

red = arcpy.Describe(inRaster+'/Band_3')
NIR = arcpy.Describe(inRaster+'/Band_4')

with

red = arcpy.sa.Raster(inRaster+'/Band_3')
NIR = arcpy.sa.Raster(inRaster+'/Band_4')

your script should work as expected.