Calculate overall area of physically discrete raster areas in QGIS

I need to get the overall area of physically discrete areas of a raster layer up to a given cell count in QGIS. The following approach is what I've tried so far, but it seems a bit too complicated and I'm looking for a simpler approach.

1. Use r.clump GRASS raster tool to get the physically discrete areas ("Clump also diagonal cells" ticked in my case).
2. Use Raster layer zonal statistics tool to create the statistics table.
3. Sort attribute table by cell count field (ascending).
4. Select all rows up to the given cell count (in this particular case: cell count of the largest obvious sliver polygon equivalent).
5. Get sum of area of all selected rows (I saved the selection to a new table layer for this).

An important part of a simpler approach that I have been thinking of is to check for the cell count in the Raster calculator and use it as value for the cells in the output, but I could not achieve this so far.

Just for illustration, here's the r.clump result where every region has its own and distinct raster value:

The exercise is to get the cell count of all sliver polygon equivalents.

• Can you post a sample of your data or at least a screenshot of your raster? Commented May 21, 2023 at 10:17
• I added a screenshot (GIS is art!). How am I supposed to post a sample of raster data? Commented May 21, 2023 at 19:13
• Upload the data to a cloud and share the link here. Commented May 21, 2023 at 19:28

If you have a raster in which some cells have values, and the others are NULL, then the GRASS GIS module r.stats can help. After your r.clump step, just run r.stats -a on the clumped raster and it will give you the area (sq.m.) of each clump, and the total area of the NULL cells.

If you don't care about the separate discrete areas, then first run: r.mapcalc "single_value_raster = if (isnull(<input_raster>), null(), 1)" This gives you a raster with value 1 for all pixels that have some value in the original, and NULL everywhere else. Then do the r.stats step and you'll get two numbers: total area of cells with value 1, and total area of NULL cells.

• I would like to upvote your answer, like two others already did (which implies that others confirmed that you answered my question). However, unfortunately I did not understand yet how it solves my problem. I don't have NULL values. See added screenshot in the OP with the r.clump result. Commented May 21, 2023 at 19:19
• I guess I didn't understand the problem. In your question you said: "the overall area of physically discrete areas of a raster layer up to a given cell count". So I assumed that there were some parts of the overall area that were not in the discrete areas. Is the issue with the small gaps between the larger clusters? If so, then take the output of r.stats as above, and filter out those gap areas that are "too small" . Commented May 23, 2023 at 7:45
• Sorry if my question was unclear or a bit confusing. The screenshot is taken from my own "reconstruction" of a comparable case, as I did not want to include original data here. I saw now that I there actually are some white cells, which should definitely not be the case. The issue is with the sliver polygons: Find the area of the largest sliver polygon. See Babel's answer. Anyway, thank you for your answer. Commented May 24, 2023 at 4:26

Run Raster Layer Zonal Statistics on the clumped raster layer and you get a data only layer output (without geometries) where for each zone (raster value) you have a separate feature (row) containing the area. Use twice the clumped raster layer as Input as well as Zones layer. As you have the same layer, it doesn't depend what you choose for Reference layer.

To calculate the area of all sliver areas (here: all areas < 2000) at once, use function sum() with this expression:

sum (m2, filter:=m2<2000)

It sums up the values of the attribute field m2, taking into consideration only the values <2000. Result here, using the data you provided: 7275.

Screenshot: largest area in the raster are pixels with value=6, they cover 366625 square meters:

• Thank you very much, this is what I was looking for. Commented May 24, 2023 at 4:29
• I deleted the cloud link because I didn't want my own cloud to be linked here permanently. This is not good practice, for sure, and I will look for a better solution to share my data. Commented May 24, 2023 at 4:31