在多个位置的距离内找到位置
我正在尝试运行一个查询,该查询将找到任何给定位置的给定距离的位置。这是用于搜索结果,用户可以选择多个位置进行查看。
我当前的方法是使用st_closestpoint
并传递st_point
在PHP中生成的数组。然后,我将该字符串传递到st_collect
。
$points = $locations->map(function ($location) {
return sprintf('ST_Point(%s, %s)', $location->longitude, $location->latitude);
})->implode(', ');
SELECT *
FROM listing_locations
WHERE ST_DWithin(
coordinate,
ST_ClosestPoint(coordinate, ST_Collect(Array[%s]),
1000,
FALSE)
但是,这是行不通的,因为它看起来像st_closestpoint
不喜欢这些混合参数:
SQLSTATE[42883]: Undefined function: 7 ERROR: function st_closestpoint(geometry, geography) does not exist
我有gist(coortion :: geagraphy)
似乎很有用。
我缺少什么 - 有更好的方法可以做到这一点,还是这是一种不好的方法?我应该每次都在其他位置执行查询吗?
I'm trying to run a query that will find locations with a given distance of any of the given locations. This is for a search result where users can select multiple locations to look around.
My current approach is to use ST_ClosestPoint
and pass in an array of ST_Point
generated in PHP. Then I pass that string into ST_Collect
.
$points = $locations->map(function ($location) {
return sprintf('ST_Point(%s, %s)', $location->longitude, $location->latitude);
})->implode(', ');
SELECT *
FROM listing_locations
WHERE ST_DWithin(
coordinate,
ST_ClosestPoint(coordinate, ST_Collect(Array[%s]),
1000,
FALSE)
However, this doesn't work because it looks like ST_ClosestPoint
doesn't like these mixed arguments:
SQLSTATE[42883]: Undefined function: 7 ERROR: function st_closestpoint(geometry, geography) does not exist
I have a gist(coordinate::geography)
index on listing_locations
which seems like it would be useful to use.
What am I missing - is there a better way to do this, or is this a bad approach? Should I be performing the query each time with a different location?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使事情变得更简单,将
st_collect
放在子查询或CTE中,在外部查询中使用st_dwithin
与新创建的多点,列coortion
以及给定的距离,例如或将其放入空间连接(以恕我基本不阅读)
演示中:
To make things simpler put the
ST_Collect
in a subquery or CTE, and in the outer query useST_DWithin
with the newly created MultiPoint, the columncoordinate
and a given distance, e.g.Or put it in a spatial join (imho less readable)
Demo:
db<>fiddle