[postgis-users] Closest point problem(s)

Kevin Neufeld kneufeld at refractions.net
Thu Jul 30 08:25:30 PDT 2009


Yes, your second query is the typical way to solve the nearest neighbour problem.  If the query returns nothing, expand 
your bounding box and repeat.

But if you know that at least some points are within 5 miles, try using ST_DWithin.

http://postgis.refractions.net/documentation/manual-svn/ST_DWithin.html
The example show how to compute the closest hospital for every school.  You could do something similar and find the 
closet point for every ship.

Of course, you're going to have to use a valid SRID (not -1) where "5 miles" makes sense.

Cheers,
Kevin

Thurber, Fred wrote:
> I think I need to clarify my previous post. It is really two-part.  Here 
> is the first part:
>  
> Closest point problem:
> -------------------------------------------------------------------------------------------------------------------------
> Given a point, find the closest point in our data to it. I was thinking 
> of a query such as this from an old posting 
> (http://postgis.refractions.net/pipermail/postgis-users/2005-June/008262.html):
>  
> SELECT the_point
> FROM foo
> ORDER BY distance(the_point, GeomFromText('POINT(-80 45)', -1))
> LIMIT 1;
>  
> But this must doia full table scan  The post had a way to first get a 
> bounding box and then do the distance query to cut down the search size 
> (http://postgis.refractions.net/pipermail/postgis-users/2005-June/008262.html):
>  
> SELECT the_point
> FROM foo
> WHERE the_point && 'BOX3D(-180 -55, 20 145)'::box3d
> ORDER BY distance(the_point, GeomFromText('POINT(-80 45)', -1))
> LIMIT 1;
>  
> Is there a better way to do this?
>  
>  
> A lot of closest point problems:
> ---------------------------------------------------------------------------------------------------------------------------------
> My second question is this: I will be running this query 100-200 times, 
> once for each item in my query. We are modeling a ship steaming in a 
> straight line, every 5 miles we will want to find the closest point in 
> our data to the ship. Is there a way to do this query in one step 
> instead of solving 100-200 closest point problems?
>  
>  
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users



More information about the postgis-users mailing list