Python – How to Programmatically Check if Number of Shapes Equals Number of Table Records

arcgis-desktoparcpyecognitionpythonshapefile

I have a handful of approximately 1000 shapefiles that are corrupted (see attached error message). The shapefiles were generated from eCognition Developer 8. There is a script tool that seems to repair the shapefile once it is identified as corrupted.

enter image description here

Edit:

I want to create a quick script to loop through all of my shapefiles and check if the number of shapes matches the table records. I can count table records using the following:

# Name: fcCount.py
# Purpose: calculate the number of features in a featureclass

# Import system modules
import arcpy
from arcpy import env

env.workspace = "C:/data"
Sample = "MyShp.shp"
result_dbf = int(arcpy.GetCount_management(Sample).getOutput(0)) 
print result_dbf

I would ultimately like to create some sort of logic check such as:

if result_dbf = result_shp:
    pass
else:
    print "There is a problem with" + str(Sample)

How can I count shapes directly without accessing the .dbf file? Or, in other words, what is the best way to programmatically check if the number of shapes matches the number of table records?

Best Answer

What about using pyshp? I installed it with pip and what I tried below is pretty much straight out of the README:

>>> import shapefile
>>> sf = shapefile.Reader("/Users/chad/CoalOutcrops.shp")
>>> shapes = sf.shapes()
>>> len(shapes)
33732
>>> records = sf.records()
>>> len(records)
33732
>>>

Unfortunately (or maybe fortunately?) I don't have any jacked-up shapefiles to test to see if no. of shapes can != no. of records.

Wait just a minute, I do now have a jacked up shapefile thanks to Kirk's idea in the comments below. I backed up the dbf, made a copy of the entire shapefile, deleted some features, then renamed the backed-up dbf back to the original, and lo and behold, the number of shapes < number of records:

>>> sf = shapefile.Reader("/Users/chad/CoalOutcrops.shp")
>>> records = sf.records()
>>> len(records)
33732
>>> shapes = sf.shapes()
>>> len(shapes)
33721
>>>