# Simplifying a map layer using PostGIS topology

Posted on by strk

Category: Free Software GIS

### The data

French administrative subdivisions, called “départements”, will be used. Data can be downloaded here.

It is composed by **96** multipolygons for a total of **47036** vertices.

### Principle of simplification

- We convert a layer’s Geometries to TopoGeometries
- We simplify all edges of the built topology
- We convert the (now-simplified) TopoGeometries back to Geometries

### Steps

The following steps assume you loaded the shapefile into a table named “france_dept”.

### The SimplifyEdgeGeom function

You may have noticed that the “SimplifyEdgeGeom” is not a core function. It is a function I wrote for the purpose of catching topological problems introduced by simplification.

The naive call would be:

The problem with the above call is that any simplification introducing a topology error would be rejected by ST_ChangeEdgeGeom by throwing an exception and the exception would rollback the whole transaction leaving you with no edge changed. Possible topology errors introduced are: edges collapsing to points, intersecting self or other edges.

The SimplifyEdgeGeom function wraps the ST_ChangeEdgeGeom call into a subtransaction and handles exceptions by reducing the simplification factor until it succeeds. The version I used reduces simplification factor in half at each failure, dropping down to zero around 1e-8. You can roll your own with other heuristics or generalize this one to take parameters about stepping and limits.

Here’s the function:

### Performance

A single run of the simplification step brought vertices down to **1369** (from **47036**).

Timings are take on this system:

### Considerations

The procedure described in this post is also valid for *LINESTRING* and *MULTILINESTRING* layers, using the exactly same code.

You could reuse the topology to produce multiple resolution levels w/out incurring again in the construction cost (and with a reduced input complexity at each level).

The simplification step doesn’t use TopoGeometry objects at all so you could choose to perform topology construction and attribute assignment in a different way.

Running the *SimplifyEdgeGeom* function again might give you more simplification because edges which may have intersected to the simplified version of an edge may not be intersecting anymore after their own simplification. The function can be changed to behave differently on exception to improve performance or quality.