[英]How to get bbox of grouped geometries using postgis queries?
I have a points table in my postgresql table.我的 postgresql 表中有一个积分表。
CREATE TABLE my_points(
gid serial PRIMARY KEY,
created_on TIMESTAMP NOT NULL,
geog geography(POINTZ,4326)
);
So I want to get bounded boxes of updated data which grouped by created_on.所以我想获得由 created_on 分组的更新数据的边界框。 The updated datas today are different locations.今天更新的数据是不同的位置。
For example table data is like this:例如表数据是这样的:
gid created_on geog
------------------------------------
1 08/15/2021 10:38:11 (1,2)
2 08/15/2021 10:38:11 (2,2)
3 08/15/2021 10:38:11 (3,2)
4 08/15/2021 11:12:04 (1,2)
5 08/15/2021 11:12:04 (2,4)
In this table there are two groups by date.在此表中,按日期分为两组。 08/15/2021 10:38:11 has ids (1,2,3) and 08/15/2021 11:12:04 has ids (4,5 08/15/2021 10:38:11 有 ids (1,2,3) 和 08/15/2021 11:12:04 有 ids (4,5
So I need a select query for two bounded boxes to gets grouped by created_on date.因此,我需要一个 select 查询来获取两个边界框,以便按 created_on 日期进行分组。
I need a seelct query to find blue square geoemtries.我需要一个 seelct 查询来查找蓝色方形 geoemtries。
How can I select this?我怎么可以select这个?
Create a cluster of points with ST_Union
and GROUP BY
and then use either ST_Envelope
or ST_Extent
to draw the bounding box:使用ST_Union
和GROUP BY
创建点簇,然后使用ST_Envelope
或ST_Extent
绘制边界框:
Returns the minimum bounding box for the supplied geometry, as a geometry:返回所提供几何体的最小边界框,作为几何体:
SELECT
ST_Envelope(
ST_Union(geog::geometry))
FROM my_points
GROUP BY created_on;
Retrieves a BBOX of given geometry or group of geometries:检索给定几何或几何组的 BBOX:
WITH j (created_on,geog) AS (
SELECT
created_on, ST_Union(geog::geometry)
FROM my_points
GROUP BY created_on
)
SELECT ST_Extent(geog) FROM j
GROUP BY created_on;
Demo: db<>fiddle
演示: db<>fiddle
CREATE TABLE my_points(
gid serial PRIMARY KEY,
created_on TIMESTAMP NOT NULL,
geog geography(POINT,4326)
);
INSERT INTO my_points VALUES
(1,'2021-08-15 10:38:11','SRID=4326;POINT(-4.481927586167595 54.32254424440715)'),
(2,'2021-08-15 10:38:11','SRID=4326;POINT(-4.44759531077697 54.28408149183809)'),
(3,'2021-08-15 10:38:11','SRID=4326;POINT(-4.563638401597283 54.29169676415854)'),
(4,'2021-08-15 11:12:04','SRID=4326;POINT(-4.52449960765197 54.23234056232733)'),
(5,'2021-08-15 11:12:04','SRID=4326;POINT(-4.478494358628533 54.1893743942604)');
Result:结果:
WITH j (created_on,geog) AS (
SELECT
created_on, ST_Union(geog::geometry)
FROM my_points
GROUP BY created_on
)
SELECT ST_Extent(geog) FROM j
GROUP BY created_on;
st_extent
-------------------------------------------------------------------------------
BOX(-4.563638401597283 54.28408149183809,-4.44759531077697 54.32254424440715)
BOX(-4.52449960765197 54.1893743942604,-4.478494358628533 54.23234056232733)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.