Whitebox QGIS Practice 26 — Elevation Statistics of Two Basins: Means of 2,000.1 m and 1,924.3 m, a 76 m Gap

In practice 25 I subtracted basin B from the whole basin of outlet A and made an interval basin (A−B) polygon. The areas were already known: 121.878 ha and 21.1372 ha. This time I wanted to see how the two basins differ in terrain. Both drain to outlets near the same confluence, but one takes in a wide stretch of the upper main stream and the other is a short tributary. On the hillshade the interval basin seems to reach higher ridges, but to say that with numbers you need zonal statistics: the mean, median, minimum and maximum elevation of the DTM cells that fall inside each polygon.

I kept the method simple. Merge the two polygons into one layer, run QGIS Zonal statistics once, and compare the results in one table. Then compute the same statistics again using the practice 22 Watershed raster as zones (value 1 = A's share, value 2 = B), to check that the polygon route and the raster route count the same cells. The two routes matched down to the cell count, and the interval basin was about 76 m higher on average.

Data

The statistics are computed on the 1 m DTM from practice 11 (USGS 3DEP tile 13SDE474426, EPSG:26913, built from ground points). The polygons and the zone raster are outputs of earlier practices, copied into a new input folder; the copies have the same hashes as the originals.

InputOriginal sourceContent
wb26_input_dtm1m.tifPractice 11 DTM1500×1500 cells, 1 m, float32, NoData −32768
wb26_input_interval_A_minus_B.shpPractice 25 wb25_interval_areaInterval basin 1,218,780 m², valid polygon (one 7 m² hole), VALUE 1
wb26_input_poly_B.shpPractice 24 wb24_poly_ab_1Basin B 211,372 m², valid polygon, VALUE 2
wb26_input_watershed_ab_wb22.tifPractice 22 Watershed (A, B order)Value 1: 1,218,780 cells, value 2: 211,372 cells, same grid as the DTM

The whole DTM has 1,834 NoData cells, but none fall inside the two basins. So the cell counts below equal the polygon areas in m².

Merging the two polygons into one layer

I opened Processing Toolbox → Vector general → Merge vector layers. You could run zonal statistics on each layer separately, but then the results end up in two files that you have to combine again to compare. Making one layer from the start makes the table easier to read.

FieldValueWhy
Input layersInterval basin A minus B (practice 25), Basin B polygon (practice 24)The two basins to compare
Destination CRSLeft emptyBoth layers are EPSG:26913, so there is nothing to reproject
Add source layer informationChecked (default)Adds layer and path fields, so you can tell which file each row came from
Mergedwb26_basins_merged.shp
QGIS Merge vector layers dialog (Korean interface). The input layers box shows two selected inputs, the destination CRS is empty and Add source layer information is checked. The output is wb26_basins_merged.shp in the WB26_Zonal_20261114 folder. Behind it, the map shows the purple interval basin and the orange basin B over a hillshade.
Figure 1. The Merge vector layers dialog, with the interval basin (purple) and basin B (orange) behind it. The screenshots show QGIS with a Korean interface.

The merge produced two polygons, both valid. One thing in the attributes needs attention. Both source files have FID 1, so after the merge both rows still have FID 1. Use VALUE (1, 2) or the layer field to tell them apart. Also, the area_ha field existed only in the interval basin, so the B row came in as NULL. Merging takes the union of the fields; it does not fill in missing values.

Zonal statistics

I used Processing Toolbox → Raster analysis → Zonal statistics. This is the version that writes the result to a new layer, so no fields are added to the source polygons.

FieldValueWhy
Input layerBasins merged (interval + B)The layer just merged
Raster layerDTM 1 m elevation (practice 11)I loaded the same file a second time for the statistics, separate from the copy shown as a hillshade. The result is identical; it just avoids confusion in the layer list
Raster bandBand 1Single band
Output column prefixz_Shapefile field names are cut at 10 characters, so keep it short; z_median and z_stdev still fit
Statistics to calculateCount, Mean, Median, St dev, Minimum, Maximum, RangeSum is meaningless for elevation, so I left it out. Count is there to check the area
Zonal statisticswb26_zonal_dtm.shp
QGIS Zonal statistics dialog (Korean interface). Input layer Basins merged (interval + B), raster layer DTM 1 m elevation (practice 11), band 1 (Gray), output column prefix z_, with count, mean, median, standard deviation, minimum, maximum and range selected. The output is wb26_zonal_dtm.shp. Behind it, the two merged polygons are filled in green tones.
Figure 2. The Zonal statistics dialog, with seven statistics selected and the prefix shortened to z_.

The run took less than a second. Counting about 1.5 million cells into two polygons is light work even for a 1 m DTM. I colored the result layer by VALUE and labeled it with the mean, minimum and maximum elevation.

Map with the interval basin filled purple and basin B filled orange over a hillshade. The interval basin is labeled A-B, mean 2,000.1 m, min 1,764.9 / max 2,284.5 m, and basin B is labeled B, mean 1,924.3 m, min 1,765.3 / max 2,206.7 m. Near the outlets at the upper right are the point labels ID 1 = A and ID 2 = B, with blue stream lines on top.
Figure 3. The zonal statistics colored by VALUE, labeled with mean, minimum and maximum elevation.
Attribute table of the Basins with DTM zonal stats layer. Both rows have FID 1, VALUE 1.0000 and 2.0000. area_ha is 121.8780 and NULL, z_count 1218780 and 211372, z_mean 2000.132430 and 1924.342586, z_median 1994.911071 and 1910.161926, z_stdev 111.3837486 and 86.70015644, z_min 1764.892822 and 1765.329589, z_max 2284.531494 and 2206.698242.
Figure 4. The zonal statistics attribute table. Both rows have FID 1, so VALUE tells them apart.

The two basins in one table

ItemInterval basin A−B (VALUE 1)Basin B (VALUE 2)Difference (A−B minus B)
Cell count (= area in m²)1,218,780211,372
Mean elevation2,000.13 m1,924.34 m+75.79 m
Median1,994.91 m1,910.16 m+84.75 m
Minimum1,764.89 m1,765.33 m−0.44 m
Maximum2,284.53 m2,206.70 m+77.83 m
Range519.64 m441.37 m+78.27 m
Standard deviation111.38 m86.70 m+24.68 m

The minimum elevations differ by only 0.44 m. In practice 22 outlets A and B were placed near the same confluence, so this is expected, and the number actually confirms that the outlets are in the right place. The difference is at the top. The maximum is almost 78 m higher, and the mean and median are 76–85 m higher. The impression that the interval basin collects water from higher ridges holds up in numbers.

The mean alone hides something, so I looked a little further at the distribution in the validation script. In the interval basin, 48.7% of cells are at or above 2,000 m and 8.4% are below 1,850 m. In B, 19.2% are at or above 2,000 m and 21.5% are below 1,850 m. The interquartile range is 1,910.80–2,087.21 m for the interval basin and 1,857.31–1,979.13 m for B, so B has a larger share of low ground near the outlet. B's median (1,910.16 m) being about 14 m below its mean (1,924.34 m) tells the same story: a long tail toward higher ground, with the bulk of the basin low.

The interval basin has the larger standard deviation. It is more than five times larger and spans a wider elevation range, so that is natural. A large standard deviation does not mean steep slopes, though. A large basin can have a wide elevation spread even if it is gentle; to talk about slope you need separate statistics on a slope raster.

Once more with raster zones

To check that the polygon route counted correctly, I ran Processing Toolbox → Raster analysis → Raster layer zonal statistics. This tool takes the zones as raster values instead of polygons, and the practice 22 Watershed raster is exactly such a zone raster.

FieldValueWhy
Input layerDTM 1 m elevation (practice 11)The raster to summarize
Band number1
Zones layerWatershed A,B raster (practice 22)Value 1 = A's share, value 2 = B
Zones band number1
Reference layerInput layer (default)Both rasters share the same grid, so no resampling happens either way
Statisticswb26_raster_zones_dtm.csvA single table, so I saved it as CSV
Table DTM stats by practice 22 raster zones. Row zone 2.00000000: m2 211372, sum 406752141.1, count 211372, min 1765.329589, max 2206.698242, mean 1924.342586. Row zone 1.00000000: m2 1218780, sum 2437721404, count 1218780, min 1764.892822, max 2284.531494, mean 2000.132430.
Figure 5. Results using the practice 22 Watershed raster as zones. Cell counts, mean, minimum and maximum match Figure 4.

Zone 1 has 1,218,780 cells and zone 2 has 211,372, exactly the same counts as the polygon route. The mean, minimum and maximum match to the displayed digits. In the validation script I rasterized the polygons onto the same grid and compared them cell by cell with the practice 22 zones: 0 mismatched cells for both basins. This also means the 7 m² hole from practice 25 is excluded identically on both sides: in the polygon it is a hole, so no cell centers fall inside, and in the raster it was NoData to begin with.

This tool does not give the median or standard deviation; it adds sum and m2 (area) columns instead. So rather than one replacing the other, I think the two tools work best together: compute the statistics with one, and check the cell counts and means with the other.

Practical notes

  • Zonal statistics counts only cells whose centers fall inside the polygon. When the polygon was made from the raster and follows cell edges, as here, the count and area match exactly. With boundaries from another source or a coarser raster, edge cells make count × cell area differ from the polygon area. Including the count and comparing it with the area shows such differences right away.
  • Merging can leave duplicate FIDs from the sources. Check for a field that tells the rows apart (VALUE here), and create one before merging if there is none.
  • When saving to shapefile, statistic field names are cut at 10 characters. Use a short prefix or save to GeoPackage.
  • The same statistics on a DSM would mix in building and canopy heights. Use a bare-earth DTM to compare basin terrain; this data is a DTM built from ground points.
  • Do not report only the difference in means; look at the median or quartiles too. In a basin like B, with most of its area on low ground, the mean is pulled above the typical elevation.

Next

The means and quartiles showed that the two basins have differently shaped elevation distributions. Next I plan to split the DTM into elevation bands (for example 50 m) and count the area in each band for each basin, to compare their elevation–area distributions side by side.

Work notes

This practice was run by a QGIS startup script that entered the values into the same dialogs, and the screenshots were captured at that time. The original project was left untouched and the work was done in a copy (QGIS_Play_NaGeoAI_20261114_26_zonal.qgz). The data source is USGS 3DEP (The National Map) tile 13SDE474426; the original DTM is not redistributed. The results were re-read by a separate script and the same statistics were computed independently with numpy (maximum difference below 1×10⁻¹² m), and the polygons were matched cell by cell against the practice 22 zone raster (0 mismatched cells). The quartiles and elevation-band shares come from that script. QGIS 3.44.15, GDAL 3.13.3. SHA-256 (first 12 characters):

  • Input DTM 069a9c249240
  • Input interval basin / B polygon .shp d891fa55042f / 0796f835c715
  • Input Watershed (practice 22) 12c9fc71b969
  • Merged .shp / .dbf 235891ed214a / 67d3965a6d72
  • Zonal statistics .shp / .dbf 235891ed214a / e8e1b17ea680
  • Raster zonal statistics .csv 0f55aa98e87c

This is an English adaptation of a post in the Korean Whitebox QGIS practice series. The original is here.

This post was drafted with the help of AI tools and reviewed by the author. Tool features and data portals change over time, so check the official documentation when you practice.

Comments

Popular posts from this blog

What Is GeoAI, and How Does It Help Disaster Safety?

Getting Started with QGIS: What to Do First