How to create MultiPolygons in Shapely?

In this tutorial we will discuss how to create MultiPolygons in Shapely. Let start with the definition of a MultiPolygon. What is it, and what makes it different from a regular Shapely Polygon?


Creating MultiPolygons in Shapely

Here is a quick recap on how we create regular Polygons in Shapely and extract its coordinates.

from shapely.geometry import Polygon

poly = Polygon([(20, 20), (200, 20), (200, 180), (20, 180)])

x, y = poly.exterior.xy
print(x, y)

The exterior attribute can be used to retrieve the x and y values from the polygon, which we can use later in some other operation (such as plotting the Polygon).

Now lets explore how to create a MultiPolygon in Shapely. There are several ways of doing this, and we will be covering all of them as we go through this tutorial.

Here is the first method.

poly_1 = [(20, 20), (60, 20), (60, 40), (20, 40)]
poly_1_interior = []
poly_2 = [(60, 50), (60, 70), (80, 70), (80, 50)]
poly_2_interior = []
polygons =  MultiPolygon([[poly_1, poly_1_interior], [poly_2, poly_1_interior]])

The MultiPolygon Class takes a list, which consists of smaller lists containing two lists each (Its basically a 3D list). Each one of the lists in the MultiPolygon Class (the ones with the two lists inside them) represent the coordinates for a single Polygon.

The first list amongst the two (e.g: poly_1) represents the coordinates for the exterior of a Polygon. The second list (e.g: poly_1_interior) represents the coordinates for the interior (holes) if there are any. We do not, so we will leave this as an empty array.


Alternatively, you can just pass a list of Polygons to the MultiPolygon class, and it will automatically combine and extract all the coordinates from it (so that we don’t have to). This is a better technique to use, especially when we already have the regular Polygons created.

from shapely.geometry import MultiPolygon, Polygon

poly_1 = Polygon([(20, 20), (60, 20), (60, 40), (20, 40)])
poly_2 = Polygon([(60, 50), (60, 70), (80, 70), (80, 50)])
polygons =  MultiPolygon([poly_1, poly_2])

One handy trick you need to know, is how to access the individual Polygons inside the MultiPolygon. This can be done by iterating over the MultiPolygons “geoms” attribute.

for poly in polygons.geoms:
    print(poly)
POLYGON ((20 20, 60 20, 60 40, 20 40, 20 20))
POLYGON ((60 50, 60 70, 80 70, 80 50, 60 50))

(You can iterate over the MultiPolygon directly too, but that’s deprecated. Don’t do it)

To learn how to visualize them (using Matplotlib graphs), follow the link.


MultiPolygons with Union

There is a third scenario in which MultiPolygons can be created (sometimes even without us realizing it). Whenever we perform Union between Polygons which do not intersect/overlap with each other, a MultiPolygon is returned to us.

Here is a small example.

from shapely.geometry import Polygon
from shapely.ops import unary_union

poly_1 = Polygon([(20,20), (60,20), (60,40), (20,40)])
poly_2 = Polygon([(60,50), (60,70), (80,70), (80,50)])

polygons = unary_union([poly_1, poly_2])
print("Type: ", polygons.type)
Type: MultiPolygon

Using the type attribute is an easy way quickly identifying whether we are dealing a Polygon or a MultiPolygon.


This marks the end of the How to create MultiPolygons in Shapely? Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments