{"id":2042,"date":"2025-03-31T19:02:52","date_gmt":"2025-03-31T17:02:52","guid":{"rendered":"https:\/\/hannes.enjoys.it\/blog\/?p=2042"},"modified":"2025-03-31T21:00:50","modified_gmt":"2025-03-31T19:00:50","slug":"calculate-the-percentage-of-cells-per-value-category-in-a-raster-in-qgis","status":"publish","type":"post","link":"https:\/\/hannes.enjoys.it\/blog\/2025\/03\/calculate-the-percentage-of-cells-per-value-category-in-a-raster-in-qgis\/","title":{"rendered":"Calculate the percentage of cells per value category in a raster in QGIS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/mapstodon.space\/@kbevers\/114257666126054055\">Kristian Evers asked on the fediverse<\/a>: <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">I have a raster where each cell is categorized with a value of either 1, 2 or 3.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/norden.social\/tags\/QGIS\" target=\"_blank\" rel=\"noreferrer noopener\">#QGIS<\/a>, how would I determine the percentage of cells in each category?<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">This piqued my curiosity so here you go!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/docs.qgis.org\/testing\/en\/docs\/user_manual\/processing_algs\/qgis\/rasteranalysis.html#qgisrasterlayeruniquevaluesreport\">Raster layer unique values report<\/a> tool provides both a table containing <code>value<\/code> &amp; <code>count<\/code> columns and a number of outputs, among other things <code>TOTAL_PIXEL_COUNT<\/code> and <code>NODATA_PIXEL_COUNT<\/code>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"529\" height=\"478\" src=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-44.png\" alt=\"\" class=\"wp-image-2046\" srcset=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-44.png 529w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-44-300x271.png 300w\" sizes=\"auto, (max-width: 529px) 100vw, 529px\" \/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"647\" height=\"469\" src=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-43.png\" alt=\"\" class=\"wp-image-2045\" srcset=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-43.png 647w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-43-300x217.png 300w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-43-624x452.png 624w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><figcaption class=\"wp-element-caption\">Unique values tables for a UInt16 raster<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The Field calculator can be used to calculate an additional column for the table. Note that I decided for <code>NODATA<\/code> pixels to <strong>not<\/strong> count towards the total number of pixels here!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"558\" src=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/fc-1024x558.png\" alt=\"\" class=\"wp-image-2047\" srcset=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/fc-1024x558.png 1024w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/fc-300x164.png 300w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/fc-768x419.png 768w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/fc-624x340.png 624w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/fc.png 1363w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Combine them in a model, add appropriate inputs and you have a fancy new QGIS tool to calculate the percentages of each value in a raster image:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"637\" height=\"284\" src=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/qgis-model-value-percentages-1.png\" alt=\"\" class=\"wp-image-2044\" srcset=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/qgis-model-value-percentages-1.png 637w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/qgis-model-value-percentages-1-300x134.png 300w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/qgis-model-value-percentages-1-624x278.png 624w\" sizes=\"auto, (max-width: 637px) 100vw, 637px\" \/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"471\" height=\"105\" src=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-46.png\" alt=\"\" class=\"wp-image-2049\" srcset=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-46.png 471w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/image-46-300x67.png 300w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Here is my model file: <\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-1f120394-91d2-451d-ab5e-e8ab15df0cc2\" href=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/Percentage-of-cells-per-value-category.model3_.zip\">Percentage of cells per value category.model3<\/a><a href=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/Percentage-of-cells-per-value-category.model3_.zip\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-1f120394-91d2-451d-ab5e-e8ab15df0cc2\">Download<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Kristian Evers asked on the fediverse: I have a raster where each cell is categorized with a value of either 1, 2 or 3. In #QGIS, how would I determine the percentage of cells in each category? This piqued my curiosity so here you go! The Raster layer unique values report tool provides both a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,31],"tags":[],"class_list":["post-2042","post","type-post","status-publish","format-standard","hentry","category-gis","category-qgis"],"_links":{"self":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/2042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/comments?post=2042"}],"version-history":[{"count":3,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/2042\/revisions"}],"predecessor-version":[{"id":2053,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/2042\/revisions\/2053"}],"wp:attachment":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/media?parent=2042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/categories?post=2042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/tags?post=2042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}