[postgis-users] identfying a nearest point

Stephen Woodbridge woodbri at swoodbridge.com
Thu Apr 23 07:28:36 PDT 2009


Stephen Woodbridge wrote:
> Supunmali Ahangama wrote:
>> Please suggest me with the POSTGIS/pgrouting function to identify the 
>> nearest
>> point from set of points (vertices in a graph) to a particular point
>> (outside the graph)? 
> 
> You typically want to search within some radius so you do not have to do 
> a full table scan.
> 
> select * from vertices_tmp where st_expand(setsrid(makepoint(x,y),4326)) 
> && the_geom order by distance(setsrid(makepoint(x,y),4326),the_geom) asc 
> limit 1;
> 
> Should do what you want.
> 
> -Steve

Oops, forgot to put the raduis in:

select * from vertices_tmp where 
st_expand(setsrid(makepoint(x,y),4326),radius)
&& the_geom order by distance(setsrid(makepoint(x,y),4326),the_geom) asc
limit 1;

-Steve



More information about the postgis-users mailing list