Select to view content in your preferred language

Count number of features in FeatureClasses

9014
12
06-09-2010 11:16 PM
Andreas_ForøTollefsen
Emerging Contributor
Hi all.

What i want to do is to create a list of the number of features in each of my featureclasses.

I have already created a list of all the featureclasses in the workspace.

Now i need to iterate through all of these and count the number of features in each featureclass.

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

gp.Workspace = "H:\\geodata\\"

print "Generating list of featureclasses in" " " + gp.Workspace
fcList = gp.ListFeatureClasses()

for fc in fcList:
    print fc
   


Anyone have a suggestion? I assume i need to use the cursor object?
0 Kudos
12 Replies
Andreas_ForøTollefsen
Emerging Contributor
Thanks Dan.

That should be an easier way of doing it.
I wrote the script below, and it did work. However, I think getCount would be better.
Thanks.

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

gp.Workspace = "H:\\geodata\\"

print "Generating list of featureclasses in " + gp.Workspace
fcList = gp.ListFeatureClasses()

print "Listing all featureclasses in " + gp.Workspace
for fc in fcList:
    print fc

print "listing fields in featureclasses"
for fc2 in fcList:
    fn = gp.ListFields(fc2)
    print str(fc2) + " " + str(len(fn))

print "Counting number of records in each featureclass"
for fc3 in fcList:
    cur = gp.SearchCursor(fc3)
    row = cur.Next()
    alist = []
    while row:
        row = cur.Next()
        alist.append(row)
    alist.sort()
    print "number of features in " + fc3 + " are: " + str(len(alist))
                

del fcList, fc, fc2, fn, cur, row, alist
0 Kudos
ChrisMathers
Deactivated User
I asked the same recently and was told I would need to do count=gp.getcount(file) then count=int(Count.getOutput(0)) because what you are given as a result of the getcount tool is a geoprocessing result object not a number. I didnt actually need to though. For some reason doing count=gp.getcount(file) then featurecount=count worked fine. I really dont know why as that should make featurecount just another link to the result object.
0 Kudos
JoelCalhoun
Deactivated User
Per the help example, the getCount method changed from 9.2 to 9.3.
So if you are using the old 9.2 geoprocessor you can pass the resulting count directly to a variable.
If you are using the 9.3 geoprocessor you have to extract the value from a result object as the previous poster mentioned.


ArcGIS Script example:

# Using a 9.2-version geoprocessor
#
import arcgisscripting
gp = arcgisscripting.create()

gp.workspace = "D:/Workspace"
count = gp.GetCount_management("roads")




# Using a 9.3-version geoprocessor
#
import arcgisscripting
gp = arcgisscripting.create(9.3)

gp.workspace = "D:/Workspace"
result = gp.GetCount_management("roads")
count = int(result.GetOutput(0))



I found this out when my code failed when we switched to 9.3.  I don't remember ESRI announcing this change like they did the change to python lists so it was a little bit of a suprise.


Joel
0 Kudos
KimOllivier
Honored Contributor
You can see the counts of each featureclass in ArcCatalog without any Python scripting.
Add features count as a column in the options for display and then touch each featureclass to update the metadata.
then when you select the geodatabase or folder the counts will display in the listing.
0 Kudos
RandyKreuziger
Frequent Contributor


I found this out when my code failed when we switched to 9.3.  I don't remember ESRI announcing this change like they did the change to python lists so it was a little bit of a suprise.


Joel


Thanks Joel for pointing that out.  My python scripts seem to break with each new ArcGIS release!  It is very time consuming updating even a small number of mission critical python scripts with testing, etc.  But with each year I have more and more scripts to maintain.
0 Kudos
BrianWilson
Frequent Contributor
Thanks Joel for pointing that out.  My python scripts seem to break with each new ArcGIS release!  It is very time consuming updating even a small number of mission critical python scripts with testing, etc.  But with each year I have more and more scripts to maintain.


ESRI doesn't think code we wrote last year is important apparently. It also makes exchanging scripts with our clients impossible. Oh well.

Does anyone know where the "gcResult" object returned by GetCount is documented? The page for "Get Count" has no indication of what is being returned.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//0017000000n7000000.htm

Thanks Brian
0 Kudos
KevinHibma
Esri Regular Contributor
The help topic is here:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Result/000v000000n7000000/

Sample code:
import arcpy
inTable = arcpy.GetParameterAsText(0)
result = arcpy.GetCount_management(inTable)
print result.getOutput(0)
0 Kudos
BryceStath
Emerging Contributor
You can see the counts of each featureclass in ArcCatalog without any Python scripting. 
Add features count as a column in the options for display and then touch each featureclass to update the metadata. 
then when you select the geodatabase or folder the counts will display in the listing.


thanks
to elaborate a bit..

In ArcCatalog (9.3.1), Tools -> Options -> Contents(tab) -> Which Metadata... (2nd pane) -> # of Features
0 Kudos