2

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
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Dec 8, 2017 at 20:36
6
  • 1
    The answer is Yes. You need to use updatecursor rather than Searchcursor Commented Dec 8, 2017 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 Commented Dec 8, 2017 at 21:58
  • Can I do that without creating new fields with the start and end coordinates? @wetland Commented Dec 8, 2017 at 22:04
  • 3
    Possible duplicate of arcpy: get list of adjacent lines per line Commented Dec 8, 2017 at 22:30
  • 2
    Old-style cursors are archaic at this point -- Use DataAccess (arcpy.da) cursors for all your cursor needs. Commented Dec 8, 2017 at 22:59

2 Answers 2

5

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.

answered Dec 8, 2017 at 23:10
0
1

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'
answered Jun 19, 2018 at 20:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.