gdal.raster_histogram

Bin the valid pixel values of a single-band raster into classes and tabulate pixel count and ground area per class. Bins are right-closed (lower, upper], matching R's cut(). Ground area is derived from the pixel size in the geotransform (count x |px_w x px_h|), so results are correct in any projected CRS. Nodata, NaN, and values outside [min_valid, max_valid] are excluded. Implemented directly against the GDAL/NumPy Python bindings rather than wrapping a single gdal_* command-line utility.

  • Kind: standard
  • Network: no

Inputs🔗

NameTypeDescription
sourcefile (GeoTIFF)Single-band raster to tabulate (e.g. a county canopy height model).
breaksstringComma-separated ascending bin edges, e.g. "0,5,10,15,20,25,30,35". N edges define N-1 right-closed bins; a value equal to the lowest edge or above the highest edge falls outside all bins and is excluded.
labelsstringOptional comma-separated labels, one per bin (i.e. one fewer than breaks). Empty (default) labels each bin by its upper edge, matching height classes like 5, 10, ..., 35.
min_validnumberValues strictly below this are treated as nodata. Leave unset to skip.
max_validnumberValues strictly above this are treated as nodata. Leave unset to skip.
area_scalenumberMultiplier applied to the per-pixel ground area, e.g. 1e-6 to convert square metres to square kilometres. Default 1 (area in CRS units squared).

Outputs🔗

NameTypeDescription
tablefile (CSV)One row per bin in ascending order, with columns label, lower, upper, count (pixels), and area (count x pixel area x area_scale).