2

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

4
  • Which format is your polygon in to begin with?
    – cmrRose
    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.
    – Spacedman
    Commented Jul 26, 2018 at 16:18
  • 1
    For example, if you are reading using fiona and shapely the linestring for a polygon is the boundary attribute.
    – Spacedman
    Commented Jul 26, 2018 at 16:35

1 Answer 1

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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