calculate Azimuth of a line

08-19-2010 04:54 AM
Status: Implemented
Labels (1)
New Contributor II

Please can we include the ability to calculate the angle of rotation from north into the attribute on a two vertex line. i can do this with Iankos tools but ArcGIS 10 needs the script in python which isnt available!

I've needed to do this occasionally, and have always had to resort to writing VBA ArcObjects script (and more recently, Python code). There's no reason to restrict the applicability to two-vertex lines. I was interested in the overall azimuth of the endpoints of a multi-vertex line.
I keep this handy:

#import math somewhere....

    #Function to return the north azimuth bearing of a line segment in degrees
    def NorthAzimuth(x1,y1,x2,y2):
        degBearing = math.degrees(math.atan2((x2 - x1),(y2 - y1)))
        if (degBearing < 0):
            degBearing += 360.0
        return degBearing

This uses atan2 the wrong way round of course, but the rotation effect gives us north azimuth angles.

I have been playing with your script but come up against a problem that it doesnt seem to like field calculator in Arc 10 my script is below can anyone see why it says i have parsing problems...

thanks in advance


Field = NorthAzimuth(x1,y1,x2,y2)


def NorthAzimuth(x1,y1,x2,y2):
  degBearing = math.degrees(math.atan2((!Shape.lastPoint.X! - !Shape.firstPoint.X!),(!Shape.lastPoint.Y! - !Shape.firstPoint.Y!)))
  if (degBearing < 0):
      degBearing += 360.0
  return degBearing
I agree, this is needed.

Not sure if Ian Ko will provide this with Easy Calculate 10.  He is definitely saying it won't be available for 10.1.  Bummer his stuff is great.  I've tried to reproduce this calculation in Python with no success.  Am not familiar with VBA so it it tough to translate to python.    

Agree with the user below, any ESRI Solution should handle multi-vertex lines not just simple 2 vertex lines. 
Bump, Again.  Think I have a work around but would still like to see this functionality as a direct field calculation.

I believe Linear Directional Mean (Spatial Statistics Toolbox) will work for my purposes.  Just wanted to share, hate to hijack the suggestions box.

The tool is intended to calculate the mean direction of multiple lines; however there is a Case Field option that will compute the 'Compass' angle for each feature.  If you use a unique ID field as the Case Field, angle is calculated for each respective feature.  Then it's just a cursor or join/calculate operation to populate the original FC with the output FC from Linear Directional Mean.  

Use with caution: there are nuiances between spatial reference files and other variables that may generate inaccurate angle values.  My results match Easy Calculates resutls sometimes, sometimes they are off by as much as a degree.  This may/may not fit a given need; in my case it's acceptable error.  Hope this is helpful to someone.
Easy Calculate 10 works in ArcGIS 10.1 and has function to calculate the azimuth of a line segment. Azimuth can be calculated based on start, middle or end of line segment.


followup on comment just posted. Prior to running Easy Calculate tool, a field to populate with the azimuth value must be added to the attribute table of the line shapefile being processed.

I'm an emergency dispatcher and keep ArcGIS open on my desktop for finding locations/place names; establishing coordinates to guide helicopter response etc. The other day we had a report of a fire at a given azimuth and distance. I coulnd't believe Arc didn't do that! Had to use Google Earth which gives the azimuth and the distance as you create the line segment. How hard can that be? Add it to the existing point/line/poly measure bar.