I have a list of GPS coordinates that make up a bus route:

busRoute = [(x1, y1), (x2, y2), ... (xn, yn)]

I also have a point that may not be exactly on that route (due to GPS error):

currentLocation = (x, y)

Using Python, how can I get the total distance from the bus's currentLocation to the end of the busRoute?


The simplest way to do this is to use the linear referencing methods with Shapely.

from shapely.geometry import LineString, Point

# Example data
busRoute = LineString([(80, 380), (160, 380), (220, 370), (280, 330), (310, 210), (379, 185)])
currentLocation = Point(280, 350)

# The current location is not exactly on the bus route
print(busRoute.distance(currentLocation))  # 16.6410058868

# Distance along line to nearest projected point, from start
print(busRoute.project(currentLocation))  # 201.8446468877574

# .. and distance from end .. I think this is ultimately what you are looking for
print(busRoute.length - busRoute.project(currentLocation))  # 208.17654522066948

# and the point that is used, which is on the busRoute
# POINT (270.7692307692307700 336.1538461538461900)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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