Aggregating a categorical raster to zones — Recipe (QGIS)
Tool-bound implementation of the Aggregating a categorical raster to zones (Task) Task Schema in QGIS 4.2. Worked example: Soil carbon and land use at Ledreborg (Example).
Data you need
| Role | Form | Level of measurement | Notes |
|---|---|---|---|
| Categorical raster | raster, one band of class codes | nominal (or ordinal) classes | e.g. a land-cover or land-use map |
| Symbology file | .qml next to the raster | — | a Paletted/Unique values style whose labels name the classes; without it the columns get the raw codes |
| Zones | polygons | nominal zone type | e.g. landscapes, municipalities, grid cells |
| Composition (output) | the zones with one field per class | ratio (area or percentage) |
Raster and zones in the same CRS.
Prepare the raster (Task Schema step 1)
Add the raster. A style file with the same name as the raster (<raster>.qml) loads automatically; otherwise Layer Properties › Symbology › Style › Load Style…. Check that the legend shows class names, not codes.
One zone per zone type (step 4, done first)
If several polygons share a type, Dissolve (Tool - QGIS) the zones by the type field first. The shares are then computed from summed areas, never averaged.
Tabulate (step 3)
Tabulate area (Tool - QGIS) (SemanticGIS plugin): Categorical raster the raster, Polygons the (dissolved) zones, Report as Percent of the zone’s classified pixels or Area. The output has one column per legend label; codes without a label get a column value <n>. Without the plugin, QGIS’s Zonal histogram gives the same counts with the codes as column names.
Chart (step 5)
Export the table (Export › Save Features As…, format MS Office Open XML spreadsheet, without geometry) and make a 100 % stacked bar chart, one bar per zone. Group small classes first.
Checked in QGIS 4.2.2, October 2026.