[GIS] Working with feature classes from multiple geodatabases in ArcPy

arcpyfeature-classfile-geodatabase

I have produced a Python script which creates multiple buffers of specific (listed) feature classes within a geodatabase. This is achieved by running each feature class through a for loop.

I would like to know if/how it would be possible to list feature classes from more than one geodatabase?

Currently I am using env.workspace to specify the single geodatabase. Listing the feature classes and then running them through the 'for loop'. A possible problem might be that many of the feature classes in the various geodatabases have the same name.

import arcpy
import os.path
import tempfile
from arcpy import env

# sets the workspace to my gdb
arcpy.env.workspace = "C:/GIS Home/Mining Features (MATTHEW).gdb"

# Output workspace and temp location is defined
output_workspace = "C:/GIS Home/project_1"
temp_workspace = tempfile.mkdtemp()

# list of features to buffer in gdb
fcList = ["Adit_Metalliferous", "Shaft_Metalliferous", "Quarry_Metalliferous", "Dump_Metalliferous", "Engine_House", "Goffan_Working", "Lode_Surface_Outcrop", "Lode_Undefined_Elevation", "Mine_Level", "Mine_Raise", "Portal", "Reported_Subsidence", "Secured_Feature", "Tunnel"]

#create empty lists for output
l_250 = []
l_500 = []
l_750 = []

for featureClass in fcList:
    b250 = os.path.join(temp_workspace, featureClass + "_b250.shp")  # make temp output file
    b500 = os.path.join(temp_workspace, featureClass + "_b500.shp")  # make temp output file
    b750 = os.path.join(temp_workspace, featureClass + "_b750.shp")  # make temp output file
    arcpy.Buffer_analysis(featureClass, b250, 250, "", "", "ALL")   # carry out buffer
    arcpy.Buffer_analysis(featureClass, b500, 500, "", "", "ALL")   # carry out buffer
    arcpy.Buffer_analysis(featureClass, b750, 750, "", "", "ALL")   # carry out buffer
    l_250.append(b250)  # append the buffer to the l_250 list
    l_500.append(b500)  # append the buffer to the l_500 list
    l_750.append(b750)  # append the buffer to the l_750 list

merge_250 = os.path.join(temp_workspace, "250_merge.shp")
arcpy.Merge_management(l_250, merge_250)
merge_500 = os.path.join(temp_workspace, "500_merge.shp")
arcpy.Merge_management(l_500, merge_500)
merge_750 = os.path.join(temp_workspace, "750_merge.shp")
arcpy.Merge_management(l_750, merge_750)

dissolve_250 = os.path.join(output_workspace, "250_dissolve.shp") # create 250_dissolve.shp for final output
arcpy.Dissolve_management(merge_250, dissolve_250)
dissolve_500 = os.path.join(output_workspace, "500_dissolve.shp") # create 500_dissolve.shp for final output
arcpy.Dissolve_management(merge_500, dissolve_500)
dissolve_750 = os.path.join(output_workspace, "750_dissolve.shp") # create 750_dissolve.shp for final output
arcpy.Dissolve_management(merge_750, dissolve_750)

Best Answer

To read from different sources with a 'wildcard (*)' use glob

import glob

gdbs = glob.glob("D:\\geodata\*\*.gdb") # looks for all .gdb's in sub- folders of geodata
for gdb in gdbs:
    # do stuff...

To get the names of the features in the gdb you better define a function based on this answer:

def feat_in_gdb(name):
    return [[os.path.join(feat, feat_class) for feat_class in \
    gp.ListFeatureClasses('','',feat)] \
    for feat in gp.ListDatasets('','feature')]

I'd also define functions for the geoprocessing as you call those methods so often, and will do this even more so when you alter your loop.

Related Question