3

I'm using shapely to calculate the boundary of individual LineString from a MultiLineString. The code was working fine until I encountered a case where the extracted LineString has the same start and end point. Though it should be a Polygon but upon printing the wkt version of the shapely object I found out that shapely is considering it as a LineString. Now the boundary method is failing here as I'm getting MULTIPOINT EMPTY as output. Is this a bug in shapely or I'm doing something wrong ?

Here's my code:

for lines in multilines:
    print lines.wkt
    boundary_points = lines.boundary

The output is:

LINESTRING (6.799200988255513 51.44578708679462, 6.79938 51.4457, 
6.80022 51.44523, 6.80187 51.44439, 6.80447 51.44315, 6.80474 51.44293, 6.80487 51.44278, 6.80494 51.44264, 6.80496 51.44246, 
6.80481 51.44219, 6.80467 51.44205, 6.8045 51.44196, 6.80427 51.44188, 6.80408 51.44184, 6.80387 51.44182, 6.80363 51.44183, 
6.80327 51.44187, 6.80294 51.44195, 6.80256 51.4421, 6.80191 51.44245, 6.80136 51.44287, 6.80095 51.44323, 6.80017 51.44407, 
6.79988 51.44447, 6.79963 51.44488, 6.7994 51.44532, 6.79921 51.44575, 6.799200988255513 51.44578708679462)
5

A LineString with the same start and end point is not a Polygon but a LinearRing. You need to understand the difference. If you want a Polygon from the LinearRing

# test if your output is a LinearRing
line.is_ring
True
# thus you can make a Polygon with the LinearRing
poly = Polygon(yourline)

enter image description here

Now from the manual of Shapely

A ring’s boundary is empty.

But poly.boundary not why ? Because the poly.boundary = the LinearRing of the Polygon

ring = LinearRing([(0, 0), (1, 1), (1, 0)])
for pt in list(ring.coords):
    print Point(pt)
 POINT (0 0)
 POINT (1 1)
 POINT (1 0)
 POINT (0 0)
poly = Polygon(ring)
print ring.boundary
MULTIPOINT EMPTY
print poly.boundary # the result is a LineString and not Points
LINESTRING (0 0, 1 1, 1 0, 0 0)
for i in list(poly.exterior.coords):
   print Point(i)
POINT (0 0)
POINT (1 1)
POINT (1 0)
POINT (0 0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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