# PostGIS: How to distinguish overlapping routes (i.e., LineStrings) based on their direction?

I have a set of routes which are represented by LineStrings in my PostGIS database. These routes represent journeys that people make. I want to come up with routes that (a) overlap, and (b) have the same "direction" (orientation?). I.e., I want to figure out which journeys have the most in common.

To understand what I mean by direction, please see the figure below. The paths A (red) and B (green) start at the hollow markers and end at the solid markers respectively. They overlap to a considerable extent, but they have different directions and hence don't match.

I can find intersections using ST_Intersection(.). Now I need to filter out paths with opposite directions. How can I accomplish this?

EDIT: for now I have an approach based on angle between vectors from start to end points of the paths, but it can clearly fail if the paths intersect but are not "parallel" in their entirety. So I am still looking for answers.

• Would it be helpful to get first intersection point and calculate original linestring length from A startpoint and Bstart point to it (see linear referencing). Or get all intersection points , solve those points order on original lines, then (if you can) drop those "middle" intersection points take first and last intersection point (on A & B) and create 5 lines, from Astart to A1, A1B2 to A2B1, A2 To Aend (and same for B line ) You could take those "middle" points and calculate difference between A and B line between those points to solve if route is actually shared there or not Mar 3, 2014 at 9:52
• Thanks - "linear referencing" was the technical term I was looking for. Will give it a shot. For now, the vector based approach seems to be sufficient for the kind of data that we have.
– ARV
Mar 6, 2014 at 14:38

``````select a.fid,