curviriver.smoothing#

Smoothing functions for LineString objects or a set of coordinates.

This module is taken from the pygeoutils package, which is available at HyRiver.

Module Contents#

curviriver.smoothing.interpolate_na(x, y, z, fill_value)[source]#

Interpolate NaN values in z using B-spline interpolation.

Notes

The input x, y, and z must be 1D arrays of the same size.

Parameters:
  • x (numpy.ndarray) – 1D arrays representing the X-coordinates of the points.

  • y (numpy.ndarray) – 1D arrays representing the Y-coordinates of the points.

  • z (numpy.ndarray) – 1D arrays representing the Z-coordinates of the points.

  • fill_value (float) – Value to use to fill NaNs at the beginning and end of the array.

Returns:

numpy.ndarray – The input array z with NaN values interpolated or filled.

Raises:

ValueError

Return type:

numpy.typing.NDArray[numpy.float64]

curviriver.smoothing.make_bspline(x, y, n_pts, k=3)[source]#

Create a B-spline curve from a set of points.

Parameters:
  • x (numpy.ndarray) – x-coordinates of the points.

  • y (numpy.ndarray) – y-coordinates of the points.

  • n_pts (int) – Number of points in the output spline curve.

  • k (int, optional) – Degree of the spline. Should be an odd number less than the number of points and greater than 1. Default is 3.

Returns:

Spline – A Spline object with x, y, phi, radius, distance, and line attributes. The line attribute returns the B-spline as a shapely.LineString.

Return type:

Spline

curviriver.smoothing.smooth_linestring(line, crs, n_pts, degree=3)[source]#

Smooth a line using B-spline interpolation.

Parameters:
  • line (shapely.LineString, shapely.MultiLineString) – Line to smooth. Note that if line is MultiLineString it will be merged into a single LineString. If the merge fails, an exception will be raised.

  • crs (int, str, or pyproj.CRS) – CRS of the input line. It must be a projected CRS.

  • n_pts (int) – Number of points in the output spline curve.

  • degree (int, optional) – Degree of the spline. Should be less than the number of points and greater than 1. Default is 3.

Returns:

Spline – A Spline object with x, y, phi, radius, distance, and line attributes. The line attribute returns the B-spline as a shapely.LineString.

Return type:

Spline

Examples

>>> import geopandas as gpd
>>> import shapely
>>> line = shapely.LineString(
...     [
...         (-97.06138, 32.837),
...         (-97.06133, 32.836),
...         (-97.06124, 32.834),
...         (-97.06127, 32.832),
...     ]
... )
>>> sp = smooth_linestring(line, 4326, 5)
>>> list(zip(*sp.line.xy))
[(-97.06138, 32.837),
(-97.06132, 32.83575),
(-97.06126, 32.83450),
(-97.06123, 32.83325),
(-97.06127, 32.83200)]