sieve#

sieve(array: typing.Union[numpy.ma.core.MaskedArray, numpy.ndarray], out_meta: dict, sieve_thresh: int, connectivity: int = 4) -> (typing.Union[numpy.ma.core.MaskedArray, numpy.ndarray], <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)

>>> 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)
Parameters
  • array (Union[np.ma.masked_array, np.ndarray]) – 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

(Union[np.ma.masked_array, np.ndarray], dict)