So 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'
positionAlongLine
is very expensive. You should only execute it once (put the result in a variable, then access X and Y in place)shape
, sofeature = shape
(or just replace "feature" with "shape"). Again, yourpositionAlongLine
is wasteful:p1 = shape.positionAlongLine(0.49, True).firstPoint
...