Skip to content

smooth

Utilities for smoothing geometries.

Fork + Port of https://github.com/philipschall/shapelysmooth (Public domain)

InputeTypeError

Bases: TypeError

Raised when the input geometry is of the incorrect type.

Source code in src/rastr/gis/smooth.py
20
21
class InputeTypeError(TypeError):
    """Raised when the input geometry is of the incorrect type."""

catmull_rom_smooth(geometry, alpha=0.5, subdivs=10)

Polyline smoothing using Catmull-Rom splines.

Parameters:

Name Type Description Default
geometry BaseGeometry

The geometry to smooth

required
alpha float

The tension parameter, between 0 and 1 inclusive. Defaults to 0.5. - For uniform Catmull-Rom splines, alpha = 0. - For centripetal Catmull-Rom splines, alpha = 0.5. - For chordal Catmull-Rom splines, alpha = 1.0.

0.5
subdivs int

Number of subdivisions of each polyline segment. Default value: 10.

10

Returns: The smoothed geometry.

Source code in src/rastr/gis/smooth.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def catmull_rom_smooth(
    geometry: BaseGeometry, alpha: float = 0.5, subdivs: int = 10
) -> LineString | Polygon:
    """Polyline smoothing using Catmull-Rom splines.

    Args:
        geometry: The geometry to smooth
        alpha: The tension parameter, between 0 and 1 inclusive. Defaults to 0.5.
               - For uniform Catmull-Rom splines, alpha = 0.
               - For centripetal Catmull-Rom splines, alpha = 0.5.
               - For chordal Catmull-Rom splines, alpha = 1.0.
        subdivs:
            Number of subdivisions of each polyline segment. Default value: 10.

    Returns: The smoothed geometry.
    """
    if not isinstance(geometry, (LineString, Polygon)):
        msg = "Only LineString and Polygon geometries are supported."
        raise NotImplementedError(msg)
    coords, interior_coords = _get_coords(geometry)
    coords_smoothed = _catmull_rom(coords, alpha=alpha, subdivs=subdivs)
    if isinstance(geometry, LineString):
        return geometry.__class__(coords_smoothed)
    elif isinstance(geometry, Polygon):
        interior_coords_smoothed = [
            _catmull_rom(c, alpha=alpha, subdivs=subdivs) for c in interior_coords
        ]
        return geometry.__class__(coords_smoothed, holes=interior_coords_smoothed)
    else:
        assert_never(geometry)