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
  • 1
    The answer is Yes. You need to use updatecursor rather than Searchcursor – BBG_GIS Dec 8 '17 at 21:02
  • I think you'd better to compare x and y. You are using field calculator. You don't need fc, searchcursor or updatecursor in field calculator – BBG_GIS Dec 8 '17 at 21:58
  • Can I do that without creating new fields with the start and end coordinates? @wetland – Tigerwoulds Dec 8 '17 at 22:04
  • 3
    Possible duplicate of arcpy: get list of adjacent lines per line – BBG_GIS Dec 8 '17 at 22:30
  • 2
    Old-style cursors are archaic at this point -- Use DataAccess (arcpy.da) cursors for all your cursor needs. – Vince Dec 8 '17 at 22:59

enter image description here

You don't need to use a Cursor. Right-click the Field you want to populate, then enter this info into the Field Calculator.

| improve this answer | |

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):
        if geom.firstPoint.X == geom.lastpoint.X and geom.firstPoint.Y == geom.lastPoint.Y:
            return "1"
            return "0"
        return 'exception'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.