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

Nyall Dawson nyall.dawson at gmail.com
Tue Apr 10 17:03:30 PDT 2018


On 10 April 2018 at 20:25, Stephen Bosch <posting at vodacomm.ca> wrote:
> Hi Nyall:
>
> On Tue, Apr 10, 2018 at 2:20 AM, Nyall Dawson <nyall.dawson at gmail.com>
> wrote:
>>
>> On 10 April 2018 at 01:03, Stephen Bosch <posting at vodacomm.ca> wrote:
>>
>> > 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)
>> >
>>
>> Looks correct
>>
>> > 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)>
>>
>> That can be ignored - it's unrelated.
>
>
> (Messages like this occur only when the process exits with a code other than
> 0.)
>
> I should add that, without using the debugger, I wouldn't be able to see the
> error at all. Is that intended behaviour? Is there any way to turn it on?
> The absence of exceptions and tracebacks make things fail silently, which
> can have nasty consequences.
>
>>
>> >
>> > If I step through the code in PyCharm and trace the exception, I see an
>> > AttributeError: 'NoneType' object has no attribute 'messageBar'.
>>
>> Where is this thrown? I suspect the problem is because you're passing
>> "feedback=None" and somewhere there's code which is expecting a valid
>> feedback object. Can you let me know which line the exception is
>> thrown on and I'll fix?
>
>
> It is thrown in line 42 of MessageBarProgress.py:
> __init__ [MessageBarProgress.py:42]  id:94435226437000

Thanks, fixed in
https://github.com/qgis/QGIS/commit/d5573a5a7bf5a32490b50f528da10c6f58871eab

>
> After correcting the statement to the following:
>
>> file_grid_r = QgsRasterLayer()
>> processing.run("grass7:v.to.rast", {'input': grid_v, 'type': 0, 'use': 1,
>>                                     'GRASS_REGION_PARAMETER': extent,
>>                                     'output': file_grid_r,
>>                                     'GRASS_REGION_CELLSIZE_PARAMETER':
>> 0.008333,
>>                                     'GRASS_SNAP_TOLERANCE_PARAMETER': -1,
>>                                     'GRASS_MIN_AREA_PARAMETER': 0.000100},
>>                              feedback=QgsProcessingFeedback())
>
>
>  I get the message
>
>> Unable to execute algorithm
>> Incorrect parameter value for output

The OUTPUT parameter should be set to the path to save the result,
e.g. 'd:\blahblah.tif":

 processing.run("grass7:v.to.rast", {'input': grid_v, 'type': 0, 'use': 1,
                                     'GRASS_REGION_PARAMETER': extent,
                                     'output': 'd:\blah.tif',
                                     'GRASS_REGION_CELLSIZE_PARAMETER':
 0.008333,
                                     'GRASS_SNAP_TOLERANCE_PARAMETER': -1,
                                     'GRASS_MIN_AREA_PARAMETER': 0.000100},
                              feedback=QgsProcessingFeedback())

processing.run will return a dict a results, and inside that you'll
automatically have a QgsRasterLayer loaded corresponding to this
output.

E.g.

params = { ....., 'OUTPUT': 'd:\blah.tif' }
results = processing.run('grass7:v.to.rast', params, feedback )
my_raster_layer = results['OUTPUT']
assert my_raster_layer.isValid()

Here my_raster_layer will be a QgsRasterLayer, ready for further
processing or adding to the project for visualisation.

I'm pondering whether we should automatically set output locations to
temporary files if they aren't specified. E.g. if you don't set OUTPUT
to 'd:\blah.tif', it'll fill it in automatically with a .tif file in
your temp folder...

Nyall

> I've tried a couple of variations (specifying 'output' as an index and
> assigning it to a variable while setting 'output': None in the dictionary,
> for example) and get the same error.
>
> In my 2.x code, I just used the index and assigned the return to a variable.
> The implementation of v.to.rast for QGIS 3 Processing seems to require a
> value for output, and simply putting an variable doesn't work either (it
> complains that the variable is undefined).
>
> With a few more hints, I'm sure I'll get the hang of this ;-)
>
> Thanks
>
> Stephen



More information about the Qgis-user mailing list