自定义 python 函数将无法识别定义的变量
Custom python function will not recognize defined variables
我是编写函数的新手(使用 IDLE 作为我的 IDE 选择),我正在尝试编写一个函数来获取 LANDSAT 8 卫星图像并计算 NDVI 图像。如果您不熟悉 LANDSAT 8 卫星,它会收集多个波段,这些波段可以组合成 NDVI 等指数。函数如下图,保存为ndviCalc2.py:
def ndvi(var1, var2):
var1 = floatNir
var2 = floatRed
num = Minus(floatNir, floatRed)
denom = Plus(floatNir, floatRed)
ndvi = Divide(num, denom)
我正在尝试调用以下脚本中的函数:
#Import required python modules
import arcpy
import sys
#Update directory to import custom python modules, where the calcNdvi2.py file is saved
sys.path.append("C:\Users\Documents")
#Import custom function
import ndviCalc2
#Import classes: env specifies the workspace environment, and arcpy.sa specifies an extension that must be activated to run the script
from arcpy import env
from arcpy.sa import *
#Check out spatial extension
arcpy.CheckOutExtension("Spatial")
#Set environments
env.workspace = "C:\Users\Documents\toolData"
env.overwriteOutput = True
#Define local parameters, including different bands from the satellite image
input = "LANDSAT8_20150609.tif"
nir = input + "\Band_5"
red = input + "\Band_4"
#Convert parameters to floatin point rasters for calculation
floatNir = Float(nir)
floatRed = Float(red)
#Use custom script to calculate NDVI
#Calling the custom function here, and error occurs here:
ndvi = ndviCalc2.ndvi(floatNir, floatRed)
#Save raster to the workspace
ndvi.save(env.workspace + "\ndvi_image.tif")
#Check spatial extension back in
arcpy.CheckInExtension("Spatial")
我收到以下错误消息:
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
ndvi = calcNdvi_correct.ndvi(floatNir, floatRed)
File "C:\Users\Documents\ndviCalc2.py", line 11, in ndvi
var1 = floatNir
NameError: global name 'floatNir' is not defined
如何重写并正确调用我的函数以使其正确执行?
def ndvi(floatNir, floatRed):
num = Minus(floatNir, floatRed)
denom = Plus(floatNir, floatRed)
ndvi = Divide(num, denom)
这应该是您的函数定义。如果传入 floatNir 和 floatRed,则无需使用其他变量。
我是编写函数的新手(使用 IDLE 作为我的 IDE 选择),我正在尝试编写一个函数来获取 LANDSAT 8 卫星图像并计算 NDVI 图像。如果您不熟悉 LANDSAT 8 卫星,它会收集多个波段,这些波段可以组合成 NDVI 等指数。函数如下图,保存为ndviCalc2.py:
def ndvi(var1, var2):
var1 = floatNir
var2 = floatRed
num = Minus(floatNir, floatRed)
denom = Plus(floatNir, floatRed)
ndvi = Divide(num, denom)
我正在尝试调用以下脚本中的函数:
#Import required python modules
import arcpy
import sys
#Update directory to import custom python modules, where the calcNdvi2.py file is saved
sys.path.append("C:\Users\Documents")
#Import custom function
import ndviCalc2
#Import classes: env specifies the workspace environment, and arcpy.sa specifies an extension that must be activated to run the script
from arcpy import env
from arcpy.sa import *
#Check out spatial extension
arcpy.CheckOutExtension("Spatial")
#Set environments
env.workspace = "C:\Users\Documents\toolData"
env.overwriteOutput = True
#Define local parameters, including different bands from the satellite image
input = "LANDSAT8_20150609.tif"
nir = input + "\Band_5"
red = input + "\Band_4"
#Convert parameters to floatin point rasters for calculation
floatNir = Float(nir)
floatRed = Float(red)
#Use custom script to calculate NDVI
#Calling the custom function here, and error occurs here:
ndvi = ndviCalc2.ndvi(floatNir, floatRed)
#Save raster to the workspace
ndvi.save(env.workspace + "\ndvi_image.tif")
#Check spatial extension back in
arcpy.CheckInExtension("Spatial")
我收到以下错误消息:
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
ndvi = calcNdvi_correct.ndvi(floatNir, floatRed)
File "C:\Users\Documents\ndviCalc2.py", line 11, in ndvi
var1 = floatNir
NameError: global name 'floatNir' is not defined
如何重写并正确调用我的函数以使其正确执行?
def ndvi(floatNir, floatRed):
num = Minus(floatNir, floatRed)
denom = Plus(floatNir, floatRed)
ndvi = Divide(num, denom)
这应该是您的函数定义。如果传入 floatNir 和 floatRed,则无需使用其他变量。