# Transforming a polygon to a linestring

Is it possible to (trivially, via a tool in python) to convert a polygon to a linestring? if so, how? If not, why?

• Which format is your polygon in to begin with? Commented Jul 26, 2018 at 14:45
• WSG84 read from a shapefile Commented Jul 26, 2018 at 14:51
• How are you reading it from a shapefile? Try editing your question and showing some code. Commented Jul 26, 2018 at 16:18
• For example, if you are reading using `fiona` and `shapely` the linestring for a polygon is the `boundary` attribute. Commented Jul 26, 2018 at 16:35

The simpliest way I think is to use boundary method in Shapely lib. When using

``````from shapely.geometry import Polygon, MultiPolygon
Polygon(feature['geometry']['coordinates'][0]).boundary
``````

it returns

``````LINESTRING (30.916671 55.61667, 31.166671 56.91667, 32.550004 57.350003,...)
``````

In case Multipolygon it obviously returns Multilinestring:

``````multy = MultiPolygon([Point(0, 0).buffer(1.0), Point(1, 1).buffer(1.0)])
multyline = multy.boundary
print(multy)
print(multyline)

MULTIPOLYGON (((1 0, 0.9951847266721969 -0.09801714032956051, ...))((...)))
MULTILINESTRING ((1 0, 0.9951847266721969 -0.09801714032956051, ...)(...))
``````

QGIS way:

Vector -> Geometry tools -> Polygons to lines