TransWikia.com

Anti-aliased rasterization of vectors in QGIS?

Geographic Information Systems Asked by tgerdin on July 4, 2021

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.

One Answer

  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. 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

Correct answer by Babel on July 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP