Feature count

8266
14
11-07-2017 10:22 PM
eyadghatasheh
New Contributor III

Hi

I have a file geodatabase which have around 23 datasets. under each data set there are a huge number of feature classes. I need to count features inside those feature classes. 

is there any automated way to do this? get count tool is a headache in my case.

Regards

Tags (1)
14 Replies
shan_sarkar
Occasional Contributor III

Eyad,

Try using Get Count—Data Management toolbox | ArcGIS Desktop 

I hope this helps!

~Shan


~Shan
0 Kudos
eyadghatasheh
New Contributor III

Shantona

Get count is not helpful in this case as I have batch it and drag hundreds of layers in the wizard. I am doing this daily and its not helpful anymore.

eyad.

0 Kudos
MitchHolley1
MVP Regular Contributor

You can use Python for this. I am going to tag Python here for visibility.  I've manipulated a code snippet found on StackExchange here to solve your problem.  The below code should do the job. 

import arcpy
import os

db = r'path_to_database'

arcpy.env.workspace = db

for ds in arcpy.ListDatasets():
    for fc in arcpy.ListFeatureClasses('','',ds):
        fc_path = os.path.join(db, fc)
        fc_count = arcpy.GetCount_management(fc_path)
        print "{0} : {1} rows".format(fc, fc_count)
eyadghatasheh
New Contributor III

Mitch

you are amazing. can I do the same to count features of feature classess in a specific dataset???

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

You can just add the Dataset Name inside arcpy.ListDatasets() function.

e.g. Following would be Line No. 8 in Mitch's code

for ds in arcpy.ListDatasets('Dataset1'):


Think Location
eyadghatasheh
New Contributor III

Jayanta

I ran the script which worked fine; but it doesn't follow the same sequence of FCs in the dataset as shown in the attached image. is there any way to get them in the same sequence and is there a way to export the result to excel?

Get count result doesn

Regards

MitchHolley1
MVP Regular Contributor

I use the Python Excel module openpyxl.  Please download this if it's not currently installed.  The below code should work if the module is installed correctly.  Please create an empty .xlsx file and set the path to the variable in line 26.

import arcpy
import os
from openpyxl import Workbook

db = r'path_to_geodatabase_.gdb'

arcpy.env.workspace = db

#wb stuff
wb = Workbook()
ws = wb.active
ws['A1'] = 'FeatureClass'
ws['B1'] = 'Count'

count = 2
for ds in arcpy.ListDatasets():
    for fc in sorted(arcpy.ListFeatureClasses('','',ds)):
        fc_path = os.path.join(db, fc)
        fc_count = str(arcpy.GetCount_management(fc_path))
        print "{0} : {1} rows".format(fc, fc_count)
        ws['A{0}'.format(count)] = fc
        ws['B{0}'.format(count)] = fc_count
        count += 1

        
output = 'path_to_output_Excel_.xlsx'
wb.save(output)
DanPatterson_Retired
MVP Emeritus

Instead of using the print line... why not add the results to a list, collect all the information, then sort the list at the end.

Add this to Mitch's code

result = []  # Mitch's line 7 ie the blank line

# replace this

print "{0} : {1} rows".format(fc, fc_count)

# with

result.append("{0} : {1} rows\n".format(fc, fc_count)) 

#  And at the very end add this line

result.sort()
result_txt = "\n".join([i for i in result])
print(result_txt)

JoshuaBixby
MVP Esteemed Contributor

The ArcPy Data Access Walk function was created for such a situation:

fgdb = # path of file geodatabase

for root, fds, fcs in arcpy.da.Walk(fgdb, datatype="FeatureClass"):
    for fc in fcs:
        fc = os.path.join(root, fc)
        print fc[len(fgdb)+1:], arcpy.GetCount_management(fc)