Select to view content in your preferred language

Parameter B selection of features based on Parameter A Geodatabase - Python Toolbox

427
2
10-20-2023 02:08 PM
Jake_S
by Esri Contributor
Esri Contributor

Hey folks,

New to python toolboxes. 

What I'm looking to do

parameter A -  user defined Geodatabase

parameter B -  list of features in parameter A or in datasets within parameter A the user can select one or more

Then the execute code does thing to those selected features only

I'm a bit stumped is this possible. 

Current code

# -*- coding: utf-8 -*-

import arcpy


class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = "toolbox"

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


class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Parcel Migration Data Conversion"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""

        # Create a geodatabase parameter
        geodatabase = arcpy.Parameter(
            displayName="Input Geodatabase",
            name="geodatabase",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")

        # Create a feature class parameter
        feature_classes = arcpy.Parameter(
            displayName="Feature Classes",
            name="feature_classes",
            datatype="DEFeatureClass",
            parameterType="Required",
            direction="Input",
            multiValue=True)
        

        
      
        #feature_classes.filter.list = []
        #feature_classes.parameterDependencies = [geodatabase.name] 

        # Add the parameters to a parameter list
        params = [geodatabase, feature_classes]

        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""

        gdb = parameters[0]

        fclist = []

        for fc in arcpy.ListFeatureClasses(feature_dataset=gdb):
            fclist.append(fc)
            
        parameters[1].filter.list = fclist

        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""

        ## do stuff code goes here

        "params[1] = list of features"
        return

    def postExecute(self, parameters):
        """This method takes place after outputs are processed and
        added to the display."""
        return

Current script toolbox and geodatbase

JS_Esri_0-1697836118985.png

 

Thanks in advance!

 

0 Kudos
2 Replies
BlakeTerhune
MVP Regular Contributor

I think you need to define parameter B as GPValueTable and populate the ValueList filter with a list of feature class names.

Defining parameters in a Python toolbox—ArcGIS Pro | Documentation

0 Kudos
StaticK
MVP

You have your gdb set to the feature dataset parameter in the List Featureclass which I think will return in an empty list.  You need to iterate through the gdb, by setting the arcpy.env.workspace to the gdb and use ListDatasets() with ListFeatureClasses to get them:

 

 

arcpy.env.workspace = gdb
 
if self.params[0].altered:
    _tmparray = []
    for ds in [ds for ds in arcpy.ListDatasets(feature_type='feature') if ds is not None and ds != '']:
            for fc in [fc for fc in arcpy.ListFeatureClasses(feature_dataset=ds) if fc not in fcExclude]:
                    # append featureclass string to the fc list
                    _tmparray.append(fc)
    fcFilter = self.params[1].filter
    fcFilter.list = _tmparray

 

 Adding the .altered flag to that param will execute the list filter each time a new gdb is selected.

0 Kudos