How to convert polygon data to line segments using PostGIS

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

Thanks at Advance

+8
sql postgresql gis postgis
source share
2 answers

As a rule, converting a polygon to a string may not be easy, because there is no unique display and different elements of the polygonal map for different lines (outer ring, inner rings, etc.).

Given this, you need to separate each of them according to the following possible approach, for example:

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 is stored in mypolygontable , you may want to reset not only the border (as described above using ST_Boundary ), but also other elements. The code above with a more detailed overview is taken from the postgis-users list: Split the polygon into N linestrings

There is also a general approach to the problem described in Line break or polygon into separate vectors in PostGIS

+16
source share

This is the first hit on google when looking for this problem. I don’t know if there has been so much time that the function was created since then, but for future googlers ST_ExteriorRings (geom) did a great job for me. http://postgis.net/docs/ST_ExteriorRing.html

0
source share

All Articles