Tuesday, 27 November 2018

arcpy - Formatting dependent parameters in Python Toolbox?


I am setting up my parameters for my tool in python toolbox. So far it looks like this:


enter image description here



The user inputs a tif into the Input Raster. I want the Select Bands (optional) tab to be a drop down menu that includes all of the bands contained in the .tif in the Input Raster. What changes do I need to make to my code (included below) to make this happen? I do not want to use the script wizard available in arcmap. This has to be done in Python Toolbox only.


import arcpy,os,sys

class Toolbox(object):
def __init__(self):
self.label = "Exploding Rasters"
self.alias = "ER"

# List of tool classes associated with this toolbox
self.tools = [ExplodeRasters]


class ExplodeRasters(object):
def __init__(self):
self.label = "Explode Rasters"
self.description = " This tool will take an input multiband raster and extract its individual bands. "

def getParameterInfo(self):

# Input Features parameter
in_features = arcpy.Parameter(

displayName="Input Raster",
name="in_features",
datatype="DEFeatureClass",
parameterType="Required",
direction="Input")

# create select all bands button
all_bands = arcpy.Parameter(
displayName="Select all bands",
name="Select_All",

datatype="Boolean",
parameterType="Required",
direction="Input")
TIFF = arcpy.Parameter(
displayName="TIFF",
name ="TIFF",
datatype="Boolean",
parameterType="Optional",
direction="Input")
JPG = arcpy.Parameter(

displayName="JPG",
name="JPG",
datatype="Boolean",
parameterType="Optional",
direction="Input")
IMG = arcpy.Parameter(
displayName="IMG",
name="IMG",
datatype="Boolean",
parameterType="Optional",

direction="Input")

# Range of Desired Bands parameter
select_bands = arcpy.Parameter(
displayName="Select Bands",
name="Select_Bands",
datatype="DEFeatureClass",
parameterType="Optional",
direction="Input")


select_bands.parameterDependencies = [in_features.name]
select_bands.schema.clone = True

#select bands for NDVI band parameters
NDVI_red= arcpy.Parameter(
displayName="Select a red band for NDVI calculation",
name="NDVI_bRed",
datatype="DERasterDataset",
parameterType="Optional",
direction="Input")


NDVI_NIR= arcpy.Parameter(
displayName="Select a NIR band for NDVI calculation",
name="NDVI_BNIR",
datatype="DERasterDataset",
parameterType="Optional",
direction="Input")

#out directory
OutDir=arcpy.Parameter(

displayName="Output Workspace",
name="Out_Directory",
datatype="DEWorkspace",
parameterType="Required",
direction="Input")

# Create filename output prefix
prefix = arcpy.Parameter(
displayName="Output filename prefix",
name="output_prefix",

datatype="String",
parameterType="Required",
direction="Input")

parameters = [in_features, all_bands, select_bands, NDVI_red, NDVI_NIR, prefix, OutDir, TIFF, JPG, IMG]

return parameters

def execute(self, parameters, messages):
Out_Dir=parameters[6].valueAsText

in_raster=parameters[0].valueAsText
out_prefix=parameters[5].valueAsText

messages.addMessage("INPUT RASTER=" +in_raster)
messages.addMessage("Yay, you're doing great!")

#individual bands within multiband rasters already exist, so overwrite

arcpy.env.workspace=in_raster
arcpy.env.overwriteOutput = True


# get a list of the bands that make up the raster
bRng = arcpy.ListRasters()

# loop through the bands and export each one with CopyRaster
for ThisBnd in bRng:
InBand = ThisBnd
bndDesc = arcpy.Describe(InBand)
NoData = bndDesc.noDataValue


outRaster = os.path.join(Out_Dir, out_prefix + ThisBnd+".tif")

#copy single band rasters
arcpy.CopyRaster_management(InBand,outRaster, format = "TIFF", nodata_value = NoData)
return

Answer



Looking at your code, it looks like the root of your problem may lie in the very first input parameter... "Input Raster". You've defined the data type as a "DEFeatureClass", but that can only be used for vector data, not raster data.


Instead, try changing the data type to one of the raster types, defined in the help link below. I would start off with either "DERasterDataset" or "GPRasterLayer". Finally, for the "select_bands" class, I think you might be looking for the "DERasterBand" data type.


See Defining parameter data types in a Python toolbox for more detail on the types of parameters that are available in a Python toolbox.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...