I want to track and visualize the progress of a bus, as follows:

schematic of a bus on a route map

  • I have the coordinates of each station
  • as well as the coordinates of the bus, updated every 10 seconds

The information is stored in Postgres, as float tuples, without any awareness of street topology, or other GIS-related metadata. At the moment I have a naive implementation that calculates the "as the crow flies" distance between 2 points (the bus and the next expected station). This allows me to build a rough progress-bar, but due to the way the streets are laid out in certain parts of the city, sometimes these estimations are... very wrong.

My understanding is that the proper way to deal with this is to rely on "map-matching", which will do a "snap to road" transformation of the coordinates of the bus. This, in turn, will allow me to see what distance was covered, as if the entire route is a straight line:

6500m out of 10000m were covered by the bus

However, I do not have the GIS experience that would give me the confidence that (1) I am on the right track and that (2) I chose the path of minimum resistance.

What are the best practices in addressing this problem?

Update: the current software is a pure Python program that performs these calculations. No GIS tools are involved at all, though I suspect that the problem at hand is probably a typical scenario for them. I would prefer a solution that uses free/libre open source software, which can be integrated into existing systems, a Python API would be a plus.

  • which software?
    – MrXsquared
    Commented Apr 26, 2019 at 23:49
  • I've appended this info to the question. At the moment it is a custom-written Python program that loads coordinates from a Postgres database and does all the calculations by itself. I am willing to try whatever tool makes the job easy, though free/libre software is a preference.
    – ralien
    Commented Apr 27, 2019 at 7:46

1 Answer 1


I would try with a python library called Shapely. After you import topologies into Shapely geometry you will be able to create MultiPoint(collection of Points) geometry along the line at the specific distance. Then, the only thing you need is the closest point of this new geometry to your bus coordinates. When you have it, measure the distance from the first station to the next point of MultiPoint geometry until you reach your 'match' point with a bus. Hope it will help.

  • 1. as I understand, Shapely is for calculations on a Cartesian plane - is it "good enough" for small-scale practical purposes on a geoid? 2. Is LineString the appropriate structure for storing the information about stops on a route?
    – ralien
    Commented Apr 29, 2019 at 8:05

Your Answer

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

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