cartagen.smooth_snake

Contents

cartagen.smooth_snake#

smooth_snake(geometry, iterations=10, alpha=0.1, beta=0.1, gamma=1.0, kappa=0.1)#

Smooth a line or polygon using the snake method.

This algorithm proposed by Burghardt [1] treats the geometry as an elastic snake optimizing an energy functional. Internal energy terms (alpha and beta) control the tension and rigidity of the line to smooth out sharp noise, while an external spring force (kappa) anchors the vertices to their original positions to prevent geometric shrinkage and uncontrolled displacement (Burghardt’s shape-preservation term).

The smoothed position is obtained by solving the linear system

\[(A + (\gamma + \kappa)\,I)\,\mathbf{x}^* = \gamma\,\mathbf{x}_0 + \kappa\,\mathbf{x}_0\]

Because both the system matrix and the right-hand side are constant across iterations, the snake converges to the same fixed point regardless of the number of iterations. The iterations parameter therefore controls a pseudo-temporal relaxation: starting from x_0, the snake is advanced iterations steps of implicit Euler integration, which can be useful to observe intermediate states or to limit over-smoothing on short geometries. With iterations=1 the result is the direct closed-form solution.

Multi-geometries are handled recursively. For polygons, the smoothing is applied independently to the exterior ring and to each interior ring (hole).

Parameters:
  • geometry (LineString, MultiLineString, Polygon, MultiPolygon, LinearRing) – The geometry to smooth. If an open line is provided, the endpoints are preserved. If a closed ring or polygon is provided, the smoothing wraps around.

  • iterations (int, optional) – Number of implicit Euler steps for the snake evolution. With iterations=1 the direct closed-form solution is returned. Default is 10.

  • alpha (float, optional) – Tension parameter (continuity). Higher values penalise line stretching and pull vertices toward their neighbours. Default is 0.1.

  • beta (float, optional) – Rigidity parameter (curvature). Higher values penalise sharp turns and produce smoother, more rounded curves. Default is 0.1.

  • gamma (float, optional) – Step-size / weighting of the current position in the RHS. Acts as a regularisation weight on the current iterate rather than a physical viscosity coefficient. Higher values slow down the evolution per step. Default is 1.0.

  • kappa (float, optional) – Shape-preservation parameter (Burghardt’s external spring force). Higher values pull the snake back to its original shape, preventing shrinkage and structural collapse. Setting kappa=0 disables this term and closed loops will shrink toward their centroid over many iterations. Default is 0.1.

Returns:

LineString, Polygon, MultiLineString, MultiPolygon, LinearRing – Smoothed geometry of the same type as the input.

Notes

The stiffness matrix A is pentadiagonal: the tension band uses coefficients [-α, 2α, -α] and the rigidity band uses [β, -4β, 6β, -4β, β]. For closed rings the matrix is circulant (wrap-around indices); for open lines the rows are clamped at the boundaries and the first/last rows are replaced by identity rows to enforce exact endpoint preservation.

The evolution at each step solves:

M · x_new = γ · x_current + κ · x_original

where M = A + + κ) · I. This is equivalent to one step of implicit gradient descent on the snake energy.

See also

smooth_gaussian

Smooth a line or a polygon and attenuate its inflexion points.

smooth_platre

Smooth a line and preserve the integrity of sharp turns.

smooth_taubin

Smooth a line or polygon and prevent shrinkage.

smooth_topographic

Smooth a line or polygon and mimic hand-made cartographic generalization.

smooth_wma

Smooth a line or polygon using a low-pass filter.

Examples

>>> line = LineString([(0, 0), (1, 1), (2, 0), (3, 1), (4, 0)])
>>> smooth_snake(line)
<LINESTRING (0 0, 0.875 0.256, 2 0.298, 3.125 0.256, 4 0)>

(Source code)

../_images/smooth_snake.png