zonal_stats

museotoolbox.stats.zonal_stats(in_image, in_vector, unique_id, stats=['mean', 'median', 'std'], verbose=False)[source]

Extract zonal stats according to an predifined id.

Parameters
  • in_image (str.) – Path of the raster file where the vector file will be rasterize.

  • in_vector (str.) – Path of the vector file to rasterize.

  • unique_id (str or False.) – If False, MuseoToolBox will create a field called ‘uniquefid’ using thefunction _add_vector_unique_fid().

  • stats (list, optional (default=['mean','median','std'])) – str in list must be a function available from numpy. For example [‘var’] will output the variance per band and per unique id.

  • verbose (bool or int, optional (default=True)) – The higher is the int verbose, the more it will returns informations.

Returns

stats – Returns as many np.ndarray as number of stats asked. Stats ordered by bands.

For example (each line correspond to the unique_id ordered asc) :

mean band_1

mean band_2

mean band_3

mean band_1

mean band_2

mean band_3

Return type

np.ndarray

Examples

>>> raster,vector = mtb.datasets.load_historical_data()
>>> mean,var = mtb.stats.zonal_stats(raster,vector,'uniquefid',stats=['mean','var'])
>>> mean.shape
(17, 3)
>>> mean[0,:] # mean of the first unique_id
array([117.75219446, 109.80958812,  79.64213369])
>>> var[0,:]
array([1302.29983482, 1250.59980003, 1015.76659747])

Examples using museotoolbox.stats.zonal_stats