[GIS] Find the farthest point

postgispostgresql

I have a table with 2000 records and with those columns
OutletName, longitude, latitude, geom

I would like to find a way to find the farthest outlet between a selection of outlets

For example we have outletnames : A,B,C,D,E,F,G,H,I,J,K...

And we would like to know who is the farthest outlet from A
between those outlets 'B' 'F' and 'D'

Now I am doing it with lot of queries calculating distances between all of them and comparing the distances in java, but it takes time. Is there a better way to do it?

Best Answer

If it is just 2000 records and only points (no complex polygons or linestrings) it shouldn't take many milliseconds to calculate.

If it would have been a few million points to check distance between you could consider using knn-distance with PostgreSQL 9.1 and PostGIS 2.0.

But I think you should do fine with something like?

SELECT b."OutletName" FROM the_table AS a, the_table AS b 
WHERE a."OutletName" = 'A' AND b."OutletName" in ('B', 'F', 'D') 
ORDER BY ST_Distance(a.geom, b.geom) desc
limit 1;

The bottleneck in this query will not be the spatial calculation since it is only three distance calculations between points, but to find the OutletName fast. Because of that you will maybe save a efw milliseconds if you put an index n the OutletName column.

edit:
From Whubers comment I guess I am misunderstanding something here, but I let it be for now.

HTH

Nicklas

Related Question