Running ArcGIS 10.2 here and a complete beginner with Python. I'm trying to figure out a workflow to test for closed polylines using the field calculator. Essentially what I'd like to do is to populate a specific field with "closed" or "open". This is what I've come across and if I understand it correctly, it loops through each row and checks if the first x coordinate matches the last x coordinate, if there is a match then it prints the OID.
How do I convert this to a field calculator expression?
Do I need to use an update cursor rather than search to populate my new field?
fc = "Boundary"
rows = arcpy.SearchCursor(fc)
for row in rows:
geom = row.shape
if geom.firstPoint.X == geom.lastPoint.X:
print row.OBJECTID
del row, row
2 Answers 2
You don't need to use a Cursor. Right-click the Field you want to populate, then enter this info into the Field Calculator.
I modified @klewis answer recently since I was running into an issue where some polylines had null geometry causing the field calculator to error out or give me false positives. Here is the updated code block:
def isClosed(geom):
try:
if geom.firstPoint.X == geom.lastpoint.X and geom.firstPoint.Y == geom.lastPoint.Y:
return "1"
else:
return "0"
except:
return 'exception'
Explore related questions
See similar questions with these tags.
arcpy.da
) cursors for all your cursor needs.