I'll just point out that this type of script can be greatly simplified by using modern arcpy geometry objects:
>>> fc = 'lines' # line feature layer ... bearing_field = 'BEARING' # bearing field ... sr = arcpy.Describe(fc).spatialReference # spatial reference of lines ... with arcpy.da.UpdateCursor(fc,['SHAPE@',bearing_field],spatial_reference=sr) as cursor: # create cursor ... for row in cursor: # loop through lines ... pt1 = arcpy.PointGeometry(row[0].firstPoint,sr) # first point geometry ... pt2 = arcpy.PointGeometry(row[0].lastPoint,sr) # last point geometry ... row[1] = pt1.angleAndDistanceTo(pt2)[0] # (angle, distance)[0] = angle ... if row[1] < 0: # if you want all positive angles ... row[1] += 360 ... cursor.updateRow(row) # write value
Thanks for the tool. It worked like a charm!
Hello,
I know it's been awhile since the original post, but I just came across this comment and figured I'd add to the conversation since it seems like this can be done with the built-in toolboxes quite simply.
The "Add Geometry Attributes" tool has an option to calculate the "LINE_BEARING", described as "The start-to-end bearing of the line. Values range from 0 to 360, with 0 meaning north, 90 east, 180 south, 270 west, and so on."
Here's the docs for more info:
Add Geometry Attributes—Help | ArcGIS Desktop
Hopefully this helps someone out there,
Cheers,
Ben
Is there any way of telling if the "LINE_BEARING" calculates magnetic or grid?
Nothing would calculate true magnetic bearing since it varies by location and (often erratically) over time
What about if you're referencing the World Magnetic Model...?
Since the angles given in the table are static, it wouldn't matter