Is it possible to (trivially, via a tool in python) to convert a polygon to a linestring? if so, how? If not, why?
1 Answer
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
fiona
andshapely
the linestring for a polygon is theboundary
attribute.