Spatial predicates

Asking whether two shapes intersect, touch, overlap or contain each other — and the DE-9IM matrix that answers all of these at once.

What you'll learn
  • Choose the right predicate among intersects, touches, overlaps, contains and within
  • Read a DE-9IM string from relate()
  • Compute intersection and union areas

The notebook stops at single objects. The natural next step, and the reason most people reach for Shapely, is asking how two geometries relate: does the planned path cross the obstacle?, is the robot inside its allowed zone? This lesson goes beyond the notebook; every value below was checked with Shapely 2.2.

Take a fixed rectangle A = box(0, 0, 6, 4) and move a second box B around it.

Six positions of B against A, with each predicate's value. The violet mark is the intersection: empty, a line, a point, or an area. The readout gives the DE-9IM string and the areas from intersection() and union().

The predicates

Predicate True when
A.intersects(B) they share at least one point (boundary counts)
A.disjoint(B) they share nothing — the negation of intersects
A.touches(B) they meet only on their boundaries; interiors stay apart
A.overlaps(B) interiors overlap, but neither contains the other
A.contains(B) B lies inside A, with B’s interior inside A’s interior
A.within(B) A lies inside B — the mirror of contains

The edge-sharing and corner-sharing cases show why touches exists: intersects is True for both, though no area is shared, and A.intersection(B) returns a LineString and a Point respectively. For collision checking, that is often the distinction that matters — two parts touching is not two parts colliding.

One matrix behind them all

Every predicate is a summary of the DE-9IM matrix, which records the dimension of the intersection between the interior, boundary and exterior of A and those of B: 2 for an area, 1 for a line, 0 for a point, F for nothing. Shapely returns it as a nine-character string, row by row:

A.relate(box(6, 1, 8, 3))   # 'FF2F11212'

Read the first three characters as “A’s interior meets B’s interior, boundary, exterior”: F, F, 2. The interiors never meet, so the boxes do not overlap. The fifth character, A’s boundary against B’s boundary, is 1: they share a line. That pattern — interiors apart, boundaries meeting — is exactly the definition of touches. Any predicate not built in can be written as a pattern and tested with A.relate_pattern(B, pattern).

Areas

Overlay operations return new geometries:

B = box(4, 1, 8, 3)
A.intersection(B).area   # 4.0
A.union(B).area          # 28.0 = 24 + 8 - 4

The union obeys inclusion–exclusion: the area of A plus the area of B, minus the part counted twice.

Try it yourself. A point exactly on A’s boundary, Point(6, 2): predict A.contains(p), A.covers(p), A.touches(p) and A.intersects(p), then check. The difference between contains and covers is exactly this boundary case. Finally, buffer the point with Point(6, 2).buffer(1) and predict which predicates change.

View source on GitHub →