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.

From python how can I build a list of all feature classes in a geodatabase, including inside feature datasets? The standard example only lists FC's at the top level of the gdb:

import arcgisscripting, os
gp = arcgisscripting.create(9.3)

gp.workspace = 'd:\scratch.gdb'
fcs = gp.ListFeatureClasses()

for fc in fcs:
    print fc

Please indicate which Arcgis version your answer applies to (I'm looking for 9.3 but we might as well collect all versions in one place).

share|improve this question
add comment

3 Answers

This routine for arcgis10 returns all fcs (standalone OR within a feature dataset) inside a gdb. Just set your arcpy.env.workspace then do a for loop

def listFcsInGDB():
    ''' set your arcpy.env.workspace to a gdb before calling '''
    for fds in arcpy.ListDatasets('','feature') + ['']:
        for fc in arcpy.ListFeatureClasses('','',fds):
            yield os.path.join(arcpy.env.workspace, fds, fc)
share|improve this answer
    
Nice and clean! –  Chad Cooper Feb 8 '11 at 19:10
    
thanks gotchula! That's the first time I've encountered the yield statement, had to do a bit of reading to figure it out. You forgot to note your sample is for arcgis v10. –  matt wilkie Feb 8 '11 at 22:28
    
sorry, yeah this is for 10.x. and yes, the yield is great, makes for clean code. –  gotchula Feb 8 '11 at 23:09
add comment
up vote 5 down vote accepted

I ended up using gotchula's answer reworked for arcgis9.3:

def listFcsInGDB(gdb):
    ''' list all Feature Classes in a geodatabase, including inside Feature Datasets '''
    gp.workspace = gdb
    print 'Processing ', gp.workspace

    fcs = []
    for fds in gp.ListDatasets('','feature') + ['']:
        for fc in gp.ListFeatureClasses('','',fds):
            #yield os.path.join(fds, fc)
            fcs.append(os.path.join(fds, fc))
    return fcs

gdb = sys.argv [1]
fcs = listFcsInGDB(gdb)
for fc in fcs:
    print fc            

Results:

d:\> python list-all-fc.py r:\v5\YT_Canvec.gdb
Processing  r:\v5\YT_Canvec.gdb
Buildings_and_structures\BS_2530009_0
Buildings_and_structures\BS_2380009_2
Buildings_and_structures\Tower
Buildings_and_structures\Underground_reservoir
...

EDIT: this is now in a module I call arcplus. Place with your other code or PYTHONPATH and then:

import arcplus
fcs = arcplus.listAllFeatureClasses('d:\default.gdb')
for fc in fcs:
    print "magic happens with: ", fc
share|improve this answer
    
I changed the function to use a normal list instead of a yield generator object because usually when I grab a list of feature classes in a geodatabase I want to use them more than once (generators are used once and then discarded). –  matt wilkie Feb 16 '11 at 22:30
add comment

The ListDatasets method is what I think you are looking for. This FGDB has a FD in it called "Wells" and it has 3 FCs in it. For 9.3.1:

C:\WINDOWS\system32>python
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import arcgisscripting
>>> gp=arcgisscripting.create(9.3)
>>> d='c:\data\Third_Party_Wells_PRD.gdb'
>>> gp.workspace = d
>>>
>>> fds=gp.ListDatasets('','Feature')
>>> for fd in fds:
...     print fd
...
Wells
>>> for fd in fds:
...     gp.workspace=d + '/' + fd
...     fcs=gp.ListFeatureClasses()
...     for fc in fcs:
...             print fc
...
Third_Party_Wells_BHL
Third_Party_Wells_LAT
Third_Party_Wells_SHL
>>>
share|improve this answer
add comment

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.