6

How can I calculate the angle of each polyline in ArcMap?

1
  • This is an old question, but I am wondering what you mean by "the angle of each polyline" - is it the angle of a straight line from the start to end node of that polyline, or the angle of every segment in that polyline? A picture included in your question would clarify this.
    – PolyGeo
    Commented Sep 24, 2014 at 23:56

3 Answers 3

13

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:

  1. Use the Split Line at Vertices tool to create individual line segments from your poly line.
  2. Add a field in your attribute table called "angle"
  3. Right-click the "angle" field name header and open the field calculator
  4. Add the python code to the code block as in the example and click OK
  5. You may need to alter the code to meet you specific needs, but this will get you started.

enter image description here

enter image description here

1
  • 2
    You should be using atan2
    – Mike T
    Commented Dec 16, 2012 at 20:34
12

Additional to Aarons correct answer here the a way to use atan2 as suggested by Mike T and the transformation to geographical Degrees where the geographical North is at 0/360°.

# 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! )

screenshot filed calculator

Example:

example radian

4

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

  1. split lines tool
  2. add field, either 'degrees' or 'radians'
  3. calculate value of field by adding below code to Codeblock, following Aaron's answer above (see snapshot of field calculator)
  4. 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

Here is a graphic showing what an atan2 output looks like, overlain with a unit circle.atan2 overlain with unit circle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.