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 would like to count the number of feature classes in a geodatabase. Some of the classes are in feature datasets as well. I have tried get count management, but that returns the number of rows in each feature class in the GDB.

share|improve this question

2 Answers 2

up vote 7 down vote accepted

You can run this code to get it:

arcpy.env.workspace = NAME OF GDB HERE

# Get stand alone FCs
fccount = len(arcpy.ListFeatureClasses("",""))

# Get list of Feature Datasets and loop through them
fds = arcpy.ListDatasets("","")
for fd in fds:
    oldws = arcpy.env.workspace
    arcpy.env.workspace = oldws + "\\" + fd
    fccount = fccount + len(arcpy.ListFeatureClasses("",""))
    arcpy.env.workspace = oldws


print fccount

Hope this works for you.

share|improve this answer
    
this works great, thank you! –  Geoffrey West Feb 12 at 18:52
    
If it worked for you - please check the green check mark beside my answer! Thanks! –  dklassen Feb 12 at 19:07
    
+1 Also a good approach. You may have an error with arcpy.ListFeatureDatasets(). Should this be arcpy.ListDatasets()? –  Aaron Feb 12 at 21:41
    
it is arcpy.ListDatasets –  Geoffrey West Feb 12 at 23:23
1  
Sorry about that, fixed in the code now! –  dklassen Feb 12 at 23:54

The arcpy.da.Walk() method is the way to go:

import arcpy, os

workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,
                                                  datatype="FeatureClass",
                                                  type="Any"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print "There are %s featureclasses in the workspace" % len(feature_classes)

The main operators here are arcpy.da.Walk and len(). arcpy.da.Walk simply walks through folders and subfolders and collects files of datatype="FeatureClass" using feature_classes.append(os.path.join(dirpath, filename)). Finally, len() is used to count the number of files in the feature_classes = [] list.

EDIT:

From a benchmark standpoint and in the interest of science, it appears that arcpy.da.Walk method is significantly faster than the other approach. I tested them on a FGDB with 7 FDS and 35 FC's. Here are the results:

import arcpy, os, time

###########################################################################
# METHOD 1
start = time.clock()
workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"
feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, datatype="FeatureClass", type="Any"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

print "There are %s featureclasses in the workspace" % len(feature_classes)

end = time.clock()
method1 = (end - start)

############################################################################
# Method 2
start = time.clock()
arcpy.env.workspace = r"C:\Users\OWNER\Documents\ArcGIS\Default.gdb"

# Get stand alone FCs
fccount = len(arcpy.ListFeatureClasses("",""))

# Get list of Feature Datasets and loop through them
fds = arcpy.ListDatasets("","")
for fd in fds:
    oldws = arcpy.env.workspace
    arcpy.env.workspace = oldws + "\\" + fd
    fccount = fccount + len(arcpy.ListFeatureClasses("",""))
    arcpy.env.workspace = oldws

print fccount

end = time.clock()
method2 = (end - start)
##########################################################################

print 'method1 completed in %s seconds; method2 completed in %s seconds' % (method1, method2)

enter image description here

share|improve this answer
1  
That is great Aaron, I was un-aware of Arcpy.da.Walk method - but I don't have 10.1 installed yet, so I don't have access to it. For now, I am going to have to stick with my old method, but when I upgrade - I will remember to implement your method! –  dklassen Feb 13 at 19:53

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.