R sf Package – How to Convert a Polygon to a Polyline

polygonrsf

I have some simple polygons that I would like to convert to polylines using the sf package in R. I have found st_polygonize to convert polylines to polygons but I cannot but the reserve operation. Some documentation suggested that st_segmentize would work but I haven't found it useful.

Best Answer

To convert a simple polygon P (that defines an area) to a line L use:

L = st_cast(P,"LINESTRING")

eg using pl from example(st_polygon):

> st_polygon(pl)
POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
> st_cast(st_polygon(pl),"LINESTRING")
LINESTRING (0 0, 1 0, 1 1, 0 1, 0 0)