按最近的顺序排序-PostGIS,GeoRuby,spatial_adapter [英] Order by nearest - PostGIS, GeoRuby, spatial_adapter
本文介绍了按最近的顺序排序-PostGIS,GeoRuby,spatial_adapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试进行订单查询,以查找离current_user最近的记录.
I'm trying to do an order query that finds records nearest to the current_user.
我知道两点之间的距离是:current_location.euclidean_distance(@record.position)
I know the distance between the two points is: current_location.euclidean_distance(@record.position)
如何将其用于PostGIS(或active_record/spatial_adapter)查询?
How can I work this into a PostGIS (or active_record/spatial_adapter) query?
推荐答案
获取最接近的5个:
SELECT * FROM your_table
ORDER BY ST_Distance(your_table.geom, ST_Geomfromtext(your point as wkt))
limit 5;
如果您有一个庞大的数据集,并且知道您不想进行进一步搜索,例如1公里,那么这样做将更有效:
If you have a big dataset and know that you don't want to search further than , say 1 km, the query will be more efficient if you do:
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;
/尼古拉斯
这篇关于按最近的顺序排序-PostGIS,GeoRuby,spatial_adapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文