如何通过两列聚合多个点并从中创建一个LineString / MultiLineString

时间:2018-08-29 08:14:03

标签: sql postgresql postgis

我有一个名为location的表,其中包含以下行:

id uuid NOT NULL,
"deviceId" text COLLATE pg_catalog."default",
"userId" uuid,
"userName" text COLLATE pg_catalog."default",
"creationDateTime" timestamp with time zone,
shape geometry,
CONSTRAINT id PRIMARY KEY (id)

想象一下,我的用户每小时在该表的shape列中注册点。当注册该点的时间到表中时,像这样creationDateTime保存到2018-08-22 00:03:41.649+04:30列中。

我如何提取此信息:

each User ---- each day ---- list of geometry(shape column) 例如:

  

第一天的User1包含几何点列表。   第二天的User1包含几何点列表,依此类推...

我通过mongo对同一项目进行了此查询:

{$project: {
    _id: 0,
    uId : "$UserId",
    dId : "$DeviceId",
    ts :"$CreationDateTime",
    point : "$Point"
    }
 }, 
{$group: {
    _id :{
        did: "$dId",
        day: { $dayOfMonth: "$ts" }
    },
    docs: { $push: "$$ROOT" }
     }
 },

 {
    $sort:{"_id.day": -1}
 }

但是我怎么用postgresql做到这一点?在postgre上不存在这种聚合,而在postgresql上我是新来的。 这是我的查询:

(Select test1."deviceId",test1."shape", test1."creationDateTime" From   
    (Select * from locations) as test1 Group By test1."deviceId",test1."shape",test1."creationDateTime"
ORDER BY  test1."creationDateTime")

此查询不适合结果,我知道此查询有问题。 deviceId 用户经常在其他行中重复。我该如何处理?

最后,我想创建多段线per user - per day - multi poly line

1 个答案:

答案 0 :(得分:2)

大概有一百种方法可以回答这个问题。这是其中之一:

考虑您的表结构..

CREATE TEMPORARY TABLE locations
(id uuid,
deviceId text COLLATE pg_catalog."default",
userId uuid,
userName text COLLATE pg_catalog."default",
creationDateTime timestamp with time zone,
shape geometry);

..这些样本数据..

INSERT INTO locations (userId, creationDateTime, shape) 
VALUES ('d1166a84-ab66-11e8-98d0-529269fb1459',CURRENT_DATE,'POINT(-1.25 51.75)'),
       ('d1166a84-ab66-11e8-98d0-529269fb1459',CURRENT_DATE,'POINT(-1.15 52.96)'),
       ('d1166a84-ab66-11e8-98d0-529269fb1459',CURRENT_DATE,'POINT(-0.13 50.82)'),
       ('d1166a84-ab66-11e8-98d0-529269fb1459',CURRENT_DATE-1,'POINT(-2.22 53.48)'),
       ('d1166a84-ab66-11e8-98d0-529269fb1459',CURRENT_DATE-1,'POINT(-0.11 51.51)');

..您可以汇总每个用户+日期的积分,并使用ST_MakeLineLINESTRING创建一个GROUP BY

SELECT userId, creationDateTime, ST_AsText(ST_MakeLine(shape))
FROM locations
GROUP BY userId, creationDateTime
ORDER BY  creationDateTime;

                userid                |    creationdatetime    |                    st_astext                    
--------------------------------------+------------------------+-------------------------------------------------
 d1166a84-ab66-11e8-98d0-529269fb1459 | 2018-08-28 00:00:00+02 | LINESTRING(-2.22 53.48,-0.11 51.51)
 d1166a84-ab66-11e8-98d0-529269fb1459 | 2018-08-29 00:00:00+02 | LINESTRING(-1.25 51.75,-1.15 52.96,-0.13 50.82)
(2 Zeilen)

d1166a84-ab66-11e8-98d0-529269fb1459中用户2018-08-28 00:00:00+02的图形描述 enter image description here

您可以用相同的方式使用ST_Collect创建一个MULTIPOINT

SELECT userId, creationDateTime, ST_AsText(ST_Collect(shape))
FROM locations
GROUP BY userId, creationDateTime
ORDER BY  creationDateTime;

                userid                |    creationdatetime    |                    st_astext                    
--------------------------------------+------------------------+-------------------------------------------------
 d1166a84-ab66-11e8-98d0-529269fb1459 | 2018-08-28 00:00:00+02 | MULTIPOINT(-2.22 53.48,-0.11 51.51)
 d1166a84-ab66-11e8-98d0-529269fb1459 | 2018-08-29 00:00:00+02 | MULTIPOINT(-1.25 51.75,-1.15 52.96,-0.13 50.82)
(2 Zeilen)

enter image description here

编辑-使用CTE(又称WITH子句)为每个用户(LINESTRINGS)每天创建一组MULTILINESTRING

WITH j AS (
  SELECT userId, creationDateTime, ST_MakeLine(shape) AS shape
  FROM locations
  GROUP BY userId, creationDateTime)
SELECT userId, ST_AsText(ST_Collect(shape))
FROM j
GROUP BY userId

                userid                |                                    st_astext                                     
--------------------------------------+----------------------------------------------------------------------------------
 d1166a84-ab66-11e8-98d0-529269fb1459 | MULTILINESTRING((-2.22 53.48,-0.11 51.51),(-1.25 51.75,-1.15 52.96,-0.13 50.82))
(1 

enter image description here

基本上,您需要对所需的记录(在这种情况下为用户和日期)进行分组,并使用您选择的汇总功能,例如ST_MergeLineST_CollectST_UnionST_Multi等。