4

I want to convert a point shapefile to a raster with the same extent and resolution as an input raster using the algorithm saga:shapestogrid. When I run this code:

##Sampled_trees=vector
##Input_field= field Sampled_trees
##Elevation_model=raster

from qgis.core import *
from PyQt4.QtCore import *

inputTrees= processing.getObject(Sampled_trees)
inputField = inputTrees.fieldNameIndex(Input_field)
dem = processing.getObject(Elevation_model)

result = processing.runalg('saga:shapestogrid', inputTrees, Input_field, 0, 0, 4, dem.extent(),  dem.rasterUnitsPerPixelX(), path)

I get the error: Error: Wrong parameter value: qgis._core.QgsRectangle object at 0x0000000019F287B8

If I add or remove any parameter, the error will be Error: Wrong number of parameters.

The documentation gives the following syntax: processing.runalg('saga:shapestogrid', input, field, multiple, line_type, grid_type, output_extent, user_size, user_grid)

How should I define the output_extent parameter to make the algorithm to run?

1
  • Interesting, don't have this tool available in QGIS 2.16.1 but anyway, try defining the extent instead like in an example I posted here which defines the xmin, xmax... etc and then calling this in your parameter as "%f,%f,%f,%f"% (xmin, xmax, ymin, ymax).
    – Joseph
    Commented Oct 7, 2016 at 12:13

1 Answer 1

4

I agree with you, Processing could just accept a QgsRectangle and make our lives easier.

However, Processing needs the extent in this way (see http://docs.qgis.org/2.2/en/docs/training_manual/processing/extents.html):

"Xmin, Xmax, Ymin, Ymax"

So, you can just pass that argument as:

extent = "%s,%s,%s,%s" % ( dem.extent.xMinimum(), dem.extent.xMaximum(), dem.extent.yMinimum(), dem.extent.yMaximum() )

result = processing.runalg('saga:shapestogrid', inputTrees, Input_field, 0, 0, 4, extent,  dem.rasterUnitsPerPixelX(), path)
2
  • Do you mind if I edit the title to "How to pass an extent parameter to a Processing algorithm in PyQGIS"? I think that way it would be more useful to other users, while still reflecting you're original intention. Commented Oct 7, 2016 at 13:32
  • 1
    It would be a good idea. Commented Oct 7, 2016 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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