Nearest feature queries
This is a key feature for almost any location-based application: given a current location, it will return the nearest feature (or list of nearest features, ordered by distance).
The naive approach to this problem would be to query the table ordering by ST_Distance
. Let's find the five earthquakes closest to San Juan:
SELECT * FROM data_import.earthquakes_subset_with_geom ORDER BY ST_Distance(geom::geography, ST_SetSRID(ST_MakePoint(-66.11,18.46),4326)::geography) LIMIT 5; id | time | depth | mag | magtype | place | geom ------------+--------------------------+-------+-----+---------+---------------------------------------------------+---------------------------------------------------- pr16281009 | 2016-10-08 01:08:46.4+02 | 5 | 2.5 | Md | 28km SE of El Negro, Puerto Rico | 0101000020E610000062A1D634EF6850C0E561A1D634DF3140...