使用Python计算NDVI

时间:2013-05-07 04:55:50

标签: python arcpy

我知道NDVI方程是

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

我正在尝试使用python计算它。到目前为止我已经有了这个:

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)

但我收到此错误消息,

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'

关于我做错的任何想法?

2 个答案:

答案 0 :(得分:2)

如果您更换

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

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

您的脚本应该按预期工作。

答案 1 :(得分:0)

以下脚本从4波段NAIP图像计算NDVI,其中波段4 = nIR,波段3 =红色。您需要空间分析师扩展。

请记住,Landsat TM Band 4 = nIR&amp; Band 3 = Red and Landsat 8 Band 5 = nIR,Band 4 = Red。 USGS Reference

# Calculates NDVI from multispectral imagery

import arcpy, string

from arcpy import env
from arcpy.sa import*

arcpy.CheckOutExtension("spatial")

env.workspace = r'C:\Your\workspace'

input = r'C:\Your\raster.tif'

result = "outputName.tif"

# You may need to change the band combinations.  
# This is for 4-band NAIP imagery or Landsat TM.
NIR = input + "\Band_4"
Red = input + "\Band_3"

NIR_out = "NIR.tif"
Red_out = "Red.tif"

arcpy.CopyRaster_management(NIR,NIR_out)
arcpy.CopyRaster_management(Red, Red_out)

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)

NIR_eq.save(result)