[GIS] Splitting lines at intersections in PostGIS

linear-referencingNetworkpostgissplitting

I have oriented polylines (Montreal's train network) and stops (train stations).

My network looks like this:
Train Network

(closer look):
Train stations

I'd like to split my tracks at each stations and ideally add fields FromStation, ToStation to my new Train tracks table.

I've used ST_Snap to snap my points to my tracks so there's an intersection. There's no guaranty though that there's a node where it intersects.

I tried some variation using ST_Split. No error, but no results either. Just empty geometry in the end.

My data comes from the gtfs, so for now, I have a shape_id, trip_headsign and route_short_name on each line.

If I could have a resulting table like

shape_id fromstation tostation
2_1      VRM1B       SLR3B
2_1      SLR3B       SLR2B
2_1      SLR2B       SLR1B
etc.

(shape_id 2_1 is the blue line west bound), that would be ideal.

I need somehow to use that to create a personnel allocation model for our inspectors and agents on the network. Each segments will be considered a road in Network Analyst with usage restriction to match the train schedule at each train station… Never found anything like that done, but I've been given the task to at least try.

Best Answer

You might need to use the linear referencing functions to get what you want. For each point, use ST_LineLocatePoint to find out what proportion along the line the point is, then for each pairing of points, use ST_LocateBetween to extract the portion of the line between the points. So rather than splitting, you're using linear referencing to extract.

http://postgis.net/workshops/postgis-intro/linear_referencing.html