Smoothing Shapely geometries using CatMull-Rom spline
Project description
CatSmoothing: Smoothing Shapely Geometries with Catmull-Rom Splines
Overview
CatSmoothing smooths Shapely
geometries, LineString
and (Multi)Polygon
, using the Catmull-Rom spline algorithm.
The implementation is based on the
Splines
library, but offers performance improvements and additional features.
You can try CatSmoothing directly in your browser by clicking the Binder badge above.
Key Features
- Creating splines from 2D/3D vertices of a line that allows computing n-th derivatives
- Smoothing geometries with Centripetal Catmull-Rom splines with uniform spacing (spacing is determined iteratively based on the arc length of the input geometry)
- Computing tangent vectors at each vertex of a line
- Optional Gaussian filtering to reduce noise in input geometries before smoothing
(requires
scipy
to be installed) - Automatically using
opt-einsum
for efficient computation of the spline coefficients, if it is installed
Installation
Install CatSmoothing via pip
or micromamba
:
Using pip
:
pip install catsmoothing
Using micromamba
(or conda
/mamba
):
micromamba install -c conda-forge catsmoothing
Quick Start
CatSmoothing provide one class called CatmullRom
that is general purpose,
Catmull-Rom spline interpolation class. You can tweak the alpha
parameter of
the class to interpolate with different versions of the Catmull-Rom spline
from 2D/3D vertices of a line and compute n-th derivatives.
For smoothing geometries, CatSmoothing uses the centripetal Catmull-Rom spline
algorithm, i.e., alpha=0.5
. There are two functions that can be used
for smoothing geometries: smooth_line
and smooth_polygon
. There is also
a function for computing tangent angles (in radians) at each vertex of a line.
Basic Usage
For fitting a Catmull-Rom spline to a line, we can use the following code:
from catsmoothing import CatmullRom
verts = [(0, 0), (0, 0.5), (1.5, 1.5), (1.6, 1.5), (3, 0.2), (3, 0)]
n_pts = 15
for alpha in (0, 0.5, 1):
s = CatmullRom(verts, alpha=alpha, bc_types="closed")
dots = int((s.grid[-1] - s.grid[0]) * n_pts) + 1
distances = s.grid[0] + np.arange(dots) / n_pts
smoothed[alpha] = s.evaluate(distances)
For smoothing a geometry, we can use the following code:
from shapely import Polygon
import catsmoothing as cs
poly = Polygon(verts)
ploy_smoothed = cs.smooth_polygon(poly, n_pts=50)
For smoothing a noisy line, we can use the following code:
import numpy as np
from shapely import LineString
import catsmoothing as cs
rng = np.random.default_rng(123)
x = np.linspace(-3, 2.5, 50)
y = np.exp(-(x**2)) + 0.1 * rng.standard_normal(50)
line = LineString(np.c_[x, y])
line_smoothed = cs.smooth_linestring(line, n_pts=30, gaussian_sigma=2)
We can then compute the tangent angles in radians at each vertex of the smoothed line:
vertices = shapely.get_coordinates(line_smoothed)
tangents = cs.compute_tangents(vertices)
For more examples, visit the documentation.
Contributing
We welcome contributions! For guidelines, please refer to the CONTRIBUTING.md and CODE_OF_CONDUCT.md.
License
CatSmoothing is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file catsmoothing-0.1.1.tar.gz
.
File metadata
- Download URL: catsmoothing-0.1.1.tar.gz
- Upload date:
- Size: 371.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d629775e5cca037ea75b72360da3a5de8be40f1842b969a255d786053a2485f |
|
MD5 | 9064ccbedda3ad76731ff689a721beb8 |
|
BLAKE2b-256 | 3fa3924851100185ae15102d85b3ead5d429b3b89299750827750e57002ea8f3 |
File details
Details for the file catsmoothing-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: catsmoothing-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7343615654e6a4aa9edf68abe1930a480b00d853438af1b09bbd497351d2661a |
|
MD5 | 8aec5dac459ffa101cda334dbb573414 |
|
BLAKE2b-256 | c29eee4aa378c429ae9c5e824bd7de74a06a93950821fa49e95678a4d7cf801e |