Documente Academic
Documente Profesional
Documente Cultură
txt
import arcpy,os,sys,string
from arcpy.sa import *
class Toolbox(object):
def __init__(self):
self.label = "Exploding Rasters"
self.alias = "ER"
class ExplodeRasters(object):
def __init__(self):
self.label = "Explode Rasters"
self.description = " This tool will take an input multiband raster and " +\
" extract all or some of its individual bands, saving
these " +\
" bands as separate single band rasters. Additionally, it
will create NDVI and SAVI images." + \
" Exploding Rasters is everythying you've ever hoped for
and dreamed of." + \
" It's perfection."
def getParameterInfo(self):
NDVI_NIR = arcpy.Parameter(
displayName="Select a NIR band for NDVI and SAVI calculation",
name="NDVI_BNIR",
datatype="String",
parameterType="Optional",
Page 2
pytool.txt
direction="Input")
NDVI_NIR.enabled= False #gray out the NIR band parameter until user enters
a red band selection
#out directory
OutDir = arcpy.Parameter(
displayName="Output Workspace",
name="Out_Directory",
datatype="DEWorkspace",
parameterType="Required",
direction="Input")
return parameters
#execution code
def execute(self, parameters, messages):
#accessing the parameters list and retrieving values as text
in_raster=parameters[0].valueAsText
ALL = parameters[1].valueAsText
Bands = parameters[2].valueAsText
Red = parameters[3].valueAsText
NIR = parameters[4].valueAsText
prefix = parameters[5].valueAsText
Out_Dir = parameters[6].valueAsText
TIFF = parameters[7].valueAsText
BMP = parameters[8].valueAsText
PNG = parameters[9].valueAsText
#NDVI calculations
NDVI_num=arcpy.sa.Float(NDVI_NIR-NDVI_red)
NDVI_denom=arcpy.sa.Float(NDVI_NIR+NDVI_red)
sr=arcpy.sa.Con(NDVI_red<>0, NDVI_num/NDVI_denom, -9999) #avoid diving
by 0
NDVI_calc=arcpy.sa.Divide(NDVI_num,NDVI_denom)
Out_NDVI= os.path.join (Out_Dir, prefix + "NDVI" + assignment)
arcpy.CopyRaster_management(NDVI_calc, Out_NDVI, format = ending,
nodata_value = NoData)
#SAVI calculations
SAVI_num=arcpy.sa.Float(NDVI_NIR-NDVI_red)
SAVI_denom=arcpy.sa.Float(NDVI_NIR+NDVI_red-0.5)
SAVI_sr=arcpy.sa.Con(NDVI_red<>0, NDVI_num/NDVI_denom*1.5, -9999) #avoid
dividing by 0
SAVI_calc=SAVI_num/SAVI_denom*1.5
Out_SAVI= os.path.join (Out_Dir, prefix + "SAVI" + assignment)
arcpy.CopyRaster_management(SAVI_calc, Out_SAVI, format = ending,
nodata_value = NoData)
#else if the user has not selected all and instead will only export
specific bands
elif parameters[2].value:
messages.addMessage("Someone loves you:)")
bands_in_input = [] #create empty list for all possible bands in input
raster
ListBandNames = arcpy.ListRasters()
result = [] #create new empty list for bands common to UpdatedInput and
bands_in_input
for element in bands_in_input:
if element in UpdatedInput:
result.append(element) #add bands common to UpdatedInput and
bands_in_input to result list
outRaster = os.path.join(Out_Dir, prefix + element + assignment)
arcpy.CopyRaster_management(element, outRaster, format = ending,
nodata_value = NoData) #copy bands of interest
#default
else:
messages.addWarningMessage("You forgot to select an extension. The
process will default to using the TIFF output format.")
return
#function that will enable the second NDVI parameter "NIR band" once "Red band"
is satisfied
def updateParameters(self, parameters):
if(parameters[3].value):
parameters[4].enabled=True
return
Page 8