[GIS] Shapely’s simplify won’t simplify any more

geospythonshapelysimplify

I have a multipolygon in shapely, and I want to simplify it until it has less than a certain number of points. When I use the preseve_topology=False argument to geom.simplify, I can get the number of points arbitrarily low.

However if I leave that out (i.e. preserve_topology=True), there appears to be a hard limit of how much it will simplify the object. As I increase the parameter (and hence make it "more simple"), the number of points stays stuck.

gg is a MultiPolygon with 255726 points in Google Mercator projection (EPSG:3857).

With preserve_topology=False:

(Pdb) pp [(10**i, i, num_points_in_polygon(gg.simplify(10**i, preserve_topology=False))) for i in range(1, 20)]
[(10, 1, 134598),
 (100, 2, 27531),
 (1000, 3, 2523),
 (10000, 4, 156),
 (100000, 5, 13),
 (1000000, 6, 0),
 (10000000, 7, 0),
 (100000000, 8, 0),
 (1000000000, 9, 0),
 (10000000000, 10, 0),
 (100000000000, 11, 0),
 (1000000000000, 12, 0),
 (10000000000000, 13, 0),
 (100000000000000, 14, 0),
 (1000000000000000, 15, 0),
 (10000000000000000, 16, 0),
 (100000000000000000, 17, 0),
 (1000000000000000000, 18, 0),
 (10000000000000000000L, 19, 0)]

With default preserve_topology=True:

 (Pdb) pp [(10**i, i, num_points_in_polygon(gg.simplify(10**i))) for i in range(1, 20)]
[(10, 1, 135174),
 (100, 2, 32572),
 (1000, 3, 10947),
 (10000, 4, 8760),
 (100000, 5, 8519),
 (1000000, 6, 8509),
 (10000000, 7, 8509),
 (100000000, 8, 8509),
 (1000000000, 9, 8509),
 (10000000000, 10, 8509),
 (100000000000, 11, 8509),
 (1000000000000, 12, 8509),
 (10000000000000, 13, 8509),
 (100000000000000, 14, 8509),
 (1000000000000000, 15, 8509),
 (10000000000000000, 16, 8509),
 (100000000000000000, 17, 8509),
 (1000000000000000000, 18, 8509),
 (10000000000000000000L, 19, 8509)]

As you can see, it stubbornly stays at 8509 points, even with a massive simplification param.

Is this sensible? Is there a bug in shapely/geos? Is there a way to simplify, and preserve topology and get arbitrarily simple objects?

Best Answer

Read "preserve topology" as "don't let polygon rings intersect other rings." At some point (~1/15th the number of original points in your case) the algorithm can't simplify any further without invalidating one or more parts of the multipolygon.

It's possible that some tricky parts of the multipolygon are holding the others up. You could try simplifying its polygons separately.

Related Question