3

I'm trying to rasterize a simple line-vector layer. I can do it by using QGIS GUI, but I want to do the same with python console. Unfortunately it always gives back an "Error: Wrong number of parameters" or "Wrong parameter:___".

Call like processing.alglist('gdalogr:rasterize') gives the shown list of parameters, where for many of them there is no further explanation what they are and how to set them. I'm using QGIS 2.16.1.

Has anyone made a successful call for this algorithm?

ALGORITHM: Rasterize (vector to raster)

INPUT <ParameterVector>
FIELD <parameters from INPUT>
DIMENSIONS <ParameterSelection>
WIDTH <ParameterNumber>
HEIGHT <ParameterNumber>
RAST_EXT <ParameterExtent>
TFW <ParameterBoolean>
RTYPE <ParameterSelection>
NO_DATA <ParameterString>
COMPRESS <ParameterSelection>
JPEGCOMPRESSION <ParameterNumber>
ZLEVEL <ParameterNumber>
PREDICTOR <ParameterNumber>
TILED <ParameterBoolean>
BIGTIFF <ParameterSelection>
EXTRA <ParameterString>
OUTPUT <OutputRaster>
3

Borrowing @Germán Carrillo method of calling processing algorithms using a dictionary (as described in this post), you could use something like:

import processing

input = iface.activeLayer()
layer = QgsVectorLayer(input.source(),"polygon","ogr")

extent = layer.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

processing.runalg("gdalogr:rasterize",
                   {"INPUT":layer,
                   "FIELD":"d",
                   "DIMENSIONS":0,
                   "WIDTH":1,
                   "HEIGHT":1,
                   "RAST_EXT":"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),
                   "TFW":1,
                   "RTYPE":5,
                   "NO_DATA":0,
                   "COMPRESS":0,
                   "JPEGCOMPRESSION":1,
                   "ZLEVEL":1,
                   "PREDICTOR":1,
                   "TILED":False,
                   "BIGTIFF":2,
                   "EXTRA": '',
                   "OUTPUT":"C:/Users/You/Desktop/result.tif"})

You could have a look at the rasterize.py script where some of the paramters are defined in the comments inside the defineCharacteristics() function.

  • It is still missing 2 parameters: RAST_EXT <ParameterExtent> and EXTRA <ParameterString>. Is there any explanation for these two? I was looking at the links you provided, but I'm still quite unfamiliar how to read (understand) them...Tnx – Pandza Sep 21 '16 at 12:37
  • @Pandza - Apologies, edited the post so it should run now =) – Joseph Sep 21 '16 at 13:07
  • Tnx a lot. Now I can call this function and get a raster. For my simple line result raster looks like "background pixels-no data" and "line pixels-value is 2". Can I somehow set it to get 0 for background and 1 for line pixels? – Pandza Sep 28 '16 at 11:56
  • @Pandza - Most welcome, glad it worked! Hmm, I would probably use the SAGA Raster Calculator and use an if else statement like: ifelse((a=2),1,0). – Joseph Sep 28 '16 at 12:01
  • 1
    Yes, Raster calculator was also my idea in case there is no option to set it straight away. Regards – Pandza Sep 28 '16 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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