With help I've written code to determine the angle between two line geometries. However, the code assumes each line extends into infinity and doesn't account for the shared point. Thus an angle of 150 can be returned as 30, or 20 can be returned as 160. I can't come up with a way of refining my code to return the proper angle.
Here's the code:
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) #calculate declination
angle2 = GetAngle (line2StPnt, line2EndPnt) #calculate declination
angle = abs (angle1 - angle2) #angle between points - doesn't take into account shared point
#further code not shown....
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 GetAngle (p1, p2):
x1, y1 = p1
x2, y2 = p2
dX = x2 - x1
dY = y2 - y1
rads = math.atan2 (-dY, dX)
return math.degrees (rads)
AngleFromLines ([lineGeom1, lineGeom2]) #lineGeom* = arcpy polyline geometry object
Example, where the angle is calculated as 39 instead of the desired result of 141.
math.atan2 (-dY, dX)
. A picture might help. My guess is that you should use a MOD operator instead of a ABS function in line 11, but only guessing