Calculate angle of each line in a polyline shapefile

46312
8
01-20-2011 01:03 PM
RobinWilson
New Contributor
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
0 Kudos
8 Replies
FrédéricPRALLY
Esri Contributor
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:

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.

Hope that help you,

Best regards,
DuncanHornby
MVP Notable Contributor
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
RyanHuntley
New Contributor
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 AddField
arcpy.AddField_management(inFeatures, fieldName, "SHORT")
#print "Field has been added."

# Execute CalculateField
arcpy.CalculateField_management(inFeatures, fieldName, expression, "PYTHON", codeblock)
0 Kudos
SteveShaffer
New Contributor III
 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:

 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.
0 Kudos
J_B__K_
New Contributor III
May be helpful - similar topic (calculating angular changes and average curvature of lines)...
http://forums.arcgis.com/threads/49581-get-vertex-coordinates?p=175418&viewfull=1#post175418
0 Kudos
LoreenHodgkinson
New Contributor
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.
0 Kudos
RichardFairhurst
MVP Honored Contributor
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.
0 Kudos
SreeKumar
New Contributor
Checking cutbacks in lines and polygons

http://resources.arcgis.com/en/help/main/10.1/index.html#//01020000000n000000

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.

[ATTACH=CONFIG]29961[/ATTACH]
0 Kudos