3

I want to use the QGIS python scripting functions to turn to rasterize a point vector using many different dimensions.

Right now my code looks like this:

import processing

RasterPixelSize = (10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0)

for rps in RasterPixelSize:
    processing.runalg("gdalogr:rasterize","/home/cparr/Documents/Inuvik   /West_Hourglass/spiral.shp","DEPTHCM_AL",None,0,[rps],[rps],0,"/home/cparr/Documents/Inuvik/SpiralToRaster/test.tif")

print "Done"

And in the console I get this message:

execfile(u'/home/cparr/Documents/Inuvik/QgisScripts/rasterize.py'.encode('UTF-8'))
Error: Wrong parameter value: [10.0]
Error: Wrong parameter value: [20.0]
Error: Wrong parameter value: [30.0]
Error: Wrong parameter value: [40.0]
Error: Wrong parameter value: [50.0]
Error: Wrong parameter value: [60.0]
Error: Wrong parameter value: [70.0]
Error: Wrong parameter value: [80.0]
Error: Wrong parameter value: [90.0]
Error: Wrong parameter value: [100.0]
Done

The help for rasterize shows:

processing.alghelp("gdalogr:rasterize")
ALGORITHM: Rasterize (vector to raster)
INPUT <parameters>
FIELD <parameters from INPUT>
WRITEOVER <parameters>
DIMENSIONS <parameters>
WIDTH <parameters>
HEIGHT <parameters>
RTYPE <parameters>
OUTPUT <OutputRaster>


DIMENSIONS(Set output raster size (ignored if above option is checked))
0 - Output size in pixels
1 - Output resolution in map units per pixel
RTYPE(Raster type)
0 - Byte
1 - Int16
2 - UInt16
3 - UInt32
4 - Int32
5 - Float32
6 - Float64
7 - CInt16
8 - CInt32
9 - CFloat32
10 - CFloat64

And I believe my parameters are correct. Does anyone have experience scripting the QGIS processing tools from the python console?

  • 1
    Does it work if you use integer values? – BradHards Feb 27 '15 at 23:27
  • I tried replacing the floats with integers - same result! e.g. "Error wrong parameter value: [20]. – Charlie Parr Feb 28 '15 at 0:04
  • Hmm, what about if you say [rps,rps] or rps,rps – BradHards Feb 28 '15 at 2:46
  • @BradHards the code runs using rps,rps but does not create any output.tif files. Maybe something is wrong with my output parameter string. I'll try some other output options. – Charlie Parr Feb 28 '15 at 3:15
  • I have QGIS 2.6.1 installed and when I check the parameters for the rasterize algorithm, I do not have the WRITEOVER and RTYPE parameters but I am able to run the algorithm using your code (excluding those parameters mentioned). Did you install additional libraries/plugins? – Joseph Mar 2 '15 at 10:36
2

The tool works well. I modified slightly the code for producing only 3 raster (10, 20 and 30 Pixel Size) with a point shapefile with 5 features.

import processing

RasterPixelSize = (10.0,20.0,30.0)

i=1

for rps in RasterPixelSize:

    processing.runalg("gdalogr:rasterize",\
    "/home/zeito/pyqgis_data/point.shp",\
    "id",None,1,rps,rps,6,
    "/home/zeito/pyqgis_data/test" + str(i) + ".tif")

    i += 1

print "Done!"

The first one with 10 Pixel Size:

enter image description here

The second one with 20 Pixel Size:

enter image description here

and finally the third with 30 Pixel Size:

enter image description here

I used a RTYPE of 6 (Float64) whose type was effectively checked in the Layer Properties of each raster.

  • Thanks for the answer. Maybe the problem was with my output string or the RTYPE parameter I selected. – Charlie Parr Mar 2 '15 at 18:26
  • The first problem was to use a list for the RasterPixelSize. On the other hand, how many features does the vector point have? What is its projection? – xunilk Mar 2 '15 at 19:07
  • The vector shapfile has around 12,000 points. Projection is WGS84 UTM Zone 8N. – Charlie Parr Mar 3 '15 at 18:49

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.