'Snap GPS point on PolyLine / way geometry

Given A, B, C, would like to find D. Points are GPS coordinates (Lat, Lon), line AB is actually an imaginary line between two GPS coordinates taken from a way geometry / PolyLine / Tuples of (lat,lon).

What I am trying to achieve, is to find the shortest distance/perpendicular from point C to a given way geometry (PolyLine) and snap D on it.

way_geometry = [(37.8077138, -122.4080789),
                (37.807783400000005, -122.40824420000001),
                (37.8080626, -122.4089292),
                (37.8080973, -122.40901380000001)]

c = (37.8081065672997, -122.40900194804793)

I found some solutions using basic geometry, but I believe this is a special case, which involves Haversine Distance as well.

Are there any GIS libraries that can do this?

Visu, using folium:

m = folium.Map(tiles='CartoDB dark_matter', zoom_start=22, max_zoom=23)
folium.Marker(
        location = [c[0], c[1]],
        popup = 'C',
        icon = folium.Icon(color='red', icon='info-sign') ).add_to(m)

SAF=folium.PolyLine(locations=way_geometry,weight=5,color = 'red') 
m.add_child(SAF)

Perpendicular from point C to line AB



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source