2

I imported an OSM file for the state of Delaware into the PostGIS Database via connecting to it through QGIS. I would like to build a query to retrieve the nearest roads to the (latitude,longitude) points I have.

When I imported to QGIS, it created 4 layers: one for linestring, one for points, one for multilinestrings, a and once for multi polygons. I believe the only ones I need would be lines. So far I have imported my table of 1000 ( latitude, longitude) points. I need to write a query that for each point, will return the highway tag of the nearest road to that point. I've written a query that casts each latitude longitude into a point geometry:

select ST_AsText(ST_MakePoint(end_latitude::float, end_longitude::float)
from gis.delaware_trip_slices

Each row output here would be for example Point(37.2,75.32) etc. Now, I just have to find a way to return the highway tag for the nearest road to each of these points. Do you have any pointers how I would go about doing this.

I saw a query like this online, but I don't know what this means:

Select *
From towns 
order by the_geom <->
SETSRID(ST_Make_Point(lat,long),4236) 
Limit 5

My guess is somehow I could make the above query a temporary table. Then I would for each value in the temporary table search for the nearest road. Does anyone have any pointers

TomazicM
  • 25,601
  • 22
  • 29
  • 39
  • 2
    You'll find all relevant information about the concept needed and its implementation in e.g. this answer and its links, and/or via the tag [tag:knn] on GIS.SE – geozelot Oct 02 '23 at 18:42
  • see the operator doc, 2) coordinates should be in the longitude;latitude format, 3) beware that <-> uses the Cartesian distance, which is meaningless with geometries in 4236
  • – JGH Oct 02 '23 at 19:44