Skip to content

Instantly share code, notes, and snippets.

@stdavis
Created December 2, 2019 21:58
Show Gist options
  • Save stdavis/9e3adef180614a180b8906445bad5239 to your computer and use it in GitHub Desktop.
Save stdavis/9e3adef180614a180b8906445bad5239 to your computer and use it in GitHub Desktop.
'''
fix_corrupt_geometries.py
A module that contains code for fixing geometries that stubbornly refused to give you their WKT
This code assumes that the OBJECTIDs are sequential.
Example usage:
from fix_corrupt_geometries import fix
fix(r'path/to/data')
'''
import arcpy
sql_clause = (None, 'ORDER BY OBJECTID')
error_ids = []
def _try_next(cur, last_oid):
try:
oid, wkt = cur.next()
return oid
except RuntimeError:
print('error with {}'.format(last_oid + 1))
error_ids.append(str(last_oid + 1))
return -1
except StopIteration:
return False
def fix(dataset):
print('finding issues')
with arcpy.da.SearchCursor(dataset, ['OID@', 'Shape@WKT'], sql_clause=sql_clause) as cur:
status = _try_next(cur, 0)
while status:
status = _try_next(cur, status)
if len(error_ids) > 0:
print('fixing data')
with arcpy.da.UpdateCursor(dataset, ['OID@', 'Shape@'], 'OBJECTID IN ({})'.format(','.join(error_ids)), sql_clause=sql_clause) as ucur:
for oid, shape in ucur:
print(shape.partCount)
ucur.updateRow((oid, shape))
else:
print('no issues found')
if __name__ == '__main__':
import sys
fix(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment