PGROuting – Getting Nearest Node to a Given Point with Latitude and Longitude

latitude longitudeopenstreetmappgroutingpoint

I have a point whose latitude and longitude (only) are known, and a table of nodes with their geom. I want to find the closest node in the table to the given point.

I think I can achieve this with pgr_findNearestNodeDwithin function, but i cannot find the exact syntax of the function and the parameters it takes.

Any pointers in this direction will be appreciated.

Best Answer

To find the nearest point you only need a little bit PostGIS. With PostGIS 2.0 or higher you can use nearest neighbor KNN gist functionality.

I assume you have a table with all nodes ("vertices"):

SELECT * FROM vertices 
    ORDER BY the_geom <-> ST_GeometryFromText('POINT(lon lat)',4326) 
    LIMIT 1;
Related Question