Monday, 12 March 2018

Calling gdalogr:rasterize algorithm in python console in QGIS?


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 
FIELD
DIMENSIONS
WIDTH
HEIGHT
RAST_EXT
TFW
RTYPE

NO_DATA
COMPRESS
JPEGCOMPRESSION
ZLEVEL
PREDICTOR
TILED
BIGTIFF
EXTRA
OUTPUT

Answer




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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...