Chr*_*ris 9 postgresql postgis
我想计算点集群,并为每个集群获取特定属性的总和(比如,集群中每个点的得分总和)
我已经设法使用ST_ClusterWithin但是我无法计算总和.
这是我尝试过的:
SELECT sum(score), unnest(ST_ClusterWithin(coordinates, 0.1)) AS cluster
FROM locations
GROUP BY cluster;
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误 ERROR: aggregate functions are not allowed in GROUP BY
如果我删除了GROUP BY,我得到所有位置的得分总和,这不是我想要的(我想要群集中位置的总和)
这是一个棘手的问题,并且api的st_clusterwith似乎不太适合一般情况。
我能找到的唯一解决方案是重新加入集群,如下所示:
SELECT SUM(score), cluster FROM locations, (
SELECT unnest(ST_ClusterWithin(coordinates, 0.1)) AS cluster
FROM locations
) as location_clustered
WHERE ST_Contains(ST_CollectionExtract(cluster, 1), coordinates)
GROUP BY cluster;
Run Code Online (Sandbox Code Playgroud)
编辑:我已经改变ST_CollectionHomogenize到ST_CollectionExtract(<geometrycollection>, 1)(接1了点,2对线串和3多边形)作为这个答案建议:
https://gis.stackexchange.com/questions/195915/
因为这个错误:HTTPS://trac.osgeo .org / postgis / ticket / 3569
不要问我为什么你不能做ST_Contains(<geometrycollection>, <geometry>); 我们需要转换为允许作为参数的多点。
中继:这个问题本来可以与https://gis.stackexchange.com/完美匹配
| 归档时间: |
|
| 查看次数: |
1795 次 |
| 最近记录: |