Python script to only keep waypoints within x km from track?


I need to write a script in Python that can read a track and a set of waypoints, and only keep waypoints that lie within x kilometers from the track.

In pseudo-code:

from geopy.distance import distance

read track.gpx and waypoints.gpx
for each waypoint:
	get lat,lon
	compute distance from track
	if < 5km
		add waypoint to array
write array to file

Before I take a deeper look, does someone have some code I could use?

Thank you.