ESRI Python Toolbox: Limiting Input Parameter to only a File Geodatbase

779
3
07-19-2019 12:38 PM
FredKellner2
New Contributor II

I've been working with ESRI Python toolboxes for a while but I'm still having difficulties setting an input parameter as a "DEWorkspace" and using the parameter filter to make it so only file geodatabases are visible to the end user when entering parameters.

I'm using 10.5 and this is the ESRI documentation

param0 = arcpy.Parameter(
displayName="Input Workspace",    
name="in_workspace",    
datatype="DEWorkspace",    
parameterType="Required",    
direction="Input")
# Set the filter to accept only local (personal or file) geodatabases    
param0.filter.list = ["Local Database"]    
params = [param0]    
return params

The problem I'm encountering is that this filter doesn't seem to actually limit what the end user can see or enter as parameter. For example when they navigate to provide a file geodatabse the dialog allows them to see and enter .lyr files.

I'm assuming that you should be able to limit what the end user can see and enter when it comes to workspaces?

PythonGIS Developers

0 Kudos
3 Replies
RandyBurton
MVP Regular Contributor

I think your 'getParameterInfo' section is ok.  You will want to see all the folders, etc. to be able to navigate to the gdb.  You can then use 'updateMessages' to display an error message if a file or personal geodatabase is not selected.  To clear the error, the user would need to make the proper choice or exit the tool.   EDIT: changed line 31 'and' to 'or'.

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

    def getParameterInfo(self):
        """Define parameter definitions"""
        param0 = arcpy.Parameter(
            displayName = "Input Workspace",
            name = "in_workspace",
            datatype = "DEWorkspace",
            parameterType = "Required",
            direction = "Input")
        return [param0]

    def isLicensed(self):
        return True

    def updateParameters(self, parameters):
        return

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

         if parameters[0].valueAsText is not None:
            gdb = parameters[0].valueAsText
            desc = arcpy.Describe(gdb)
            if ( desc.dataType != "Workspace" ) or ( gdb[-4:].upper() not in ('.GDB', '.MDB') ):
                parameters[0].setErrorMessage("Please select a file or personal geodatabase (.gdb or .mdb)")

        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        return‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
FredKellner2
New Contributor II

Thanks for the reply Randy. While it looks like your solution will accomplish my desired outcome. I guess I'm still really wanting to know if the filter options outlined in the documentation are accurate and function as outlined. 

0 Kudos
Santiago_Plazas
New Contributor

I tested your code and it works ust fine, I think it has been solved. 

 

But maybe you can try to do this:

param0 = arcpy.Parameter(
            displayName="Input Workspace",    
            name="in_workspace",    
            datatype="DEWorkspace",    
            parameterType="Required",
            direction="Input")    
 # Set the filter to accept only local (personal or file)geodatabases    
 param0.filter.list = ["Local Database",'gdb']    
 params = [param0] 

 Check how I added "gdb" to the filter list.

0 Kudos