我想计算点簇,并为每个簇获得特定属性的总和(比如说,簇中每个点的得分总和)
我已经设法使用 ST_ClusterWithin
构建集群,但我无法计算总和。
这是我尝试过的:
SELECT sum(score), unnest(ST_ClusterWithin(coordinates, 0.1)) AS cluster
FROM locations
GROUP BY cluster;
但我收到以下错误 ERROR: aggregate functions are not allowed in GROUP BY
如果我删除 GROUP BY
,我会得到所有位置的分数总和,这不是我想要的(我想要集群中位置的总和)
最佳答案
这是一个棘手的问题,st_clusterwithin api 似乎没有针对常见情况设计得很好。
我能找到的唯一解决方案是重新加入集群,如下所示:
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;
编辑:我已经更改了 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>)
;
我们需要转换为允许作为参数的多点。
Meta:这个问题很适合 https://gis.stackexchange.com/
关于postgresql - 与其他聚合的 PostGis 聚类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35482754/