0

I'm trying to write a script that will find angles between polyline features. the polyline features always contain only two points (start and end).

A few sources indicates that the declination of a line can be calculated as so:

rads = math.atan2 (-dY, dX)

where dY is the difference in Y values and dX is the differece in X values of the start and end points. This seems to be where I'm failing.

Here's my code with comments:

import arcpy
import math

def GetAngle (p1, p2):
    x1, y1 = p1
    x2, y2 = p2
    dX = x2 = x1
    dY = y2 - y1
    rads = math.atan2 (-dY, dX) #wrong for finding angle/declination?
    return math.degrees (rads)

def LineToXYs (line): #return first and last coordinates
    firstX, firstY = (line.firstPoint.X, line.firstPoint.Y)
    lastX, lastY = (line.lastPoint.X, line.lastPoint.Y)
    return [(firstX, firstY), (lastX, lastY)]

def AngleFromLines (lines):
    #lines is a python list of line geometries that share a vertex
    for line1 in lines:
        for line2 in lines:
            if line1 == line2:
                continue
            line1StPnt, line1EndPnt = LineToXYs (line1) #get start and end xys for first line
            line2StPnt, line2EndPnt  = LineToXYs (line2) #get start and end xys for second line
            angle1 = GetAngle (line1StPnt, line1EndPnt) #calc angle - Doesn't work
            angle2 = GetAngle (line2StPnt, line2EndPnt) #calc angle - Doesn't work
            print "first line start and end coordinates:", line1StPnt, line1EndPnt
            print "second line start and end coordinates:", line2StPnt, line2EndPnt
            print "angle 1:", angle1
            print "angle 2:", angle2
            print "length 1:", line1.length
            print "length 2:", line2.length
            angle = abs (angle1 - angle2)
            print "angle between lines:", angle

AngleFromLines ([lineGeom1, lineGeom2]) #lineGeom* = arcpy polyline geometry object

For example, these two polyline geometries should return something around 270 degrees (or 90, also fine):

enter image description here

Instead I run the script and get:

first line start and end coordinates: (1974143.8040161133, 13562128.28540039) (1974120.358215332, 13562109.576782227)
second line start and end coordinates: (1974141.000427246, 13562131.794433594) (1974143.8040161133, 13562128.28540039)
angle 1: -0.000542988605953
angle 2: -0.000101843033066
length 1: 29.9952990964
length 2: 4.49148355856
angle between lines: 0.000441145572887

Any help would be great.

2

Your GetAngle function, as is, has a typo in computing delta X.

def GetAngle (p1, p2):
    x1, y1 = p1
    x2, y2 = p2
    dX = x2 = x1
    #...

It should be dX = x2 - x1. Is that the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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