Skip to content

Instantly share code, notes, and snippets.

@alexfriant
Last active December 7, 2016 00:04
Show Gist options
  • Save alexfriant/6a87ac99e7ac67ed3844 to your computer and use it in GitHub Desktop.
Save alexfriant/6a87ac99e7ac67ed3844 to your computer and use it in GitHub Desktop.
Give this script an entire Geodatabase and it reports the number of features in each feature class to a CSV file
###############################################################################
#
# Requirements: You'll need ArcGIS Desktop 10.1 or higher with Python 2.7+
# Use as a ArcToolbox Script Tool. Add the parameters as needed.
#
# Give this script an entire Geodatabase and it reports the number of
# attachments in attachment table and features in each feature class.
#
# The output is useful for comparing FC counts between multiple
# identical GDBs.
#
# Feel free to use, rewrite, and distribute as you wish.
###############################################################################
# load the esri arcgis python module
import arcpy #you need to have arcgis 10.1 or newer installed on your machine to do this
import datetime, time
## GRAB PARAMETERS FROM YOUR ARCTOOLBOX SCRIPT
workspace = arcpy.GetParameterAsText(0).replace("\\","/")
out_path = arcpy.GetParameterAsText(1).strip().replace("\\","/")
### DEFINE FUNCTIONS
# for processing through a list of feature classes
# it gets and returns the results of arcpy data access tool "search cursor"
# in the form of FC NAME, COUNT
def processGDBfc(indent_level, fcList):
results = ""
if len(fcList) > 0:
for fc in fcList:
with arcpy.da.SearchCursor(fc, ["OBJECTID"]) as cursor: # this is much faster than "get count" toolbox tool!
rows = {row[0] for row in cursor}
count = 0
for row in rows:
count += 1
temp = fc + "," + str(count)
arcpy.AddMessage(indent_level + temp)
results = results + temp + "\n"
return results
# for processing through a list of datasets
# it gets and returns the results of "processGDBfc" above
def processGDBdataset(indent_level, dsList):
results = ""
if len(dsList) > 0:
for ds in dsList:
arcpy.AddMessage("\n\tDataset: " + ds)
fcList = listFeatureClasses(ds)
results = results + processGDBfc(indent_level, fcList)
return results
# for processing through a list of attachment tables
# it gets and returns the results of arcpy data access tool "search cursor"
# in the form of Attachment Table NAME, COUNT
def processGDBat(indent_level, atList):
results = ""
if len(atList) > 0:
for at in atList:
with arcpy.da.SearchCursor(at, ["ATTACHMENTID"]) as cursor:
rows = {row[0] for row in cursor}
count = 0
for row in rows:
count += 1
temp = temp = at + "," + str(count)
arcpy.AddMessage(indent_level + temp)
results = results + temp + "\n"
return results
# returns a list of feature classes for a given dataset
# if no dataset name is given then the default is the GDB root
def listFeatureClasses(dataset=''):
temp = arcpy.ListFeatureClasses('','All',dataset)
temp.sort()
return temp
# returns a list of attachment tables for the GDB root
def listAttachmentTables():
temp = arcpy.ListTables('*ATTACH','ALL')
temp.sort()
return temp
# returns a list of datasets for a given workspace environment
def listDatasets():
temp = arcpy.ListDatasets()
temp.sort()
return temp
### INTIALIZE WORKSPACE AND OUTPUT FILE
# assign GDB parameter as the environment workspace
arcpy.env.workspace = workspace
# set up output file to be written to the GDB's home directory and named with GDB name and time stamped
gdb_name = workspace[workspace.rfind("/")+1:].replace(" ","_")
ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d_%H%M%S')
fileName = "/gdb_data_report-" + gdb_name + "-" + ts + ".csv"
if not out_path: #handle whether or not an output path was specified
gdb_path = workspace[:workspace.rfind("/")]
output_file = gdb_path + fileName
else:
output_file = out_path + fileName
# initialize output string variable for the output file
output = "," + workspace +"\n" #provides the path name of the GDB that record count column is for, useful for comparing numbers between different GDB's in a spreadsheet
output = output + "feature_class,record_count" + "\n"
# give the tool's dialog box the GDB context useful for the user
arcpy.AddMessage("\nReport for: " + gdb_name)
### ACTUAL ITERATION BEGINS HERE
# begin by iterating through all feature classes at the GDB's root level first
fcList = listFeatureClasses('')
if fcList > 0:
arcpy.AddMessage("\nRoot of Geodatabase:")
output = output + processGDBfc("\t",fcList) #indentation just for aesthetics in the dialog window
# next, iterate through each attachment table at the GDB's root level
atList = listAttachmentTables()
if atList > 0:
arcpy.AddMessage("\nAttachment Tables found:")
output = output + processGDBat("\t",atList) # I am just repurposing the fc function for counting records in the attachment tables
# last, iterate through each feature class in each dataset
dsList = listDatasets()
output = output + processGDBdataset("\t\t", dsList)
### OUTPUT
# generate output to output file
file = open(output_file, "w")
file.write(output)
file.close()
arcpy.AddMessage('\nOutput to file: ' + output_file.replace("/","\\") + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment