I want to calculate the angle between two lines where they intersect in PostGIS.
The starting point for angle calculations in PostGIS seems to be ST_Azimuth - but that takes points as input. My first thought was to take the endpoints of the intersecting lines and performing an Azimuth calculation on those. That is not good enough, because most of the line features are not straight, and I am interested in the angle at intersection. So what I came up with is a nested operation that goes through the following steps:
- Identify all the intersections between the two line feature tables.
- Create a very small buffer around the intersection point
- Identify the points where the line features intersect the buffer exterior (taking the first point if there are more than one - I'm really only interested in whether the angle is close to 0, 90 or 180 degrees)
- Calculate ST_Azimuth for those two points.
The full SQL is kind of long to post here, but I gisted it here if you're interested. (By the way, is there a better way than to carry over all the fields going down the WITH statements?)
The results don't look right, so I'm clearly doing something wrong:
EDIT I redid the calculations in EPSG:3785 and the results are a little different but still not right:
My question is where the flaws are in this process. Am I misunderstanding what ST_Azimuth does? Is there a CRS issue? Something else altogether? Or maybe there's a much, much simpler way to do this?