Given a shapefile with polygons, how do I convert the polygons to individual lines instead? I know how to do this in QGIS, but I'm looking for a Shapely function which does the same thing.

closed as off-topic by ahmadhanb, BERA, tinlyx, whyzar, aldo_tapia Mar 27 '18 at 11:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – ahmadhanb, BERA, tinlyx, whyzar, aldo_tapia
If this question can be reworded to fit the rules in the help center, please edit the question.


bugmenot123 is ok but I find easier use the boundary of the polygons. If you have a multipolygon or a polygon with holes the boundary returns a multilinestrig, if you have a polygon without holes the boundary returns a linestring.

Here is a simple example so you can see how it works:

import shapely
from shapely.geometry import MultiPolygon, Point

pol1 = MultiPolygon([Point(0, 0).buffer(2.0), Point(1, 1).buffer(2.0)])
pol2 = Point(7, 8).buffer(1.0)
pols = [pol1, pol2]

lines = []
for pol in pols:
    boundary = pol.boundary
    if boundary.type == 'MultiLineString':
        for line in boundary:

for line in lines:
    print line.wkt

According to https://shapely.readthedocs.io/en/stable/manual.html#polygons:

  • You can get the outer ring of a Polygon via its exterior property.
  • You can get a list of the inner rings via its interior property.

According to https://shapely.readthedocs.io/en/stable/manual.html#collections-of-polygons:

  • For a MultiPolygon you can iterate over its Polygon members via iterating via in or list() or explicitely using its geoms property.

Collect all the rings and you have the lines.

  • Tbh, I actually output the individual lines by getting the coordinates defining the polygons' vertices then creating LineStrings from consecutive coordinates. But your solution works as well. – absolutelydevastated Mar 27 '18 at 9:01
  • 3
    Please post your own solution then. – bugmenot123 Mar 27 '18 at 11:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.