# Calculate Direction/Angle of Polyline Feature Class Relative to North

3140
3
07-24-2012 04:35 PM
Occasional Contributor
Hi,

I am working with some cyclone data and I have plotted various tracks from a number of events. I would like to know the angle/direction of each segment of the line and I am unsure the best way to do this. Due to the nature of the data, it is imperative the angle must be in degrees relative to north, i.e. north = zero degrees.

I found this code below on another thread but it seems to be giving me the wrong results, e.g. a line which should have an angle of 180 degrees is calculated as 90 degrees, and another which should be around 60 degrees is 209 degrees - as you can see the error isn't consistent therefore I doubt it is the 180 variable as displayed in the code:

```180 + math.atan2((!Shape.lastpoint.Y! - !Shape.firstpoint.Y!),(!Shape.lastpoint.X! - !Shape.firstpoint.X!)) * (180 / math.pi)
```

I am a Python novice (attending ESRI training course soon) but I do have XML and HTML code experience, so any help would be greatly appreciated.

Thanks,
DK
Tags (3)
3 Replies
Occasional Contributor
You can try this formula :

It's works great for me
```def NorthAzimuth(Pline):
degBearing = math.degrees(math.atan2((Pline.lastPoint.X - Pline.firstPoint.X),(Pline.lastPoint.Y - Pline.firstPoint.Y)))
if (degBearing < 0) :      degBearing += 360.0
return degBearing```
Occasional Contributor
You can try this formula :

It's works great for me
```def NorthAzimuth(Pline):
degBearing = math.degrees(math.atan2((Pline.lastPoint.X - Pline.firstPoint.X),(Pline.lastPoint.Y - Pline.firstPoint.Y)))
if (degBearing < 0) :      degBearing += 360.0
return degBearing```

Hi,

Thanks for the reply. Unfortunately when I paste this into the Field Calculator I get a generic error prompt: "There was a failure during geoprocessing, check the Geoprocessing Results window for details." But there is no record of this task in the results pane.

DK
New Contributor III
```Parser:
Python

Expression:
GetAzimuthPolyline(!Shape!)

def GetAzimuthPolyline(shape):
degBearing = (math.degrees(math.atan2((shape.lastPoint.X - shape.firstPoint.X),(shape.lastPoint.Y - shape.firstPoint.Y))))+90
if (degBearing < 0) : degBearing += 360
return degBearing
```

This will return all + degrees with 0 degrees starting from the North rotating clockwise