Shapely and GeoPandas – How to Extract Biggest Polygon from MultiPolygon?

geopandasshapely

Given a Shapely MultiPolygon, how can I extract the biggest Polygon?

For example, given a MultiPolygon as follows:

from shapely.geometry import Polygon, MultiPolygon

multipolygon = MultiPolygon([Polygon([(0,0), (1,0), (1,0.25), (1, 0.5), (1,0.75), (1,1), (0,1)]),
                             Polygon([(2,0), (1.2,0.25), (1.2,0.25), (1.2,0.75), (2,0.75)])])

enter image description here

How can I get just the largest part?

enter image description here

Best Answer

Here's what I would do:

max(multipolygon, key=lambda a: a.area)

The built-in max function used this way will return the item from the list where lambda a: a.area is maximized.