QGIS Rasterization – Rasterizing Vector Shapefiles Using Multiple Burn-in Values

gdal-rasterizeqgisrasterization

I have a vector shapefile of a geographic area that contains several different object codes that I need to rasterize all as one image.

enter image description here

I am working with QGIS, but the Raster>conversion tool seems to be unable to do this (see below). In the tool, it seems as though you cannot select more than one value, not to mention the tool – even when it works – appears to ignore whatever value you have set and rasterizes all values at once, in the same image. That would be ok, but I have a large volume of output to complete, and I don't want to separate every needed raster mask into a separate layer, as this would be extremely inefficient and time consuming

enter image description here

So, I tried running the GDAL command it generates, and even the GDAL command, when entered in the python console, errors out. I have never worked with GDAL before and I am way out of my depth, so I am looking for pointers.

Below is the command I started with (and that QGIS ran successfully when I attempted to rasterize using the form), and no variation of this seems to work in the console, and any edit I perform also returns a syntax error.

gdal_rasterize -l bornholm_final_OSM -a code -ts 100492.0 100492.0 -te
14.510849428030344 54.922004004836744 15.299793055831714 55.37191520877061 -ot Float32 -of GTiff "E:/My Documents/OSM Stuffbornholm_final_OSM.shp"
"E:/My Documents/OSM Stuff/point-5m test_qgis2.tif"

I have looked at the GDAL documentation about "gdal_rasterize", but I am truly stumped at this point. I just need the raster to do multiple classes in one image.

Using @BenW'S answer I am now able to create a raster using GDAL, but I now need to ensure that it retains the colors.

enter image description here

Best Answer

I notice you have entered a field to use for the burn in value AND a fixed value to burn. I think you should only enter one or the other. If you enter a fixed value, all your vector features will be burnt to the resulting raster with that one value. If your vector layer has a numeric attribute field with certain values for each feature then you select that field to used for burn in value. Your vector features will then be rasterized with the values from the specified attribute field being burnt to the resulting raster.

If you want to run processing algorithms from the Python console you have to do something like:

import processing
processing.run(name_of_algorithm, parameters)

EDIT:

GDAL is a library of command line utilities, so the command as you have typed it above should be run from the OSGEO4W shell (I am assuming you are on Windows here) not from inside QGIS. The quickest way to launch it is to type "osgeo4w shell" into the search function. At the command prompt you first need to change directory to the location of your shapefile. To do this type cd followed by a space then the full path to your file. Hit enter then type your command e.g. gdal_rasterize etc and hit enter again to run the command

QGIS also provides access to GDAL utilities through the processing toolbox with a gui for entering parameters as shown in in your original post. This should also work fine for your purpose. It is not entirely clear to me what result you are looking for. It seems from your comment that you want to rasterize all features in your vector that have the value 7502 in an attribute field "code" and burn only those features with that value to the resulting raster. You should select those features in QGIS first which you can do with the option: select features by expression and use your expression "code" = 7502. Make sure the Selected features only box is checked and enter EITHER the "code" field to use for the burn in value OR 7502 as the fixed value (not both).

EDIT 2:

From your original question I assumed that your vector layer has an attribute field "code" with a number of unique numerical values; I guess that field is what has been used to style the vector, and that you want to produce a raster where all the vector features are burnt with the corresponding raster pixels having the values from the "code" field. If that's the case then I'm not sure why you included the 'where' expression. You should include all features and just specify the attribute field "code" to use for the burn in value. You can then use those values to apply a similar colour style to your raster.

Related Question