4

Using QGIS I want to rasterise a series of polygon layers so the output is the same size and resolution. I am using Rasterise, however am not getting the result I want.

I have a raster that is the size and resolution that I want - "Raster".

I have used Rasterise with the options Input file - Polygon layer I want to rasterise "MMEIL" Attribute field "Sus_Lvl" Output file for rasterized vectors "Raster" Keep existing raster size and resolution Load into canvas when finished.

The problem is this updates the raster in the areas where I have the polygon, but the rest keeps the values of the previous raster. I need to overwrite the entire raster. My Sus_Lvl has values 1,2,3. I want a raster that has as output values in the range 0 (no data), 1, 2,3.

If I simply set the width and height I get what I want in terms of output - but I cannot keep a consistent resolution between all the different layers.

A solution would be to update the existing raster so that all values are 0 - but I cannot figure out how to do this.

3
  • The GDAL_Rasterize command gdal.org/gdal_rasterize.html has options to implicitly specify pixel size (-tr Xres Yres), extent (-te xmin ymin xmax ymax) to ensure your rasters all match and an initialization value (-init Value) to set all the cells of the raster to a value prior to rasterization. You might need to edit that into your command line if there's not an input field on the form. Commented Oct 12, 2017 at 2:14
  • Thanks @MichaelStimson - do you know how to determine the extent of an existing raster? I have tried selecting the layer and toggling the option to extent in QGIS - but when I use these values with the flag -te I end up with a raster that isn't matching hints for what I am doing wrong would be appreciated.
    – Esme_
    Commented Oct 12, 2017 at 3:00
  • I would start with GDALinfo and look at the corner coordinates. If you're on windows start a command prompt, drag in the GDALinfo.exe from explorer, add a space, and drag the raster from explorer. You might have trouble if you don't have GDAL installed and are relying on only the QGIS install, in which case you will need to change directory (cd) to your QGIS\bin folder (like cd "C:\Program Files\QGIS Brighton\bin") and execute from there as your %path% may not be set. Or, it might be better if you used the OSGeo4W installer to find the OSGeo4W.bat and double click to get a GDAL cmd prompt. Commented Oct 12, 2017 at 3:29

1 Answer 1

5

Information about gdal_rasterize can be found here. It's important to be in mind this parameter:

-te xmin ymin xmax ymax

referred to raster extension. In raster Layer Properties you can find it in Metadata option as "Layer Extent" (see following image).

enter image description here

Copy/paste this information as plain text:

354971.3488602247089148,4471948.6279255282133818 : 356451.1233247875352390,4473428.4023900907486677

and put it as follows (eliminating commas and semi colon):

-a_nodata 0 -te 354971.3488602247089148 4471948.6279255282133818 356451.1233247875352390 4473428.4023900907486677 

Observe that it was also placed parameter for nodata values (zero in this case). You also need to write down "Pixel Size" value (73.9887 in this case).

Afterward, open Rasterize Tool (Raster -> Conversion -> Rasterize) and select Output file, Raster resolution in map units per pixel (in my case is 73.9887; Horizontal and Vertical) and finally click in pencil icon for modifying gdal_rasterize command equivalent as follow:

gdal_rasterize -a id -tr 73.9887 73.9887 -a_nodata 0 -te 354971.3488602247089148 4471948.6279255282133818 356451.1233247875352390 4473428.4023900907486677 
 -l polygon_layer2 /home/zeito/pyqgis_data/polygon_layer2.shp /home/zeito/pyqgis_data/rasterized_polygons.tif

It looks like in following image:

enter image description here

Click in OK button and result was obtained as follows:

enter image description here

Observe that each feature rasterized is perfectly aligned with base raster and its color is according to id in attributes table of polygon layer.

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.