PostGIS:多边形的边界框

发布于 2024-09-26 21:01:44 字数 763 浏览 0 评论 0原文

SELECT id, ST_Box2D(areas) AS bbox FROM mytable;

在此示例中,table“mytable”包含两列:“id”是行的唯一 ID 号,“areas”是每行包含一个 MULTIPOLYGON几何字段。


这对于仅包含一个多边形的多重多边形来说效果很好,但某些行的多边形非常分散,因此当多重多边形包含欧洲的一个多边形和加拿大的一个多边形时,边界框不相关。

所以我需要一种方法来为每个多边形每个多边形获取一个 box2d,但我还没有找到如何实现。 更准确地说,我的目标是每行返回一个多多边形,每个多边形包含一个 box2d。


第一个示例

  • id:123
  • area:澳大利亚仅包含一个椭圆形多边形的多多边形,
  • 因此 bbox 应返回仅包含一个矩形的多多边形(边界澳大利亚的

第二个示例

  • id:321
  • area:包含巴黎一个圆、多伦多一个圆的多边形,
  • 因此 bbox 应返回一个多边形包含巴黎 1 个矩形、多伦多 1 个矩形
SELECT id, ST_Box2D(areas) AS bbox FROM mytable;

In this example, the table "mytable" contains two columns: "id" is the unique id number of the row and "areas" is a geometry field containing one MULTIPOLYGON per row.


This works fine for multipolygons containing only one polygon, but some rows have polygons very spread apart, hence the bounding box is not relevant when the multipolygon contains one polygon in Europe and one in Canada for example.

So I would need a way to get one box2d per polygon per multipolygon, but I haven't found how just yet.
More exactly, my goal is to return one multipolygon per row, containing one box2d per polygon.


First example

  • id: 123
  • area: a multipolygon containing only one oval polygon in Australia
  • therefore bbox should return a multipolygon containing only one rectangle (the bounding box) in Australia

Second example

  • id: 321
  • area: a multipolygon containing one circle in Paris, one circle in Toronto
  • therefore bbox should return a multipolygon containing one rectangle in Paris, one rectangle in Toronto

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2024-10-03 21:01:44

您应该使用 ST_Dump https://postgis.net/docs/ST_Dump.html

然后你将得到每个多边形一行。当几何图形被分割时,其他字段将被复制。它就像一个聚合函数,但方式相反。

语法有点特殊,因为它输出复合数据类型,因此您必须像这样提取几何部分:

SELECT (ST_Dump(the_geom)).geom from mytable;

因为这会在表中提供更多行,所以您应该从查询中创建一个新表。

然后您可以在新表中的新几何列上创建索引,它将构建在每个多边形的边界框上。

HTH

/Nicklas

您是否也希望多边形各占一行?这就是我的想法,但如果你只想要一张带有 bbox 的表,每行一个 id 引用原始多边形(你当然会得到对多边形的每个部分重复的相同 id),那么你可以通过以下方式执行相同的操作:只是提取 bbox 之类的内容:

CREATE TABLE newTable AS
SELECT id, BOX2D((ST_Dump(the_geom)).geom) AS myBox FROM originamTable

恐怕我并没有真正得到你想要的东西,但是在这种情况下,你可以使用 ST_Dump 有很多可能性。

You should use ST_Dump https://postgis.net/docs/ST_Dump.html

Then you will get one row per polygon. The other fields will be duplicated when the geometry is split. It is like an aggregate function but the other way.

The syntax gets a little special since it outputs a compound data type so you have to extract the geometry part like this:

SELECT (ST_Dump(the_geom)).geom from mytable;

since this gives you more rows in the table you should just make a new table from the query.

then you can just create an index on that new geometry column in the new table and it will be built on bounding boxes for each single polygon.

HTH

/Nicklas

Do you want your polygons too at one row each? That is what I thought, but if you want only a table with bboxes, one per row with an id references the original multipolygon (you will of cource get the same id repeated for every part of the multipolygon) then you can do the same byt just extracting the bboxes something like:

CREATE TABLE newTable AS
SELECT id, BOX2D((ST_Dump(the_geom)).geom) AS myBox FROM originamTable

I am afraid I don't really get what you want, but you have a lot of possibilities with ST_Dump in cases like this.

天生の放荡 2024-10-03 21:01:44

您必须分别装箱相关部分(例如加拿大和法国部分)。 PostGIS 中最好的工具是几何访问器 ST_GeometryN(geometry,int) (参考:http://postgis.refractions.net/docs/ST_GeometryN.html)。该链接有一个将访问器与 ST_NumGeometries 相结合的很好的示例。

每个评论的更新:

这是来自旧金山的一个简单示例 - 该表包含一个名为 the_geom 的几何字段,gid 记录 1 是一个具有两个多边形的字段,如 < code>st_numgeometries (注意序数索引为 1 而不是 0):

=> select st_box2d(st_geometryn(the_geom, 1)) from tl_2009_06075_cousub00 \
 where gid = 1;

                                st_box2d                                 
-------------------------------------------------------------------------
 BOX(-123.173828125 37.6398277282715,-122.935707092285 37.8230590820312)
(1 row)

=> select st_box2d(st_geometryn(the_geom, 2)) from tl_2009_06075_cousub00 \
 where gid = 1;

                                  st_box2d                                  
----------------------------------------------------------------------------
 BOX(-122.612289428711 37.7067184448242,-122.281776428223 37.9298248291016)
(1 row)

You would have to box the relevant bits (say the Canadian and French components) separately. The best tool for this in PostGIS is the geometry accessor ST_GeometryN(geometry,int) (reference: http://postgis.refractions.net/docs/ST_GeometryN.html ). That link has a good example of combining the accessor with ST_NumGeometries.

UPDATE PER COMMENT:

Here is a simple example from San Francisco -- this table contains a geometry field called the_geom, gid record 1 is a field with two multipolygons as reported by st_numgeometries (note the ordinal is indexed at 1 not 0):

=> select st_box2d(st_geometryn(the_geom, 1)) from tl_2009_06075_cousub00 \
 where gid = 1;

                                st_box2d                                 
-------------------------------------------------------------------------
 BOX(-123.173828125 37.6398277282715,-122.935707092285 37.8230590820312)
(1 row)

=> select st_box2d(st_geometryn(the_geom, 2)) from tl_2009_06075_cousub00 \
 where gid = 1;

                                  st_box2d                                  
----------------------------------------------------------------------------
 BOX(-122.612289428711 37.7067184448242,-122.281776428223 37.9298248291016)
(1 row)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文