+ Reply to Thread
Results 1 to 9 of 9

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
    18
    Points
    4
    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
    930
    Points
    310
    Answers Provided
    52


    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
    2
    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
    28
    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

  7. #7
    Loreen Hodgkinson
    Join Date
    Apr 2010
    Posts
    2
    Points
    0
    Answers Provided
    0


    0

    Default 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.
    Last edited by loreenhodgkinson; 06-13-2013 at 01:13 PM.

  8. #8
    Richard Fairhurst

    Join Date
    Apr 2010
    Posts
    2,155
    Points
    1367
    Answers Provided
    194


    0

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

    Quote Originally Posted by loreenhodgkinson View Post
    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.
    Last edited by rfairhur24; 06-13-2013 at 02:17 PM.
    If your question was answered don't forget to click the Mark as answer check and to click the top arrow (promote) at the right hand side of the post.

  9. #9
    Sree Kumar
    Join Date
    Jun 2012
    Posts
    1
    Points
    0
    Answers Provided
    0


    0

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

    Click image for larger version

Name:	GUID-871458AC-3DA0-43B0-8021-AF2E27E4670B-web.png
Views:	90
Size:	28.4 KB
ID:	29961

+ 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