revrt.spatial_characterization.zonal.ZonalStats#

class ZonalStats(stats=None, nodata=None, all_touched=True, category_map=None, add_stats=None, zone_func=None)[source]#

Bases: object

Class to compute zonal statistics

Parameters:
  • stats (str | iterable of str, optional) – Names of all statistics to compute. Statistics must be one of the members of Stat or FractionalStat, or must start with the percentile_ prefix and end with an int or float representing the percentile to compute (e.g. percentile_10.5). If only one statistic is to be computed, you can provide it directly as a string. Otherwise, provide a list of statistic names or a string with the names separated by a space. You can also provide the string "ALL" or "*" to specify that all statistics should be computed (i.e. all options from both Stat and FractionalStat). If no input, empty input, or None is provided, then only the base stats (“count”, “min”, “max”, “mean”) are configured. To summarize, all of the following are valid inputs:

    • stats="*" or stats="ALL" or stats="All"

    • stats="min"

    • stats="min max"

    • stats=["min"]

    • stats=["min", "max", "percentile_10.5"]

    By default, None.

  • nodata (int | float, optional) – Value in the raster that represents nodata. This value will not show up in any statistics except for the nodata statistic itself, which computes the number of nodata values within the zone. Note that this value is used in addition to any NODATA value in the raster’s metadata. By default, None.

  • all_touched (bool, optional) – Flag indicating whether to include every raster cell touched by a geometry (True), or only those having a center point within the polygon (False). By default, True.

  • category_map (dict, optional) – Dictionary mapping raster values to new names. If given, this mapping will be applied to the pixel count dictionary, so you can use it to map raster values to human-readable category names. By default, None.

  • add_stats (dict, optional) – Dictionary mapping extra stat names to callable functions that can be used to compute that stat. The functions must take exactly three arguments as input:

    • processed_raster: Array representing the

      zone-masked raster.

    • feat: Pandas Series object containing information

      about the zone.

    • rasterized_zone: Unit8 array of the same shape

      as processed_raster representing the rasterized zone.

    By default, None.

  • zone_func (callable(), optional) – Callable function to apply to the zone-masked raster array prior to computing stats. Must take exactly one input: the zone-masked raster array. By default, None

Methods

from_array(zones, raster_array, affine_transform)

Compute zonal statistics for data in memory

from_files(zones_fp, raster_fp[, prefix, ...])

Compute zonal statistics for data stored in files

Attributes

category_map

Map of values to category names

computable_stats

ComputableStats

property computable_stats[source]#

ComputableStats

property category_map[source]#

Map of values to category names

Type:

dict

from_files(zones_fp, raster_fp, prefix=None, copy_properties=None, parallel=False)[source]#

Compute zonal statistics for data stored in files

Parameters:
  • zones_fp (path-like) – Path to GeoPackage defining zone polygons.

  • raster_fp (path-like) – Path to GeoTiff representing the data that should be used for statistics computations.

  • prefix (str, optional) – A string representing a prefix to add to each stat name. If you wish to have the prefix separated by a delimiter, you must include it in this string (e.g. prefix="test_"). By default, None.

  • copy_properties (iterable of str, optional) – Iterable of columns names to copy over from the zone feature. By default, None.

  • parallel (bool, optional) – Option to perform processing in parallel using dask. By default, False.

Returns:

list – List of dictionaries, each of which containing computed statistics for a zone.

from_array(zones, raster_array, affine_transform, prefix=None, copy_properties=None, parallel=False)[source]#

Compute zonal statistics for data in memory

Parameters:
  • zones (geopandas.GeoDataFrame) – GeoDataFrame containing the zone polygons.

  • raster_array (xarray.DataArray) – Xarray DataArray representing the data that should be used to compute statistics.

  • affine_transform (affine.Affine) – Affine transform object representing the raster transformation. This is used to compute the raster shapes for statistics computations as well as the pixel area for each pixel value in the raster.

  • prefix (str, optional) – A string representing a prefix to add to each stat name. If you wish to have the prefix separated by a delimiter, you must include it in this string (e.g. prefix="test_"). By default, None.

  • copy_properties (iterable of str, optional) – Iterable of columns names to copy over from the zone feature. By default, None.

  • parallel (bool, optional) – Option to perform processing in parallel using dask. By default, False.

Yields:

dict – Dictionary of statistics computed for a single zone.