Skip to content

Instantly share code, notes, and snippets.

@stdavis
Last active September 14, 2017 15:52
Show Gist options
  • Select an option

  • Save stdavis/49c98902b8a04b43169cfdf9e7148edf to your computer and use it in GitHub Desktop.

Select an option

Save stdavis/49c98902b8a04b43169cfdf9e7148edf to your computer and use it in GitHub Desktop.
Fix WKT Bug in Geometries
'''
fix_wkt_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_wkt_geometries import fix
fix(r'path/to/dataset')
or to use it from the command line:
python fix_wkt_geometries.py "path/to/dataset"
'''
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:
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