Problem statement: Given a bus route (defined as a linestring ln.the_geom), and a set of bus stop locations (pt1.the_geom,pt2.the_geom,...ptn.the_geom), divide the bus route into segments between bus stops. I am using two postgis functions
ST_line_substring(ln.the_geom,ST_line_locate_point
ln.the_geom,pt1.the_geom),ST_line_locate_point(ln.the_geom,pt2.the_geom))
The above works well if the bus route has no overlapping sections. However, if there is overlapping sections, the ST_line_locate_point will snap a point to the other end of the linestring. As an example: Bus travels from A to B, make a loop, then travels from B to A. "A to B" and "B to A" are parallel routes with all points overlapped. In case of bus stops P1 and P2, P2 is mapped into point P2' instead (on the opposite direction). The segment is extracted wrongly in this case (where extra segments are included).
How to solve problems like this? The overlapping bus routes can happen at any section of the route...