Multipolygon supposed to be contained by another but returned False with Shapely

In your case, a.touches(A) returns False because documentation says:

touches returns True if the objects have at least one point in common and their interiors do not intersect with any part of the other.

enter image description here

Also A.contains(a) returns False because a lot of points of a lie in the extrior of A. Documentation says:

contains returns True if no points of other lie in the exterior of the object and at least one point of the interior of other lies in the interior of object.

a.difference(A) returns empty geometry because all interior of a is in A. But A.difference(a) gives:

enter image description here

The solution is a.symmetric_difference(A)

Reread The Shapely User Manual

Returns a representation of the points in this object not in the other geometric object, and the points in the other not in this geometric object.

and not

Returns a representation of the points making up this geometric object that do not make up the other object.

nor object.contains(other) or object.touches(other) as Kadir Şahbaz says


enter image description here


enter image description here


enter image description here

The inconsistency between a.difference(A) = POLYGON EMPTY and A.contains(a) = FALSE is due to numerical precision issues. The polygon a contains many vertices which are probably intended to lie exactly on the line segments of A. But due to the limitations of finite precision floating-point, a point almost never lies exactly on a line segment.

As visible in the image below, some of these vertices lie outside the polygon A.

Vertices not lying exactly on line segments

The contains predicate computation (in GEOS) is evaluated with high precision, and so detects that there are vertices of a which lie outside A. The reason that difference is not affected is that it contains logic to improve robustness which ends up snapping these vertices to the line.

It's hoped that a future version of JTS/GEOS will provide spatial predicates that can use a tolerance value to handle situations like this.