如何使用PostGIS将多边形数据转换为线段
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,将多边形转换为线可能并不简单,因为没有人一对一映射 和多边形的各种元素映射到不同的线串(外环、内环等)。
考虑到这一点,您需要按照如下可能的方法单独分割每个数据:
根据
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:
depending on what polygon data are stored in
mypolygontable
, you may want to dump not only the boundary (as above usingST_Boundary
) but also other elements. The code above with more detailed overview is taken from the postgis-users list: Split a polygon to N linestringsThere is also a generic approach to the problem explained in Exploding a linestring or polygon into individual vectors in PostGIS
自 PostGIS 3.2.0(2021 年 12 月)开始,您可以< a href="https://postgis.net/docs/ST_DumpSegments.html" rel="nofollow noreferrer">
ST_DumpSegments()
直接。它甚至可以处理从多边形中提取的所有环(不仅仅是外部/边界):db<>fiddle 演示
上面的示例调用 SRF 在
选择
部分而不是通常应位于的from
列表中,并且它会忽略部分转储。这可能更清晰一些: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
The example above calls the SRF in the
select
section instead of thefrom
list it should normally be in, and it ignores part of the dump. This might be a bit cleaner:WITH ORDINALITY
can come handy for single-ring polygons (single linestring) because you get the segment number directly, without having to reach inside thepath
array.For multi-ring,
path
widens to holds the information which ring the segment came from, whileordinality
doesn't show that distinction.当你搜索这个问题时,这是谷歌上的第一个点击。我不知道是否已经过去了这么长时间才创建了一个函数,但对于未来的谷歌用户来说 ST_ExteriorRings(geom) 对我来说非常有用。
http://postgis.net/docs/ST_ExteriorRing.html
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