# Thread: Calculate angle of each line in a polyline shapefile

1. ## Calculate angle of each line in a polyline shapefile

Hi,

I'm looking for a tool that will calculate the angle of each line in a set of polylines individually. Ideally I would like to be able to add an extra field to the attribute table of the polylines and fill this field with the overall angle of the polyline (probably just calculated from the first point to the last point, ignoring the bits in the middle).

Is there a tool that will do this? I can find various tools that will work for polygons, but not for polylines.

Cheers,

Robin

2. ## Re: Calculate angle of each line in a polyline shapefile

Hi Robin,

To solve your problem, you can first use split lines at vertice tool to explose your polyline to segment. The following step is to use this script on the calculate field:

Code:
```Parser:
Python

Expression:
GetAzimuthPolyline( !Shape!)

Code Block:
import math
def GetAzimuthPolyline(shape):
radian = math.atan((shape.lastpoint.x - shape.firstpoint.x)/(shape.lastpoint.y - shape.firstpoint.y))
degrees = radian * 180 / math.pi
return degrees```
Now you have angle for each line in the new feature class.

Best regards,

3. ## Re: Calculate angle of each line in a polyline shapefile

Robin,

There is a tool in your ArcToolbox called Linear Direction Mean in the Spatial Statistics toolset. You could try running this tool setting the case field to a unique ID for each polyline (such as the ObjectID). Not tried it so I could be sending you off in the wrong direction (no pun intended).

Duncan

4. ## Re: Calculate angle of each line in a polyline shapefile

Hi,

I edited this code and it works fine in the field calculator, but doesn't work as shown below as a stand-alone script. Any suggestions? Thanks.

# Import system modules
import arcpy
import math
from arcpy import env

# Set environment settings
env.workspace = "C:/WorkSpace"

# Set local variables
inFeatures = "testfracs.shp"
fieldName = "Geoangle1"
expression = "GetAzimuthPolyline(!Shape!)"
codeblock = """def GetAzimuthPolyline(shape):
radian = math.atan((shape.lastpoint.x - shape.firstpoint.x)/(shape.lastpoint.y - shape.firstpoint.y))
degrees = radian * 180 / math.pi
return degrees"""

# Execute CalculateField
arcpy.CalculateField_management(inFeatures, fieldName, expression, "PYTHON", codeblock)

5. ## Re: Calculate angle of each line in a polyline shapefile

Originally Posted by fprally
Code:
` radian = math.atan((shape.lastpoint.x - shape.firstpoint.x)/(shape.lastpoint.y - shape.firstpoint.y))`
Thanks for posting. Just a quick correction (or maybe not). I'm not sure if this gives the angle in geographic coordinates vs. arithmetic or whatnot, but for my purposes, I need the angle in standard math-style degrees-from-x-axis. So I had to modify the above line switching the x and y values as follows:

Code:
` radian = math.atan((shape.lastpoint.y - shape.firstpoint.y)/(shape.lastpoint.x - shape.firstpoint.x))`
Both may be correct for different cases, but this one worked for me.

6. ## Re: Calculate angle of each line in a polyline shapefile

May be helpful - similar topic (calculating angular changes and average curvature of lines)...

7. ## Re: Calculate angle of each line in a polyline shapefile

How do I modify the Python code so that it returns the angle in a different projection? I want to use the code in the Field Calculator in ArcMap.

My data is in BC Albers and I want to return the angle in UTM Zone 11N.

8. ## Re: Calculate angle of each line in a polyline shapefile

Originally Posted by loreenhodgkinson
How do I modify the Python code so that it returns the angle in a different projection? I want to use the code in the Field Calculator in ArcMap.

My data is in BC Albers and I want to return the angle in UTM Zone 11N.
You hack it. Make sure each line has a unique ID and Project the lines to UTM Zone 11N. Do the calculation above. Join to the original lines and calc the result over. No one has shown a way to use the Field Calculator to do on the fly projections that I have seen.

Easy enough to build a ModelBuilder model to do this with an in memory output of the projection step.

9. ## Re: Calculate angle of each line in a polyline shapefile

Checking cutbacks in lines and polygons

http://resources.arcgis.com/en/help/...0000000n000000

Features with an angle between segments on a polyline or polygon feature that is below the minimum value are going to be returned as results.