Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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
print(busRoute.interpolate(busRoute.project(currentLocation)).wkt)
# POINT (270.7692307692307700 336.1538461538461900)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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