Python - SDE Get Feature Count

497
4
Jump to solution
09-18-2019 08:12 AM
JordanMiller4
Occasional Contributor III

I am trying to figure out how to get a count on each feature class in our SDE database and later record it to a text file but I am having trouble figuring out how to setup the "Get Count" portion of the script. I haven't had any success incorporating the  "Get Count"  example. 

When "My Script" runs it prints the fc name (.e.g. jmillePCReplica.DBO.Wayne_Parcels) but when I plug that variable / fc name in place to the  "Get Count"  example it fails. 

Any ideas?

My Script

# Import system modules
import arcpy, os
from arcpy import env

# Set workspace
sdeConnection = r"Database Connections\STR-DT-JMILLE.sde"

feature_classes = []

walk = arcpy.da.Walk(sdeConnection, datatype="FeatureClass", type="All")
print("Starting..")
for dirpath, dirnames, filenames in walk:
    for filename in filenames:
     feature_classes.append(os.path.join(dirpath, filename))
     print(filename)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Get Count Example

result = arcpy.GetCount_management(filename)
print('{} has {} records'.format(filename, result[0]))‍‍‍‍‍‍‍‍‍

My Script results

Starting..
jmillePCReplica.DBO.P_AbandonedDevice
jmillePCReplica.DBO.P_AbandonedPipe
jmillePCReplica.DBO.P_AbandonedService
jmillePCReplica.DBO.P_AlignmentSheet
jmillePCReplica.DBO.P_CapitalProject
jmillePCReplica.DBO.P_Coupon
jmillePCReplica.DBO.P_CPArea
jmillePCReplica.DBO.P_DrinkingWater
jmillePCReplica.DBO.P_EcologicalArea
jmillePCReplica.DBO.P_ExcavationDamage
jmillePCReplica.DBO.P_ExposedPipeInspect
jmillePCReplica.DBO.P_GasLeak
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
deleted-user-NvcfpBOWaKwr
Occasional Contributor

Josh, that's the exact way I was going to suggest if the variable worked.Good recommendation!  To expand on that even further you could even add a single line of code and set the workspace by using arcpy.env.workspace. Since you imported it already I believe it would be env.workspace

import arcpy, os
from arcpy import env

# Set workspace
sdeConnection = r"Database Connections\STR-DT-JMILLE.sde"

env.workspace = sdeConnection

feature_classes = []

walk = arcpy.da.Walk(sdeConnection, datatype="FeatureClass", type="All")
print("Starting..")
for dirpath, dirnames, filenames in walk:
    for filename in filenames:
     feature_classes.append(os.path.join(dirpath, filename))
     print(filename)
     result = arcpy.GetCount_management(filename)
     print('{} has {} records'.format(filename, result[0]))

View solution in original post

4 Replies
deleted-user-NvcfpBOWaKwr
Occasional Contributor

It looks like you are feeding arcpy.GetCount a partial path. Try making a new variable combining sdeConnection and filename so it would  look something like whats below. Let me know if it works. 

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))
        #print(filename) You might not need this
        fileName2 = sdeConnection + "\\" + filename
        result = arcpy.GetCount_management(fileName2)
        print('{} has {} records'.format(filename, result[0]))

I also added the  GetCount within the filename loop so it would pull filename correctly. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Here is a similar suggestion as Jeremy but without the need to define a new sdeConnection variable. 

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))
        print(filename)
        result = arcpy.GetCount_management(os.path.join(dirpath, filename))
        print('{} has {} records'.format(filename, result[0])
 
deleted-user-NvcfpBOWaKwr
Occasional Contributor

Josh, that's the exact way I was going to suggest if the variable worked.Good recommendation!  To expand on that even further you could even add a single line of code and set the workspace by using arcpy.env.workspace. Since you imported it already I believe it would be env.workspace

import arcpy, os
from arcpy import env

# Set workspace
sdeConnection = r"Database Connections\STR-DT-JMILLE.sde"

env.workspace = sdeConnection

feature_classes = []

walk = arcpy.da.Walk(sdeConnection, datatype="FeatureClass", type="All")
print("Starting..")
for dirpath, dirnames, filenames in walk:
    for filename in filenames:
     feature_classes.append(os.path.join(dirpath, filename))
     print(filename)
     result = arcpy.GetCount_management(filename)
     print('{} has {} records'.format(filename, result[0]))
JordanMiller4
Occasional Contributor III

Thank you guys, I appreciate the help.

0 Kudos