1

I’m working on a script (that later should be used in a processing model) that uses gdal_rasterize to write different attribute values (columns) in different bands of one raster file.

I know that this I possible with gdal_rasterize using the -b and -burn flags. Anyway, I can’t manage to integrate this commands in my QGIS-Python script. Am I missing something with the syntax?

Here is the GDAL synopsis:

Usage: gdal_rasterize [-b band]* [-i] [-at]
   {[-burn value]* | [-a attribute_name] | [-3d]} [-add]
   [-l layername]* [-where expression] [-sql select_statement]
   [-dialect dialect] [-of format] [-a_srs srs_def]
   [-co "NAME=VALUE"]* [-a_nodata value] [-init value]*
   [-te xmin ymin xmax ymax] [-tr xres yres] [-tap] [-ts width height]
   [-ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/
         CInt16/CInt32/CFloat32/CFloat64}] [-q]
   <src_datasource> <dst_filename>

And here is my script so far:

 ##Rasterize_GDAL_KRK=name
 ##Input=vector
 ##Output=output raster
 ##Extent=extent
 ##cellsize=number 25

import os, processing, osgeo.gdal

processing.runalg('gdalogr:rasterize',
                                        {"INPUT":Input,
                                        "FIELD":"KRK_1",
                                        "DIMENSIONS":1,
                                        "WIDTH":cellsize,
                                        "HEIGHT":cellsize,
                                        "RAST_EXT":Extent,
                                        "TFW":0,
                                        "RTYPE":0,
                                        "NO_DATA":None,
                                        "COMPRESS":0,
                                        "JPEGCOMPRESSION":1,
                                        "ZLEVEL":1,
                                        "PREDICTOR":1,
                                        "TILED":False,
                                        "BIGTIFF":3,
                                        "EXTRA": None,
                                        "OUTPUT":Output})
  • 1
    I think it's not possible to add extra flags when using gdal from Processing. Are you able to directly call GDAL through Python? – mgri Jan 24 '17 at 14:13
  • That is what I was wondering! If it is possible to directly call GDAL through Python, I don’t know how to do it... – Miron Jan 24 '17 at 14:18
1

From the algorithm dialog you should see something like this:

enter image description here

which is a call from GDAL using Processing. So, I think it should be enough starting from it for creating your own script.

You may try using this code (you should edit the com_string string by adding your specific parameters, including the extra flags):

##Rasterize_GDAL_KRK=name
##Input=vector
##Output=output raster
##Extent=extent
##cellsize=number 25

import os, gdal
import processing


ex = Extent.split(',')
ext = str(ex[0]) + "," + str(ex[1]) + "," + str(ex[2]) + "," + str(ex[3])

com_string = "gdal_rasterize -a KRK_1 -ot 0 -of GTiff -te " +  ext + " -tr " + str(cellsize) +" " + str(cellsize) + " -co COMPRESS=NONE -co BIGTIFF=IF_NEEDED -l " + str(Input)
os.system(com_string)

As a first step, I suggest you to initially copy-and-paste only what you see in the Processing dialog after having set your input parameters, and then trying to add your extra flags in the code.

  • All right, I will try that and report back. – Miron Jan 24 '17 at 14:41
  • @Miron I edited the code. Now it should be easier to adapt it to your case. – mgri Jan 24 '17 at 15:21
  • I tryed the code (I think a " is missing at the end, but it still gives me "SyntaxError: invalid syntax". Don't know what I'm missig... – Miron Jan 24 '17 at 15:41
  • The error is certainly in the com_string string (in this answer, the syntax for calling GDAL worked). Could you please paste here what you see in the GDAL console from the algorithm dialog (with your parameters)? – mgri Jan 24 '17 at 15:48
0

I finally solved the problem, it must have had something to do with the com_string string. At the end, I used the following code:

##Rasterize_GDAL_KRK_3.4=name
##Input=vector
##KRK_Feld=field Input
##Output=folder
##Extent=extent
##Zellengroesse=number 25

import os, gdal
import processing

shapefile = Input
(shapefilefilepath, shapefilename) = os.path.split(shapefile)
(shapefileshortname, extension) = os.path.splitext(shapefilename)

outrastername = shapefileshortname + '.tif'
outraster = Output + '/' + outrastername

ex = Extent.split(',')
ext = str(ex[0]) + " " + str(ex[1]) + " " + str(ex[2]) + " " + str(ex[3])

os.system('gdal_rasterize -a ' + str(KRK_Feld) + ' -ot Byte -of GTiff -co COMPRESS=NONE -co BIGTIFF=IF_NEEDED -te ' + ext + ' -tr ' + str(Zellengroesse) + ' ' + str(Zellengroesse) + ' -l ' + shapefileshortname +' ' +  shapefile + ' ' + outraster)

Now I just need to add the -b and -burn flag.

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.