# Calculating order of points along line?

I have a shapefile with transportation lines and another file with all bus stops along each line (with an attribute which links stops with specific lines to which they belong).

I need to calculate line-specific order numbers for all stops - 1 being first stop from which the bus departs, 2 second stop, etc.

The shapefile containing stops contains duplicates of every physical stop depending on how many bus lines are using it (each of these duplicates have different "line" attribute number).

Any suggestions for how to calculate the new "stop sequence" attribute for all stops?

Open Source tools would be preferable.

• nearest neighbour? – dassouki Aug 1 '11 at 1:31

I'm not sure, I think you mean if a bus is on a line segment, determine all the bus stops that are closest to him, but yet still on the line?

What you need to do here is:

• Create a line layer joining all the bus stops.
• for each segment, designate a "cost", that cost would be based on a formula: distance, intersection type, street type, capacity, volume, is_peakhour, is_bus_peakhour, headway, etc. for simplicity you can just use the length or time.
• at each node create a minimum spanning tree that'll span the whole network and will tell you exactly what the length from the current bus location to all the other stops is.

If you're looking to find out which bus stop is closest to you

• Add a field to your point layer, call it Road_id (this field should have the same id as your actual road id. This will indicate where the stop is located.
• Create a nearest neighbour based on this new layer.
• No, I probably need something simpler. I know which stops belong to a specific line (they have an attrib. with the ID of a particular line). I just need to determine in which order they are 'visited' by given bus on that line - first stop being no. 1, second stop being no. 2 etc. That order must be calculated from their geolocation. – Karel Aug 1 '11 at 2:23
• What I need to have at the end are the sequence numbers assigned to every stop. Every stop will have probably several sequence numbers depending on which line we are referring to. That's why every physical stop has several duplicates in the shp file, each with different 'line reference no.' – Karel Aug 1 '11 at 2:23
• @Karel In your question, can you post a detailed example. I'm sorry but i'm not sure I follow – dassouki Aug 1 '11 at 2:39

Is this simplification of your question correct: you have a polyline and a set of points on that polyline. You want to order these points in the "driving" sequence?

One option is to use PostGIS. For each point split the polyline into two and then calculate the length of the first polyline part. This is your ordering function.

If I am not taking you wrong, then you need to do the following:

1. Identify unique line IDs from the TransportationLines shapefile
2. For each line ID, pick all the points from the BusStop shapefile
3. (Ascending) sort the points based on the envelope of your region i.e. from lower-left to upper-right
4. Starting from 1, update the relevant field to designate the bus stop number

This is rather simple to do.
You need to query, sort and update (in that order).

edit: Are you open to customization? As my suggestion mainly deals with writing code

Cheers!