Count number of features in FeatureClasses

8364
12
06-09-2010 11:16 PM
Andreas_ForøTollefsen
New 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
JustinGinn1
New Contributor
You have no idea how much time I wasted because of this problem... wow. THANK YOU SO MUCH! I thought I was going insane... I had imported the 9.3 geoprocessor on a new version of the script, and I could not figure out why it wasn't working.
0 Kudos
dgesridgesri
Occasional Contributor II
Greetings,

Is there a way to get all the featureclasses in a worksapce through vb.net code?

please advise.....
0 Kudos
JohnRoberts1
New Contributor
Sub main()

Dim strServer As String
Dim strInstance As String
Dim strDB As String
Dim strUser As String
Dim strPass As String
Dim strVersion As String

strServer = "GISSDE"
strInstance = "sde:sqlserver:gissde\production"
strDB = "Utilities"
strUser = "gisreader"
strPass = "gisreader"
strVersion = "sde.DEFAULT"

        Dim pPropset As IPropertySet
        Set pPropset = New PropertySet
            With pPropset
            .SetProperty "Server", strServer
            .SetProperty "Instance", strInstance
            .SetProperty "Database", strDB
            .SetProperty "User", strUser
            .SetProperty "Password", strPass
            .SetProperty "Version", strVersion
            End With




            Dim pWorkspace As IWorkspace
            Dim pFact As IWorkspaceFactory
            Set pFact = New SdeWorkspaceFactory
            'pWOrkspace = pFact.Open pPropset, 0

           
            Set pWorkspace = pFact.Open(pPropset, 0)

            Dim pFeatws As IFeatureWorkspace
            Set pFeatws = pWorkspace

            Dim pVerWorkspace As IVersionedWorkspace
            Set m_pVersionWork = pWorkspace
   
            Dim sDatasetName As String
            sDatasetName = "<All>"
            If sDatasetName = "<All>" Then  'Checking all layers in all datasets
                Dim pWork As IWorkspace, pEnumData As IEnumDatasetName
                Dim pFeatDatasetName As IFeatureDatasetName, pDataset As IDatasetName
                Dim pEnumFeatClassName As IEnumDatasetName, pFeatClassName As IDatasetName
                Dim pVersionedObject As IVersionedObject
                Dim pFC As IFeatureClass
                Dim pName As IName
                Dim lFCount As Long
                Set pWork = m_pVersionWork
   
   
                'process data outside of feature datasets...
                Set pEnumData = pWork.DatasetNames(esriDTFeatureClass)
                Set pFeatClassName = pEnumData.Next
                Do While Not pFeatClassName Is Nothing  'Loop through the feature classes
                    ' "Checking " & pFeatClassName.Name & " layer for updates . . . "
                    Set pName = pFeatClassName
                    Set pFC = pName.Open
                    lFCount = pFC.FeatureCount(Nothing)

                            Debug.Print (pFeatClassName.Name) & " Has " & lFCount & " Features"
   
                    Set pFeatClassName = pEnumData.Next
                Loop
   
   
                'now process all feature datasets
                Set pEnumData = pWorkspace.DatasetNames(esriDTFeatureDataset)
                Set pDataset = pEnumData.Next
                Do While Not pDataset Is Nothing  'Loop through the dataset names
                    Set pFeatDatasetName = pDataset
                    Set pEnumFeatClassName = pFeatDatasetName.FeatureClassNames
                    Set pFeatClassName = pEnumFeatClassName.Next
                    Do While Not pFeatClassName Is Nothing  'Loop through the feature classes
   
                        Set pName = pFeatClassName
                        Set pFC = pName.Open
                        lFCount = pFC.FeatureCount(Nothing)
                        Debug.Print (pFeatClassName.Name) & " Has " & lFCount & " Features"
                        Set pFeatClassName = pEnumFeatClassName.Next
                    Loop
   
                    Set pDataset = pEnumData.Next
                Loop
   
            End If

End Sub
0 Kudos