如何使用PostGIS将多边形数据转换为线段

发布于 2024-12-07 01:44:17 字数 100 浏览 3 评论 0原文

我在 PostgreSQL/PostGIS 中有一个多边形数据表。现在我需要将此多边形数据转换为其相应的线段。谁能告诉我如何使用 PostGIS 查询进行转换。

提前致谢

I have a polygon data table in PostgreSQL/PostGIS. Now I need to convert this Polygon data into its corresponding line segments. Can anybody tell me how to convert it using PostGIS queries.

Thanks in Advance

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

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

发布评论

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

评论(3

浮光之海 2024-12-14 01:44:17

一般来说,将多边形转换为线可能并不简单,因为没有人一对一映射 和多边形的各种元素映射到不同的线串(外环、内环等)。

考虑到这一点,您需要按照如下可能的方法单独分割每个数据:

SELECT ST_AsText( ST_MakeLine(sp,ep) )
FROM
   -- extract the endpoints for every 2-point line segment for each linestring
   (SELECT
      ST_PointN(geom, generate_series(1, ST_NPoints(geom)-1)) as sp,
      ST_PointN(geom, generate_series(2, ST_NPoints(geom)  )) as ep
    FROM
       -- extract the individual linestrings
      (SELECT (ST_Dump(ST_Boundary(geom))).geom
       FROM mypolygontable
       ) AS linestrings
    ) AS segments;

根据 mypolygontable 中存储的多边形数据,您可能不仅需要转储边界(如上所述使用 >ST_Boundary),还有其他元素。上面的代码具有更详细的概述,取自 postgis-users 列表: 将多边形分割为 N 条线

对于 将线串或多边形分解为PostGIS 中的各个向量

Generally, converting polygon to line may be not straightforward because there is no one-to-one mapping and various elements of polygon map to different linestring (exterior ring, interior rings, etc.).

Considering that, you will need to split each of those separately following possible approach like this:

SELECT ST_AsText( ST_MakeLine(sp,ep) )
FROM
   -- extract the endpoints for every 2-point line segment for each linestring
   (SELECT
      ST_PointN(geom, generate_series(1, ST_NPoints(geom)-1)) as sp,
      ST_PointN(geom, generate_series(2, ST_NPoints(geom)  )) as ep
    FROM
       -- extract the individual linestrings
      (SELECT (ST_Dump(ST_Boundary(geom))).geom
       FROM mypolygontable
       ) AS linestrings
    ) AS segments;

depending on what polygon data are stored in mypolygontable, you may want to dump not only the boundary (as above using ST_Boundary) but also other elements. The code above with more detailed overview is taken from the postgis-users list: Split a polygon to N linestrings

There is also a generic approach to the problem explained in Exploding a linestring or polygon into individual vectors in PostGIS

请叫√我孤独 2024-12-14 01:44:17

PostGIS 3.2.0(2021 年 12 月)开始,您可以< a href="https://postgis.net/docs/ST_DumpSegments.html" rel="nofollow noreferrer">ST_DumpSegments() 直接。它甚至可以处理从多边形中提取的所有环(不仅仅是外部/边界):
db<>fiddle 演示

select st_astext((st_dumpsegments('polygon((0 0,1 2,3 3,0 0))')).geom);
st_astext
LINESTRING(0 0,1 2)
LINESTRING (1 2,3 3)
LINESTRING(3 3,0 0)

上面的示例调用 SRF选择 部分而不是通常应位于的 from 列表中,并且它会忽略部分转储。这可能更清晰一些:

select dmp.ordinality
      ,dmp.path
      ,st_astext(dmp.geom)
from (values('polygon((0 0,1 2,3 3,0 0))'))as a(g)
cross join lateral st_dumpsegments(g)with ordinality as dmp;
序数路径st_astext
1{1,1}LINESTRING(0 0,1 2)
2{1,2}LINESTRING(1 2,3 3)
3{1,3}LINESTRING(3 3,0 0)

WITH ORDINALITY 对于单环多边形(单线串)来说非常方便,因为您可以直接获取线段编号,而无需到达 路径内部 数组。
对于多环,path 会加宽以保存该段来自哪个环的信息,而 ordinality 则不会显示这种区别。

Since PostGIS 3.2.0 (December 2021), you can ST_DumpSegments() directly. It even handles all ring extractions from the polygon (not just the exterior/boundary):
demo at db<>fiddle

select st_astext((st_dumpsegments('polygon((0 0,1 2,3 3,0 0))')).geom);
st_astext
LINESTRING(0 0,1 2)
LINESTRING(1 2,3 3)
LINESTRING(3 3,0 0)

The example above calls the SRF in the select section instead of the from list it should normally be in, and it ignores part of the dump. This might be a bit cleaner:

select dmp.ordinality
      ,dmp.path
      ,st_astext(dmp.geom)
from (values('polygon((0 0,1 2,3 3,0 0))'))as a(g)
cross join lateral st_dumpsegments(g)with ordinality as dmp;
ordinalitypathst_astext
1{1,1}LINESTRING(0 0,1 2)
2{1,2}LINESTRING(1 2,3 3)
3{1,3}LINESTRING(3 3,0 0)

WITH ORDINALITY can come handy for single-ring polygons (single linestring) because you get the segment number directly, without having to reach inside the path array.
For multi-ring, path widens to holds the information which ring the segment came from, while ordinality doesn't show that distinction.

静谧 2024-12-14 01:44:17

当你搜索这个问题时,这是谷歌上的第一个点击。我不知道是否已经过去了这么长时间才创建了一个函数,但对于未来的谷歌用户来说 ST_ExteriorRings(geom) 对我来说非常有用。
http://postgis.net/docs/ST_ExteriorRing.html

SELECT ST_ExteriorRing(ST_Dump(geom)).geom)
FROM foo

This is the first hit on google when you search this problem. I don't know if so much time has passed a function has been created since, but for future googlers ST_ExteriorRings(geom) worked great for me.
http://postgis.net/docs/ST_ExteriorRing.html

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