I have such problem and I would be nice If somebody can help me.
I have points table with GIST index. Those points don’t change in time.
I would like to fetch points that are near some given linestring.
Example: Imagine that linestring is the road and points are poi along the road.
I would like to fetch poi’s that are in 5 km distance from the given road. I would like to fetch those pois in correct order (driving order along the road). Look at the image:

For given road from point 1 to 5 i would like to fetch POIs that is in 5 km max from the road and in order from point 1 to 5 along the road. So the result should be:
POI_ID
1
5
6
8
9
10
12
13
This should tell me what POI I can visit during traveling along the road with minimum cost.
Does anybody have some ideas how to do it with postgres and postgis?
Assuming you have geometry columns
geomthat use a projected SRID of meters in tablesroad(LINESTRING) andpoi(POINT), your query to find all POIs within 5 km of road (where id = 123) should be something like:The first
ORDERpart withST_LineLocatePointuses a fraction between 0.0 and 1.0, depending where the point is along the LINESTRING. If the direction of the road is going “the wrong way”, then appendDESCto reverse the order. The second ORDER part is based on distance, which could be used if the point is slightly past the start/end of the LINESTRING (whereST_LineLocatePointwould return 0.0 or 1.0, respectively).This query might also work if you are using the
geographytype with Long/Latitude values, since it automagically calculates metres — not degrees. Check out docs for more:ST_DistanceST_DWithinST_LineLocatePoint(orST_Line_Locate_Pointfor older versions of PostGIS)