1

enter image description hereSo I want to calculate angles for each line feature layer using UpdateCursor method. So far I have progressed with the code but what it is doing is; it is giving the result of the last feature angle and substituting it for all the fields. I tried to avoid that function calling and now I am getting this error. I have a polyline layer in a GDB consisting of smaller line features, I want to calculate the angle as in orientation of each line feature and update them in a marked field above in the attribute table. Since I am new to this, I don't understand where I am going wrong.

Is there some other concrete method to do this?

Here is my code:

count1 = 0
spatial_Reference = arcpy.Describe(output_split_merge_fibre_drop_sheath2).shapeFieldName
with arcpy.da.UpdateCursor(output_split_merge_fibre_drop_sheath2, ["Fibre_SH_D", "SHAPE@", "OBJECTID"]) as cursor:
            for row in cursor:
                shape = row[1]
                count1 = count1+1
                feature = row.append(shape)
                x1 = feature.positionAlongLine(0.51, True).firstPoint.X
                x2 = feature.positionAlongLine(0.49, True).firstPoint.X
                y1 = feature.positionAlongLine(0.51, True).firstPoint.Y
                y2 = feature.positionAlongLine(0.49, True).firstPoint.Y
                radian = math.atan2((y1 - y2), (x1 - x2))
                row[0] = str(radian)
                print(row[0])
                cursor.updateRow(row)

The error message I am getting is:-

AttributeError: 'NoneType' object has no attribute 'positionAlongLine'

12
  • 1
    First rule of cursors: Never nest DA cursors. Second rule of cursors (experts only): If you must nest cursors, make sure your loop variables don't conflict. You don't need the Search cursor at all, then you can start paying attention to your update loop code. You're also using non-DA cursor syntax.
    – Vince
    Commented Feb 17, 2021 at 13:09
  • 1
    Your code, as written, can't run (bad indentation and dangling except). Your field list is too short. The WHERE constraint is invalid. And row.setValue should fail (invalid usage). You also haven't described the data or what you are attempting. This makes it difficult to help you.
    – Vince
    Commented Feb 17, 2021 at 13:46
  • 1
    Yes, DA cursor rows are just lists, and don't have setter/getter functions. Use the documentation samples from the right function.
    – Vince
    Commented Feb 17, 2021 at 14:45
  • 1
    Your description is unclear. Giving an example in the body of the question would make it more clear. positionAlongLine is very expensive. You should only execute it once (put the result in a variable, then access X and Y in place)
    – Vince
    Commented Feb 17, 2021 at 14:50
  • 1
    You already have shape, so feature = shape (or just replace "feature" with "shape"). Again, your positionAlongLine is wasteful: p1 = shape.positionAlongLine(0.49, True).firstPoint ...
    – Vince
    Commented Feb 17, 2021 at 15:28

2 Answers 2

2

To get the angles in degrees use this one line code.

arcpy.AddGeometryAttributes_management(output_split_merge_fibre_drop_sheath2, Geometry_Properties= "LINE_BEARING")
2

I used this:

count1 = 0
spatial_Reference = arcpy.Describe(output_splitLine_fibre_duct3).shapeFieldName
with arcpy.da.UpdateCursor(output_splitLine_fibre_duct3, ["Fibre_D", "SHAPE@", "OBJECTID"]) as cursor:
    for row in cursor:
        count1 = count1+1
        feature = row[1]
        p1 = feature.positionAlongLine(0.51, True).firstPoint
        p2 = feature.positionAlongLine(0.49, True).firstPoint
        radian = math.atan2((p1.Y - p2.Y), (p1.X - p2.X))
        row[0] = str(radian)
        print(row[0])
        cursor.updateRow(row)

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.