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.

Routes whose directions are significant

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.

Thanks in advance!

  • 1
    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

1 Answer 1


I can think of a general solution involving the use of ST_Startpoint and ST_Endpoint, although it only really works if your paths aren't circular. The idea is checking to see if the startpoint of line 1 is closer to the startpoint of line 2 than it is to the endpoint of line 2. Code, self-joining a table with all the paths:

select a.fid,
case when st_distance(st_startpoint(a.geom),st_startpoint(b.geom)) <  st_distance(st_startpoint(a.geom),st_endpoint(b.geom)) then 'same direction' else 'different direction' end as directionality
from lines a,lines b where ST_intersects(a.geom,b.geom)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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