如何使用 postgis 查询获取分组几何的 bbox?
How to get bbox of grouped geometries using postgis queries?
我在我的 postgresql table 中有一个点 table。
CREATE TABLE my_points(
gid serial PRIMARY KEY,
created_on TIMESTAMP NOT NULL,
geog geography(POINTZ,4326)
);
所以我想获取按 created_on 分组的更新数据的边界框。今天更新的数据是不同的位置。
例如table数据是这样的:
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)
在此 table 中,按日期分为两组。 08/15/2021 10:38:11 有 ID (1,2,3) 和 08/15/2021 11:12:04 有 ID (4,5
所以我需要 select 查询两个边界框以按 created_on 日期分组。
我需要一个 seelct 查询来查找蓝色方形几何图形。
我怎样才能select这个?
使用 ST_Union
和 GROUP BY
创建点簇,然后使用 ST_Envelope
or ST_Extent
之一绘制边界框:
ST_Envelope
Returns 所提供几何体的最小边界框,作为几何体:
SELECT
ST_Envelope(
ST_Union(geog::geometry))
FROM my_points
GROUP BY created_on;
ST_Extent
检索给定几何或几何组的 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;
演示: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)');
结果:
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)
我在我的 postgresql table 中有一个点 table。
CREATE TABLE my_points(
gid serial PRIMARY KEY,
created_on TIMESTAMP NOT NULL,
geog geography(POINTZ,4326)
);
所以我想获取按 created_on 分组的更新数据的边界框。今天更新的数据是不同的位置。
例如table数据是这样的:
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)
在此 table 中,按日期分为两组。 08/15/2021 10:38:11 有 ID (1,2,3) 和 08/15/2021 11:12:04 有 ID (4,5
所以我需要 select 查询两个边界框以按 created_on 日期分组。
我需要一个 seelct 查询来查找蓝色方形几何图形。
我怎样才能select这个?
使用 ST_Union
和 GROUP BY
创建点簇,然后使用 ST_Envelope
or ST_Extent
之一绘制边界框:
ST_Envelope
Returns 所提供几何体的最小边界框,作为几何体:
SELECT
ST_Envelope(
ST_Union(geog::geometry))
FROM my_points
GROUP BY created_on;
ST_Extent
检索给定几何或几何组的 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;
演示: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)');
结果:
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)