sieve

Contents

sieve#

sieve(array: ~numpy.ndarray | ~numpy.ma.core.MaskedArray, out_meta: dict, sieve_thresh: int, connectivity: int = 4) -> (typing.Union[numpy.ndarray, numpy.ma.core.MaskedArray], <class 'dict'>)[source]#

Sieving, overloads rasterio function with raster shaped like (1, h, w).

Forces the output to np.uint8 (as only classified rasters should be sieved)

Parameters:
  • array (AnyNumpyArray) – Array to sieve

  • out_meta (dict) – Metadata to update

  • sieve_thresh (int) – Sieving threshold in pixels

  • connectivity (int) – Connectivity, either 4 or 8

Returns:

Sieved array and updated meta

Return type:

(AnyNumpyArray, dict)

Example

>>> raster_path = "path/to/raster.tif"  # classified raster
>>>
>>> # Read raster
>>> raster, meta = read(raster_path)
>>>
>>> # Rewrite it
>>> sieved, sieved_meta = sieve(raster, meta, sieve_thresh=20)
>>>
>>> # Write it
>>> raster_out = "path/to/raster_sieved.tif"
>>> write(sieved, raster_out, sieved_meta)