# How to extract sublinestring from a linestring with overlapping segments

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...

• Is there a way for you to identify the termini of your bus routes and split the closed linestring into a line in either direction? Then you will not have overlapping segments anymore. For example, you could find the furthest point on the line from the start, and separate your line into two directions using that point. Commented Oct 3, 2014 at 20:11
• Old question but, looks like you need get start and enpoint of sections, join with the points dataset and remove any section with a initial point ordering bigger than the end point. Does this makes any sense? Commented Jun 5, 2016 at 9:00
• Will you please provide the geometry WKT for the given bus route? Commented Sep 22, 2016 at 13:14

``````ST_LineSubstring(ln.the_geom,