2

I have some polygons that I want to rasterize, but in a fairly low-resolution grid (cell size 250x250m). The raster values of the area inside the polygons should normally have the same value (100), but most of the polygons are fairly small (say max 50 m wide), which means that right now they often don't even show up in the final raster (using gdal:rasterize or saga:rasterize). What I would have liked is for the raster value to have been interpolated, in the sense that the final pixel value would have been lower than 100 (say 30) but still present in the output raster. Is there any stock algorithm in QGIS capable of this?

I guess that another way would be to first do a much higher-resolution bi-valued rasterization, and then downsample the resulting raster with an algorithm that hopefully handles this. This would probably be slower and require more memory.

1 Answer 1

3

Update: see here for a model to automatically run this workflow.

  1. Create a vector grid with the same extent and resolution as the raster you intend to create.

  2. Calculate the percentage (area) each grid cell intersects with the polygons to be rasterized - see screenshot 1. Use filed calculator to create a new field with this value. To calculate the percentage, you can use this expression (replace poly with the name of your polygon layer):

    area (
        intersection (
            $geometry,
            collect_geometries(
                overlay_intersects( 
                    'poly',
                    $geometry
                )
            )
        )
    )
    /area ($geometry) * 100
  1. Use this percentage value for the raster value to be created: from 100 (complete interesection) to 0 (no intersection at all) wit in-between values based on intersection percentage, see screenshot 2. For rasterize, use Menu Raster / Conversion / Rasterize (vector to raster) - see screenshot below for settings: set the grid and the attribute field with the calculated percentage (named pixelval in my case) is input and set Output raster size units to the same size as your grid and Output size to the extent of your grid-layer (or your polygon-layer, because the extent of the grid should be the same as the polygon, see step 1). Be aware: the screenshot shows a layer in EPSG:3857, this normally is not ideal for processing, just due to creation of a fast test dataset for visualization.

Screenshot 1: Polygons in yellow, grid in blue. The grid-cells are labeled (for visualization purpose) with the expression from above + a round() function: enter image description here

Screenshot 2: And that's how the final raster looks like:

enter image description here

Screenshot 3: setting of the rasterize dialog: enter image description here

Edit/addition

I created a model for this workflow that can be downloaded here (update: improved version here). You have to adapt the values and make sure that the project CRS is the same as the CRS of the input polygon layer you load. The expression from step 2 refers to the layer named polygon, so the input layer should be named like this.

Screenshot of the model: enter image description here

10
  • Thank you, this is a very exact (and may I say "GIS-esque") way of solving it (whereas my idea was more of an image processing-approach). Will try it out!
    – tgerdin
    May 20, 2021 at 9:45
  • Indeed, the solution is "GIS-esque", nicely sayed ;-) It depends of course for what you need it, but I suppose using a GIS (instead on an image processing software), makes sense if you apply space aware operations and that's why you opted for it.
    – Babel
    May 20, 2021 at 9:51
  • In the expression, be sure to replace 'poly' with the name of your polygon layer.
    – Babel
    May 20, 2021 at 9:52
  • Sure did, worked nicely! Any tips on specific algorithm to use for the rasterization (step 3)? I am quite new to QGIS.
    – tgerdin
    May 20, 2021 at 10:07
  • Update my answer for your question about rasterize
    – Babel
    May 20, 2021 at 13:23

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.