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 file geodatabase (*.gdb), including inside feature datasets? The standard example only lists feature classes at the top level of the geodatabase:

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 Desktop version your answer applies to (I am looking for 9.3 but we might as well collect all versions in one place).

share|improve this question
1  
A related but more up to date Q&A (including use of arcpy.da.Walk) is How to make a GIS inventory? –  blah238 May 22 at 19:30
1  

3 Answers 3

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

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
up vote 6 down vote accepted

I ended up using gotchula's answer, but without yield because I generally re-use the FC handles created and yield's are used once then discarded.

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

    fcs = []
    for fds in arcpy.ListDatasets('','feature') + ['']:
        for fc in arcpy.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
...

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

Arcplus also adds wildcard filtering; to process only feature classes that start with "HD_" within feature datasets containing "Hydro"

fcs = arcplus.listAllFeatureClasses(gdb, fd_filter='*Hydro*', fc_filter='HD_*')

.* now on Github, upgraded for 10.x. For arcgis 9.3 see here.

share|improve this answer
1  
Not sure I understand the rationale behind avoiding use of yield here. For one, the "handles" you refer to are not handles at all, they're just strings. And if your intention is to keep the list of feature classes around for multiple iterations, you can still keep it as a generator function and just "listify" it: my_list = list(generator_function(args)) this evaluates the generator and stores the result in a list variable. –  blah238 May 22 at 19:39
    
@blah238: oh. I guess I still don't grok yield then. I understand what a statement like fcs = fcs.append(...) is doing much more quickly than fcs = list(yield(...)). –  matt wilkie May 26 at 18:07

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.