Cödingers Cat
Cödingers Cat

Reputation: 37

Python 2.7 Syntax to calculate line segment bearings - Not populating field

I'm trying to calculate the true north bearing of line segments in a feature class using python 2.7. I have found some posts with information but I need to do this in python modules. (Work restrictions) I've imported math and have made several attempts that don't work. These two attempts are the closest & it may just be a syntax error.

Attempt 1 gives me syntax errors Attempt 2 does not give me errors but does NOT populate the field.

    def bearing():
    field = ["BEARING"]
    arcpy.AddField_management(multi_gap, "BEARING", "DOUBLE", "", "", "10","", "NULLABLE")
    print('Field added')

#Attempt 1 - INVALID SYNTAX
    with arcpy.da.UpdateCursor(multi_gap,["SHAPE@"]) as uc:
        for row in uc:
            row = '{}'.format(180 + math.atan(!SHAPE.lastPoint.Y! - !SHAPE.firstPoint.Y!),(!SHAPE.lastPoint.X! - !SHAPE.firstPoint.X!)) * (180 / math.pi))
            uc.updateRow(row)

#Attempt 2 - NO ERRORS BUT DOES NOT POPULATE FIELD
    field = ["BEARING"]
    with arcpy.da.UpdateCursor(multi_gap, field) as uc:
        
        for row in uc:
            if row[0]:
                startptx = vertex[0].firstPoint.X
                startpty = vertex[0].firstPoint.Y

                endptx = vertex[1].firstPoint.X
                endpty = vertex[1].firstPoint.Y

                field[0] = '{}'.format(180 + math.atan((startpty - startptx)/(endpty - endptx))* (180 / math.pi))
                uc.updateRow(row)

            print('done')#prints

Can anyone help with what I am doing wrong?

Upvotes: 0

Views: 95

Answers (0)

Related Questions