[GIS] Getting closest point on road to point in Google Maps using Python

google mapsgoogle-maps-apiopenstreetmap

I need to find the closest point on a road to a external point. I need to do it without downloading the data (I don't know if this is possible in Google Maps) nor using PostgreSQL. I know some Python wrappers to Overpass for using OpenStreetMap, where is easy to use the function 'around', which is I need. But is possible to do the same for the free API of Google Maps?

I've asked a similar question in Getting closest point on road to point in OpenStreetMap using Python?, but with OpenStreetMap. What about Google Maps?

Best Answer

Since you downloaded the OSM data I would suggest you convert it to GeoJSON, so you can work with Turf.js, which accepts GeoJSON as input data.

To convert data to GeoJSON you can work with different tools, such as geojson.io or QGIS.

Once everything is in GeoJSON you can use turf.nearest (documentation and example). Since you are probably testing a point against a line you should have a look at turf.point-on-line. In case you are working with polygons, you should check out the documentation and example of turf.explode, so you can actually access individual vertices from that polygon.

Turf.js is very easy to use and it works with any mapping library, as long as it supports GeoJSON. So, not a problem at all with Google Maps.