How to convert polygon data into line segments using PostGIS

前端 未结 2 2063
忘了有多久
忘了有多久 2020-12-31 09:11

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

相关标签:
2条回答
  • 2020-12-31 09:36

    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

    0 讨论(0)
  • 2020-12-31 09:56

    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

    0 讨论(0)
提交回复
热议问题