Geographic Information Systems Asked by absolutelydevastated on January 11, 2021
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.
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:
lines.append(line)
else:
lines.append(boundary)
for line in lines:
print line.wkt
Correct answer by César Argul García on January 11, 2021
According to https://shapely.readthedocs.io/en/stable/manual.html#polygons:
According to https://shapely.readthedocs.io/en/stable/manual.html#collections-of-polygons:
Collect all the rings and you have the lines.
Answered by bugmenot123 on January 11, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP