Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

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

Currently I am using env.workspace to specify the single geodatabase. Listing the features and then running them through the 'for loop'. A possible problem might be that many of the featureclasses 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)
share|improve this question
2  
You could create a python list with the pathname for each gdb (workspaceList = ["C:/xxx", "C:/yyy", "C:/zzz"]). When you loop through the features within a gdb you could add an identifier to the filename that references the current workspace. –  Roy Oct 2 '12 at 20:11
    
When you say "features" in your question do you mean "feature classes"? I'd recommend including your code - or at least the bit related to your question. What I think you are wanting to do should be straightforward as @Roy suggests. –  PolyGeo Oct 2 '12 at 22:05
    
@PolyGeo yes sorry I mean feature classes. –  Matt Oct 3 '12 at 7:36

2 Answers 2

up vote 4 down vote accepted

There are several ways to do this. If you want to get all the .gdbs, then LarsVegas' answer is the way to go. If you have only specific gdbs you want to target, then you could do this:

import arcpy
import os

gdb_dir = r"D:\Projects\GDBs"

gdbs = ["scratch.gdb", "slowbutter.gdb"]

for gdb in gdbs:
    arcpy.env.workspace = os.path.join(gdb_dir, gdb)
    print arcpy.env.workspace
    fc_list = [...]
    for fc in fc_list:
        # do whatever

You could take it a step further and have a list of gdb_dirs if your geodatabases are in different directories, and then iterate through those as well. You're essentially taking the different pieces of the file paths, breaking them up, them looping through each one and piecing them back together as you go.

Based on your comment below, try this (untested):

gdb_dirs = ["C:/GIS/dir1", "C:/GIS/dir2", "C:/GIS/dir3"]
gdbs = {"geo1.gdb":["list of feature classes"],
        "geo2.gdb":["list of feature classes"],
        "geo3.gdb":["list of feature classes"]}
for gdb_dir in gdb_dirs:
    for gdb, fc_list in gdbs.iteritems():
        arcpy.env.workspace = os.path.join(gdb_dir, gdb)
        for fc in fc_list: 
            DO STUFF
share|improve this answer
    
Thanks Chad Cooper and @LarsVegas. Would the following work ok? –  Matt Oct 3 '12 at 15:49
    
gdb_dir = [“C:/GIS/dir1”, “C:/GIS/dir2”, “C:/GIS/dir3”] for gdb in gdb_dir: arcpy.env.workspace = os.path.join(gdb_dir, gdb) if gdb == "geo1.gdb": fcList = ["list of feature classes"] for featureClass in fcList: DO STUFF elif gdb == "geo2.gdb" fcList = ["list of feature classes”] for featureClass in fcList: DO STUFF elif gdb == "geo3.gdb" fcList = list of feature classes"] for featureClass in fcList: DO STUFF –  Matt Oct 3 '12 at 15:50
    
@MattWalsh see my edit above. –  Chad Cooper Oct 3 '12 at 16:28

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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