# Definition of multipolygon distance in Shapely

It is not clear to me from the Shapely documentation what is the precise definition of distance between two Multipolygons. I believe I read somewhere that the distance between two Polygons A and B is defined as the minimum distance from the boundary of A to the boundary of B.

If that is correct, does that imply that Shapely's distance between Multipolygons C and D is the minimum of the distances for all possible pairings of Polygons A and B where A is an element C and B is an element of D?

You understand correctly.

Here is a small script that shows it as well. If there are specific situations where you are in doubt, you can change the script to verify.

import matplotlib.pyplot as plt
import shapely
import shapely.plotting

multi1 = shapely.MultiPolygon(
[
shapely.Polygon(shell=[(0, 0), (0, 5), (5, 5), (5, 0), (0, 0)]),
shapely.Polygon(shell=[(10, 0), (15, 0), (15, 5), (10, 5), (10, 0)]),
]
)

multi2 = shapely.MultiPolygon(
[
shapely.Polygon(shell=[(0, 10), (5, 10), (5, 15), (0, 15), (0, 10)]),
shapely.Polygon(shell=[(0, 20), (5, 20), (5, 25), (0, 25), (0, 20)]),
]
)

shapely.plotting.plot_polygon(multi1, color="red")
shapely.plotting.plot_polygon(multi2, color="blue")
plt.show()
print(f"distance: {multi2.distance(multi1)}")

### Result of print:

distance: 5.0

I'm not sure I understand your question exactly, but it's fairly simple. The distance between two MultiPolygons is equal to the distance between the closest points on the boundaries of the two closest component polygons in each MultiPolygon,

For example, if you have three MultiPolygons A, B and C each containing a number of component polygons, then:

• A.distance(B) will be equal to the distance (D) between component polygons A1 -> B1
• A.distance(C) will be equal to the distance (D) between component polygons A2 -> C1