0

I have a model that isolates a place's road network based on my input of the place's name. What I need to determine is if any of the road segments in the network are greater than 500m. To do so I've written the following into a Calculate Value tab in ModelBuilder:

def isLongerThanAllowed():
import arcpy
fc = "C:\Users\USER1\Documents\ArcGIS\Scratch.gdb\placeNetwork"
fields = "LENGTH_GEO"
with arcpy.da.SearchCursor (fc, fields) as cursor:
    #iterate rows
    for row in cursor:
        if row > 500:
            return "TRUE"
        else:
            return "FALSE"

For some reason it is returning True even when all road lengths are less than 500m.

The end goal is to use another model I created that splits the road features greater than a certain distance in two so that no segments are greater than 500m. So the logic of the end result will be "while the road network has roads greater than 500m, run the split model".

1 Answer 1

0

Your code is flawed at several levels, I provide the corrected code below. Please note I have adjusted some of the values for my test data so you need to change those back

import arcpy
def isLongerThanAllowed():
  fc = "confinementlines"
  has500 = False
  fields = ["SHAPE_Length"]
  with arcpy.da.SearchCursor (fc, fields) as cursor:
    #iterate rows
    for row in cursor:
        if row[0] > 500:
            # Length greater than 500m found no need to test any other rows
            has500 = True
            break
  return has500

Your original code would test the first row only and exit, you want to step through until a length greater than 500m is found, then exit. Note it returns a boolean, you were returning a string. You referenced the row object not the value in the row and that is a field now in a list.

Make sure the Calculate Value Data Type is set to Boolean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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