In order to get the exterior coordinates I need to convert a shapely MultiPolygon to a Polygon. I do it like this:

if poly.geometry.type == 'Polygon':
    x, y = poly.geometry.exterior.xy
elif poly.geometry.type == 'MultiPolygon':
    allparts = [p.buffer(0) for p in poly.geometry]
    poly.geometry = shapely.ops.cascaded_union(allparts)
    x, y = poly.geometry.exterior.xy  # here happens the error

This succeeds very often, but there are also cases where the Polygon obviously stays a MultiPolygon as the following error is still raised:

AttributeError: 'MultiPolygon' object has no attribute 'exterior'

I've checked, however, that every part of the MultiPolygon is a polygon and not itself a MultiPolygon:

>>>>[p.type for p in poly.geometry]
['Polygon', 'Polygon']

Any ideas why this happens and how to fix it?

Can it be the holes in the polygon? I looks like this: enter image description here

  • A MultiPolygon is a simple list of Polygons, therefore a lists has not "exterior", but every Polygon in the list has "exterior". If you want the Polygons use a for loop ([p.exterior.xy for p in Multi)] – gene Feb 10 '16 at 9:34
  • Okay, I see, but what can I do then in order to get the exterior coordinates of the MultiPolygon? In my approach I am trying a union of the single polygons, but this seems to be not working here.... – countryman Feb 10 '16 at 9:49
  • Is it possible that the API of shapely changed in the mean time? I don't see the attribute geometry in a Polygon, but geom_type ? – K.-Michael Aye Jan 11 '18 at 8:29

You need to understand the Shapely binary predicates:

1) If the two polygons intersects the result of union or unary_union (in red) is a Polygon therefore you can computes the exterior

enter image description hereenter image description here

2) If the two polygons are disconnected, the result is necessary a MultiPolygon (in red with two polygons)

enter image description hereenter image description here

And if you work with Shapefiles, without topology, this may occur

A solution is to compute the Concave Hull but it is not really an union.

enter image description here

| improve this answer | |
  • Okay, indeed, I found out that this is the case - and logic! Thanks a lot! – countryman Feb 12 '16 at 14:58
  • 1
    how do you compute concave hull? – Dima Lituiev May 10 '18 at 19:48

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.