Collections, convex hulls and validity

MultiPoint, MultiLineString and MultiPolygon; computing a convex hull; and why the notebook's MultiPolygon is invalid.

What you'll learn
  • Group geometries into Multi* collections and iterate over their parts
  • Explain how a convex hull is computed
  • Recognise an invalid geometry and repair it

When one feature has several parts — an archipelago, a road network, a building with two wings — Shapely groups them in a collection:

multi_point = MultiPoint([point1, point2, point3])
multi_line = MultiLineString([LineString([point1, point2]),
                              LineString([point2, point3])])
len(multi_line.geoms)    # 2

The parts are in .geoms; area, length and bounds are computed over all of them.

Convex hull

convex = multi_point.convex_hull
# POLYGON ((7.2 -25.1, 2.2 4.2, 9.26 -2.456, 7.2 -25.1))

The convex hull is the smallest convex polygon containing every point — a rubber band snapped around them. With three points it is simply the triangle. With many points it is computed by an algorithm like Andrew’s monotone chain: sort the points by \(x\), then sweep left to right building the lower boundary, and right to left building the upper one. Whenever the last three points turn clockwise, the middle one cannot be on the hull and is removed. The test is a 2D cross product:

\[(\mathbf{a} - \mathbf{o}) \times (\mathbf{b} - \mathbf{o}) = (a_x - o_x)(b_y - o_y) - (a_y - o_y)(b_x - o_x)\]

positive for an anticlockwise turn.

Monotone chain on random points: the blue lower chain, then the violet upper chain. An orange ring marks a point being popped after a clockwise turn. The finished hulls match Shapely's convex_hull for the point counts checked (6, 14 and 30).

Each point is pushed once and popped at most once, so after the \(O(n \log n)\) sort the sweep is linear.

An invalid MultiPolygon

The notebook builds a western hemisphere with a hole, and an eastern hemisphere as a box, and joins them:

west_poly = Polygon(shell=[(-180, 90), (-180, -90), (0, -90), (0, 90)],
                    holes=[[(-170, 80), (-170, -80), (-10, -80), (-10, 80)]])
east_poly_box = box(minx=0, miny=-90, maxx=180, maxy=90)
multi_poly = MultiPolygon([west_poly, east_poly_box])

multi_poly.area       # 39200.0
multi_poly.is_valid   # False

The last cell of the notebook checks validity and gets False, without comment. The reason, from shapely.validation.explain_validity, is Self-intersection[0 90]. The two parts share the whole meridian \(x = 0\), from \((0, -90)\) to \((0, 90)\). A valid MultiPolygon may have parts that touch at isolated points, but never along a line: a shared edge means the two pieces are really one polygon.

The area happens to be right — 6,800 for the west plus 32,400 for the east — but other operations on invalid geometries can fail or return nonsense. The repair is to merge the parts:

fixed = west_poly.union(east_poly_box)   # a single valid Polygon, area 39200.0

shapely.make_valid(multi_poly) repairs it too, but here returns a GeometryCollection rather than a polygon, so union is the cleaner fix.

Try it yourself. Shift the eastern box to box(1, -90, 180, 90) so the parts no longer meet, and check is_valid. Then try box(0, 0, 180, 90), which meets the western part only along half the meridian. Predict each result before running it, and print explain_validity for the invalid ones.

View source on GitHub →