使用Python计算NDVI

2024-10-02 22:37:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道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'

你知道我做错了什么吗?


Tags: bandsaredfloatoutvisnumfile
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')

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

下面的脚本根据4波段NAIP图像计算NDVI,其中波段4=nIR,波段3=Red。您需要空间分析员扩展。

请记住,陆地卫星TM波段4=近红外波段3=红色,陆地卫星8波段5=近红外波段4=红色。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)

相关问题 更多 >