【问题标题】:PostGis clustering with other aggregatePostGis 与其他聚合的聚类
【发布时间】:2016-05-30 16:40:53
【问题描述】:

我想计算点簇,并为每个簇获取特定属性的总和(比如说,簇中每个点的得分总和)

我已经设法使用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,我会得到所有位置的分数总和,这不是我想要的(我想要集群中位置的总和)

【问题讨论】:

  • 尝试用另一个选择包装它,并在外部选择中进行分组 .... SELECT sum,cluster FROM(YOUR QUERY) group by cluster
  • 无法正常工作。为了在我的外部查询中求和,我需要在我的内部查询中对 score 属性进行分组或聚合(因为 ST_Clusterwithin 已经是一个聚合函数)

标签: postgresql postgis


【解决方案1】:

使用 PostGIS 2.3,可能会受益于 ST_ClusterDBSCAN 函数(第三个参数的选择将其简化为层次聚类),该函数直接返回相应的集群索引:

WITH stat AS (
  SELECT
    score, ST_ClusterDBSCAN(coordinates, 0.1, 1) OVER () AS cluster_id
  FROM
    tmp_locations
)
SELECT
  cluster_id, SUM(score)
FROM
  stat
GROUP BY
  cluster_id
ORDER BY
  cluster_id

【讨论】:

    【解决方案2】:

    这是一个棘手的问题,而且 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/

    【讨论】:

    • 外部选择中应该是“FROM 位置”而不是“FROM 位置”吗?
    猜你喜欢
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多