2

I am trying to ultimately compare the gate height to the corresponding fence height located nearby. So far I have come up with the following:

gates = "gate_line"
fence = "fence_line"

arcpy.Near_analysis(gates, fence)

with arcpy.da.SearchCursor(gates, ["NEAR_FID", "gate_ht"]) as cursor1:
  with arcpy.da.SearchCursor(fence, ["OBJECTID", "fence_ht"]) as cursor2:
    while row in cursor1:
        for rows in cursor2:
             print "Fence Height: {0}, Gate Height: {1}".format(rows[1], row[1])

Obviously, I am not to a comparison stage yet, still trying to get the output to read properly to know that I am seeing results from both the feature classes represented. With this code block the fence heights are displaying correctly, however the gate heights are all the same height (they are not in the data).

Suggestions?

Edit:

This is what I have ended up with and it is working:

with arcpy.da.UpdateCursor(gates, ["NEAR_FID", "gate_ht", "OBJECTID", "SAME_HEIGHT"]) as cursor1:
    for row in cursor1:
        sqlStr = '"OBJECTID" =' + str(cursor1[0])
        with arcpy.da.SearchCursor(fence, ["OBJECTID", "fence_ht"], sqlStr)  as cursor2:
            for rows in cursor2:
                if row[1] != rows[1]:
                    row[3] = "False"
                cursor1.updateRow(row)

1 Answer 1

2

I can answer the "how do I make it work?" part of your question, but I welcome the answer to "why doesn't this work?".

Move the call to loop through cursor1 outside the call to create cursor2:

For example:

fc1 = r'path to feature class 1'
fc2 = r'path to feature class 2'

with arcpy.da.SearchCursor(fc1, ["OID@"]) as cursor1:
    for row1 in cursor1:
        with arcpy.da.SearchCursor(fc2, ["OID@"]) as cursor2:
            for row2 in cursor2:
                print str(row1[0]) + ', ' + str(row2[0])

Your Answer

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

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