Polygon Simplification

Basic simplification:

import skgeom as sg
poly = sg.random_polygon(30, seed=3)

simplify tries to remove a given portion of the vertices without changing the topology:

sg.draw.draw(poly, facecolor="white", alpha=0.4)
res = sg.simplify(poly, 0.5)  # try to remove 50% of the vertices
sg.draw.draw(res, alpha=0.85)
sg.draw.draw(poly, facecolor="red")
sg.simplify(poly, 0.2)  # try to remove 80% of the vertices

simplify works on Polygon, PolygonWithHoles and PolygonSet. You can also apply simplify on lists of polygons.

poly_set = sg.PolygonSet([sg.random_polygon(10, seed=4), sg.random_polygon(40, seed=3)])
sg.draw.draw(poly_set, facecolor="red")
sg.simplify(poly_set, 0.5)

Simplification Modes

simplify currently supports three modes: * “ratio”: try to reduce number of vertices to a given ratio of the original count * “count”: try to reduce number of vertices to an absolute number * “sqeuclidean”: try to reduce number of vertices until the error (squared euclidean distance) becomes too big

sg.draw.draw(poly, facecolor="red")
sg.simplify(poly, 0.5, "ratio")  # try to remove 50% of vertices
sg.draw.draw(poly, facecolor="red")
sg.simplify(poly, 0.2, "sqeuclidean")  # try to remove while err (sq. distance) < 0.2

In general, all modes are targets that might not be met:

len(sg.simplify(poly, 0.5, "ratio").coords)
len(sg.simplify(poly, 0.25, "ratio").coords)
len(sg.simplify(poly, 5, "count").coords)

Simplification and Topology

By default, simplify tries to preserve topology.

from skgeom import boolean_set
poly = boolean_set.difference(
    sg.random_polygon(20, size=4, seed=4234),
    sg.random_polygon(20, size=1, seed=8421))[0]

By preserving topology, features like holes will not get lost:

poly2 = sg. simplify(poly, 0.2, "ratio")  # try to remove 50% of vertices

Turning off preserve_topology is much faster, but might destroy structural features:

poly2 = sg. simplify(poly, 0.2, "ratio", preserve_topology=False)

preserve_topology implies limits to how much simplifcation can happen:

poly2 = sg. simplify(poly, 1, "count")
poly2 = sg.simplify(poly, 1, "count", preserve_topology=False)

More Topology Examples

nested = [sg.random_polygon(15, shape="circle", size=r ** 3, seed=r * 5) for r in range(3, 10)]
def draw_nested(nested):
    for p in reversed(nested):

draw_nested(sg.simplify(nested, 0.3))

with defaults, forms have been simplified while preserving topology:

def n_vertices(nested):
    return sum(len(p) for p in nested)

(n_vertices(nested), n_vertices([p.outer_boundary() for p in sg.simplify(nested, 0.3)]))
(105, 59)

same call without preserving topology:

draw_nested(sg.simplify(nested, 0.3, preserve_topology=False))