PostGIS Optimization – Getting osm_id from Random Point with OpenStreetMap Database

openstreetmapoptimizationpostgis

I've downloaded the OpenStreetMap data from GeoFabrik and imported it into PostgreSQL as described in the first procedures on this page.

The import was successful. After that, I found a way of associating random points with its osm_id from this database. I know there are APIs like https://nominatim.openstreetmap.org/reverse.php?lat=-23.513442&lon=-46.384794&zoom=18&format=jsonv2 from Nominatim where I can get this information, but since there're limitations on the number of requests that we can make. I'd like to implement a solution to get this information locally with PostGIS.

I've seen this answer that solves the problem of how to find the closest road to the point… I've adapted it to my case as:

SELECT 
    way,
    osm_id,
    ST_Distance(way, 'SRID=3857;POINT(-3890646.5744145643 -899377.0801662721)'::geometry) 
FROM planet_osm_line r 
ORDER BY 2 ASC 
LIMIT 1;

It works fine and it returns the geometry and osm_id of the closest road from the point. However, even though the query returns after some seconds I'm in doubt if there are ways of optimizing the time of these results since in my case of use I want it to have a fast response to a large number of input points that arrive at the same time.

Should I worry about fragmenting the GeoFabrik data into smaller datasets (like a dataset with only the city that I'm interested) to have a faster response with the last query?

What strategies can I use to get a fast return of the osm_id and geometry from the OpenStreetMap database?

Best Answer

Use the 2D distance operator <->:

SELECT
    way,
    osm_id
FROM
    planet_osm_line AS osm
ORDER BY
    osm.way <-> 'SRID=3857;POINT(-3890646.5744145643 -899377.0801662721)'::GEOMETRY
LIMIT
    1
;

Related: