1

In an Python Toolbox I have a parameter that gets the location of a geodatabase input.gdb which has several layers of input data.

contextual_data_param = arcpy.Parameter(
    name="context",
    displayName="source for contextual data files",
    datatype="GPFile",
    direction="Input",
    parameterType="Optional"
    )

From that parameter I would like to access feature layers in the .gdb individually.

Appending layer names using os.path.join() hasn't worked because the parameter returns a geoprocessing value rather than a string/path.

Is GPfile the right datatype to be using?

How can I open a feature layer in a geodatabase using the separate names of the feature layer and the geodatabase?

2
  • 2
    A file geodatabase is a DEWorkspace class (the contents of which can be accessed via arcpy.ListFeatureClasses() and ListTables and ListDatasets). See the documentation. I use os.path.join exclusively, so I know it works (if you extract the path with .valueAsText -- note that the value is unicode in Python 2.7),
    – Vince
    Apr 1, 2020 at 17:13
  • thanks, happy to accept as an answer Apr 1, 2020 at 19:21

1 Answer 1

2

File geodatabase is a container class which holds feature classes, feature datasets (which group feature classes), and tables. Python Toolbox parameter class for file geodatabase is DEWorkspace (which also includes directories which contain shapefiles and Enterprise geodatabase connection files (.sde)).

The for a parameter declared like this:


    def getParameterInfo(self):
        contextual_data_param = arcpy.Parameter(
            name="context",
            displayName="source for contextual data files",
            datatype="DEWorkspace",
            direction="Input",
            parameterType="Optional"
        )
        return [contextual_data_param]

The execute function to exploit the collected data might look like

    def execute(self, parameters, messages):

        cdp  = parameters[0].valueAsText
        arcpy.env.workspace = cdp

        full_fcs = []
        for fc in arcpy.ListFeatureClasses():
            full_fcs.append(os.path.join(cdp,fc))

        ...

The full list of supported toolbox parameters is documented online (this is Pro, but Desktop has the same options). It's not unusual to have to sift through and choose your best option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.