3

I have a irregular raster with empty spaces as seen in the attached image, which is the southwest corner of my raster. I would like to create a polygon or a mask that matches the non-white regions of the raster using QGIS.

When I went to "Vector -> Research Tools -> Extract Layer Extent," QGIS created the polygon seen in pink. Note that the left-most edge of the raster is in the image shown. In other words, somehow the layer extent goes beyond the visible part of the raster.

In any event, I would like to create a mask/polygon that covers only the visible part of the raster. In the image shown, it would mean a polygon that does not include the square empty space.

An example of raster irregularities.

0

1 Answer 1

2

To get a polygon layer that covers all - and only those - areas where you have (visible) pixels, proceed as follows. You can first create a binary raster with raster calculator, based on the values of the visible pixels, and than create contour polygons from it.

  1. Inspect your raster with Identify Features from the toolbar. In the screenshot below, I have a raster in colors for land, white for the sea. As can be seen in the right part, white pixels have the same value 255 for all three channels (red, green, blue). Based on this, we can create a condition: all pixels where all three channels are equal to 255 should belong to one category, all others to the other category.

  2. Open Menu Raster / Raster calculator and set the Raster Calculator Expression to "raster@1" < 255 and "raster@2" < 255 and "raster@3" < 255. The names of "raster@3" can be different, based on the name of your raster layer. Best just select them from the list in the upper part by double clicking. Define an output path and run the tool.

enter image description here

  1. You get a binary black/white raster: white land, black water. Now run the GDAL contour polygon tool: Menu Processing / Toolbox / Contour polygons. Select the b/w raster created in step 2 as input and set Interval between contour lines to 1. Run the tool:

enter image description here

  1. You get a polygon layer as output with two polygon features - one with elevation min/max 0/1 (sea) and the other with 1/2 (land). Select the one you want to deleted, toggle edit mode and delete it.

enter image description here

You remain with a polygon layer that exactely covers the (visible) pixels of your raster:

enter image description here

1
  • This is perfect. Thank you so much!
    – TCR
    Commented Jan 20, 2021 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.