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?
1 Answer 1
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.
Explore related questions
See similar questions with these tags.
DEWorkspace
class (the contents of which can be accessed viaarcpy.ListFeatureClasses()
andListTables
andListDatasets
). See the documentation. I useos.path.join
exclusively, so I know it works (if you extract the path with.valueAsText
-- note that the value isunicode
in Python 2.7),