Calculating Bearing of a Polyline ArcGIS 10

19139
16
08-15-2011 04:55 PM
AlexanderBeavis
New Contributor II
Hey there,

Firstly sorry if I have put this thread in the wrong location, this is my first post and wasn't quite sure where to put it.

I'm looking to calculate the bearing of a polyline with just two points, with reference to north. I found some other threads on this but didn't have any success.

I think it maybe because I was using VBA coding which is apparently not compatible with ArcGIS 10 which requires python or dot net.

Any help would be greatly appreciated. You might need to spell it out for me though as my knowledge of GIS is pretty limited to what I've been able to teach myself over the last couple of months ion my honours.
16 Replies
DarrenWiens2
MVP Honored Contributor

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

0 Kudos
GerryGabrisch
Occasional Contributor III

Thanks for the tool.  It worked like a charm!

0 Kudos
BenChaney
New Contributor III

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

JustinOdell
Occasional Contributor III

Is there any way of telling if the "LINE_BEARING" calculates magnetic or grid?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Nothing would calculate true magnetic bearing since it varies by location and (often erratically) over time

0 Kudos
JustinOdell
Occasional Contributor III

What about if you're referencing the World Magnetic Model...?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Since the angles given in the table are static, it wouldn't matter

0 Kudos