我正在尝试执行一个订单查询来查找最接近 current_user 的记录。
我知道两点之间的距离是:current_location.euclidean_distance(@record.position)
如何将其处理到 PostGIS(或 active_record/spatial_adapter)查询中?
要获得最接近的 5 个:
SELECT * FROM your_table
ORDER BY ST_Distance(your_table.geom, ST_Geomfromtext(your point as wkt))
limit 5;
如果您有一个大数据集并且知道您不想搜索超过 1 公里的距离,那么如果您这样做,查询将会更有效:
SELECT * FROM your_table
WHERE ST_DWithin(your_table.geom, ST_Geomfromtext(your point as wkt, 1000)
ORDER BY ST_Distance(your_table.geom, ST_Geomfromtext(your point as wkt))
limit 5;
/Nicklas
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)