Thursday, 3 November 2016

python - How to programmatically check if the number of shapes = number of table records?


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?



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
>>>

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...