shapelysmooth
is a polyline smoothing package for Python - mainly intended for use on shapely LineStrings and Polygons.
- Requirements
- Installation
- Methods
- Smoothing Collections of Geometries
- Smoothing Geometries stored in Numpy Arrays
This package requires
- Python >=3.6
- shapely
Build from source, or
pip install shapelysmooth
Currently, three smoothing methods are provided, suited to different use cases.
>>> from shapelysmooth import taubin_smooth
>>> smoothed_geometry = taubin_smooth(geometry, factor, mu, steps)
Taubin polyline smoothing.
Parameters
geometry : LineString | Polygon | list
Geometric object consisting of polylines to smooth. A
shapely.geometry.Linestring
orshapely.geometry.Polygon
or alist
containing tuples of$(x, y)$ coordinates.
factor : float
How far each node is moved toward the average position of its neighbours during every second iteration.
$0 <$ factor
$< 1$ . Default value:$\mathbf{0.5}$ .
mu : float
How far each node is moved opposite the direction of the average position of its neighbours during every second iteration.
$0 <$ -mu
$< 1$ . Default value:$\mathbf{-0.5}$ .
steps : int
Number of smoothing steps. Default value:
$\mathbf{5}$ .
Returns
output : LineString | Polygon | List
The smoothed geometry.
The Laplacian smoothing algorithm defines for each interior node in a polyline consisting of nodes
See
The implementation also supports closed polylines and polygons (with or without holes).
>>> from shapelysmooth import chaikin_smooth
>>> smoothed_geometry = chaikin_smooth(geometry, iters, keep_ends)
Polyline smoothing based on Chaikin's Corner Cutting algorithm.
Parameters
geometry : LineString | Polygon | list
Geometric object consisting of polylines to smooth. A
shapely.geometry.Linestring
orshapely.geometry.Polygon
or alist
containing tuples of$(x, y)$ coordinates.
iters : int
Number of iterations. Default value:
$\mathbf{5}$ .
keep_ends : bool
Preserve the original start and end nodes of the polyline. Not applicable to closed polylines and polygons. (The original algorithm results in a contraction of the start and end nodes). Default value:
True
.
Returns
output : LineString | Polygon | List
The smoothed geometry.
Chaikin's Corner Cutting algorithm smooths a polyline by iterative refinement of the nodes of the polyline. At each iteration, every node is replaced by two new nodes: one a quarter of the way to the next node, and one quarter of the way to the previous node.
Note that the algorithm (roughly) doubles the amount of nodes at each iteration, therefore care should be taken when selecting the number of iterations.
Instead of the original iterative algorithm by Chaikin, this implementation makes use of the equivalent multi-step algorithm introduced by Wu et al. See
By default, the algorithm will not contract the endpoints of an open polyline. Set the keep_ends
parameter if this behaviour is unwanted.
The implementation also supports closed polylines and polygons (with or without holes).
>>> from shapelysmooth import catmull_rom_smooth
>>> smoothed_geometry = catmull_rom_smooth(geometry, alpha, subdivs)
Polyline smoothing using Catmull-Rom splines.
Parameters
geometry : LineString | Polygon | list
Geometric object consisting of polylines to smooth. A
shapely.geometry.Linestring
orshapely.geometry.Polygon
or alist
containing tuples of$(x, y)$ coordinates.
alpha : float
Tension parameter.
$0 \leq$ alpha
$\leq 1$ . For uniform Catmull-Rom splines,alpha
$= 0$ . For centripetal Catmull-Rom splines,alpha
$= 0.5$ . For chordal Catmull-Rom splines,alpha
$= 1.0$ . Default value:$\mathbf{0.5}$ .
subdivs : int
Number of subdivisions of each polyline segment. Default value:
$\mathbf{10}$ .
Returns
output : LineString | Polygon | List
The smoothed geometry.
Catmull-Rom splines are a class of cubic splines which can be efficiently calculated, and have the property that they pass through each interpolation node. The splines are piecewise-defined, but have
For more detail see
This implementation makes use of the algorithm proposed by Barry and Goldman, see
There are three types of Catmull-Rom splines: uniform, centripetal and chordal, which are determined by tension parameter,
Objects of type shapely.geometry.MultiLineString
and shapely.geometry.MultiPolygon
can be smoothed by making use of the geoms
property provided by these classes.
>>> from shapely.geometry import MultiPolygon
>>> from shapelysmooth import chaikin_smooth
>>> polys = MultiPolygon(...)
>>> smoothed_polys = MultiPolygon([chaikin_smooth(poly) for poly in polys.geoms])
Given a numpy.array
of shape
>>> import numpy as np
>>> from shapelysmooth import chaikin_smooth
>>> array = np.array(...)
>>> array_smoothed = np.array(chaikin_smooth(list(map(tuple, array))))
Direct support for Numpy arrays is being worked on for a future version.