+ Reply to Thread
Results 1 to 6 of 6

Thread: Calculate angle of each line in a polyline shapefile

  1. #1
    Robin Wilson
    Join Date
    Jan 2011
    Posts
    10
    Points
    0
    Answers Provided
    0


    0

    Default 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. #2
    Frédéric PRALLY
    Join Date
    Apr 2010
    Posts
    17
    Points
    3
    Answers Provided
    0


    0

    Default 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.

    Hope that help you,

    Best regards,

  3. #3
    Duncan Hornby
    Join Date
    Apr 2010
    Posts
    655
    Points
    151
    Answers Provided
    27


    0

    Lightbulb 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. #4
    Ryan Huntley
    Join Date
    Nov 2011
    Posts
    1
    Points
    0
    Answers Provided
    0


    0

    Default 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 AddField
    arcpy.AddField_management(inFeatures, fieldName, "SHORT")
    #print "Field has been added."

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

  5. #5
    Steve Shaffer
    Join Date
    Jan 2010
    Posts
    4
    Points
    0
    Answers Provided
    0


    0

    Default Re: Calculate angle of each line in a polyline shapefile

    Quote Originally Posted by fprally View Post
    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. #6
    J. B. K.
    Join Date
    Sep 2011
    Posts
    21
    Points
    2
    Answers Provided
    0


    0

    Default Re: Calculate angle of each line in a polyline shapefile

    May be helpful - similar topic (calculating angular changes and average curvature of lines)...
    http://forums.arcgis.com/threads/495...l=1#post175418

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts