How can I calculate the angle of each polyline in ArcMap?
I found a similar question on an ESRI forum that had a good answer. The image below shows a sample polyline dataset to test the field calculator on. The attached code block below calculates the degrees from the x-axis. Try the following steps:
- Use the Split Line at Vertices tool to create individual line segments from your poly line.
- Add a field in your attribute table called "angle"
- Right-click the "angle" field name header and open the field calculator
- Add the python code to the code block as in the example and click OK
- You may need to alter the code to meet you specific needs, but this will get you started.
# Pre-Loci Script Code import math def GetGeographicalDegrees(shape): radian = math.atan2(shape.lastpoint.y - shape.firstpoint.y, shape.lastpoint.x - shape.firstpoint.x) radian = radian - (math.pi /2 ) # turn minus 90° if (radian > 0): degrees = 360 - ( radian * 360) / ( 2 * math.pi ) else: degrees = 360 - ((2* math.pi + radian ) * 360) / ( 2 * math.pi ) return degrees # angle = GetGeographicalDegrees( !SHAPE! )
Took me two days to figure this out. I needed to calculate the bearing of line segments comprising polygons *(think rays of the sun radiating from outside of sphere).This python code calculates a bearing wherein N=0; E=90; S=180; W=270. This is the same as Aaron's workflow (Dec 15 2012): 'add field', 'calculate field', etc. However, this yields a continuous 0-2pi radians value corresponding to a standard bearing on a compass. Much easier to interpret and work with than other answers I have seen. Much appreciation to all the posts that helped me solve this one. Hope this helps!
Workflow with polygon
- split lines tool
- add field, either 'degrees' or 'radians'
- calculate value of field by adding below code to Codeblock, following Aaron's answer above (see snapshot of field calculator)
- call function as Aaron did above
*note: convert to degrees if you need. This is in radians
*adjust indents if errors in field calculator
code block for 'calculate field dialogue box:
import math def GetGeographicalRadians(shape): radian = math.atan2(shape.lastpoint.y - shape.firstpoint.y, shape.lastpoint.x - shape.firstpoint.x) if (radian<0): radian = (radian+math.pi)*(-1)+math.pi else: radian=(radian-math.pi)*(-1)+math.pi return radian