[Qgis-user] How do I pass parameters to grass7 algorithms with Processing.run?

Stephen Bosch posting at vodacomm.ca
Mon Apr 9 08:03:01 PDT 2018


Hi everyone,

I am trying to grasp how parameters are to be passed to Processing
algorithms.

Thanks to Håvard Tveite's advice, I've understood that it is now done with
a Python dictionary.

I'm trying to use the grass7 algorithm v.to.rast. Processing.algorithmHelp
(thanks for the tip, Håvard) gives the following output:

----------------
> Input parameters
> ----------------
>
> input:  <QgsProcessingParameterVectorLayer>
>     Input vector layer
>
> type:  <QgsProcessingParameterEnum>
>     Input feature type
>         0 - point
>         1 - line
>         2 - boundary
>         3 - area
>
> where:  <QgsProcessingParameterString>
>     WHERE conditions of SQL statement without 'where' keyword
>
> use:  <QgsProcessingParameterEnum>
>     Source of raster values
>         0 - attr
>         1 - cat
>         2 - va
>         3 - z
>         4 - dir
>
> attribute_column:  <QgsProcessingParameterField>
>     Name of column for 'attr' parameter (data type must be numeric)
>
> rgb_column:  <QgsProcessingParameterField>
>     Name of color definition column (with RRR:GGG:BBB entries)
>
> label_column:  <QgsProcessingParameterField>
>     Name of column used as raster category labels
>
> value:  <QgsProcessingParameterNumber>
>     Raster value (for use=val)
>
> memory:  <QgsProcessingParameterNumber>
>     Maximum memory to be used (in MB)
>
> output:  <QgsProcessingParameterRasterDestination>
>     Rasterized
>
> GRASS_REGION_PARAMETER:  <QgsProcessingParameterExtent>
>     GRASS GIS 7 region extent
>
> GRASS_REGION_CELLSIZE_PARAMETER:  <QgsProcessingParameterNumber>
>     GRASS GIS 7 region cellsize (leave 0 for default)
>
> GRASS_RASTER_FORMAT_OPT:  <QgsProcessingParameterString>
>     Output Rasters format options (createopt)
>
> GRASS_RASTER_FORMAT_META:  <QgsProcessingParameterString>
>     Output Rasters format metadata options (metaopt)
>
> GRASS_SNAP_TOLERANCE_PARAMETER:  <QgsProcessingParameterNumber>
>     v.in.ogr snap tolerance (-1 = no snap)
>
> GRASS_MIN_AREA_PARAMETER:  <QgsProcessingParameterNumber>
>     v.in.ogr min area
>
> ----------------
> Outputs
> ----------------
>
> output:  <QgsProcessingOutputRasterLayer>
>     Rasterized
>

The input types are described with terms like
"<QgsProcessingParameterVectorLayer>"; what does this mean exactly? Do I
need to pass class instances? In the previous (2.x) iteration of my code, I
had statements like

file_grid_r = processing.run("grass7:v.to.rast.value", grid_v, 0, 1,
>      extent, 0.008333, -1, 0.000100, None)['output']
>

where 'grid_v' is a QgsVectorLayer object and 'extent' is a string that
looks like this:

'33.91415594546105,41.91384412832699,30.679722671256611,35.467319665842772'
>

I've tried calling the algorithm this way:

file_grid_r = processing.run("grass7:v.to.rast", {'INPUT': grid_v, 'type':
> 0, 'use': 1, 'GRASS_REGION_PARAMETER': extent}, feedback=None)
>

But it fails. On the console, I see

sys:1: ResourceWarning: unclosed <socket.socket fd=3,
> family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0,
> laddr=('127.0.0.1', 41358), raddr=('127.0.0.1', 37307)>
>
> Process finished with exit code 1
>

If I step through the code in PyCharm and trace the exception, I see
an AttributeError:
'NoneType' object has no attribute 'messageBar'.

I'm not sure if this is a bug in QGIS or whether there is something wrong
with my parameters.

Do you have any suggestions or ideas?

Thanks

Stephen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20180409/526004bb/attachment.html>


More information about the Qgis-user mailing list