Skip to content

Commit

Permalink
Clarify and adjust use of runAndLoadResults method (#9068)
Browse files Browse the repository at this point in the history
  • Loading branch information
DelazJ committed May 15, 2024
2 parents 209c430 + 54b34ab commit 28e0b3b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
25 changes: 11 additions & 14 deletions docs/training_manual/processing/log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,20 @@ This is an easy way of replicating the work we already did before.

For instance, try the following:

#. Open the data corresponding to the first chapter of this manual and run the algorithm explained there.
#. Now go to the log dialog and locate the last algorithm in the list, which corresponds to the algorithm you have just run.
#. Double-click on it and a new result should be produced, just like when you run it using the normal dialog and calling it from the toolbox.
#. Open the data corresponding to the first chapter of this manual
and run the algorithm explained there.
#. Now go to the History dialog and locate the last algorithm in the list,
which corresponds to the algorithm you have just run.
#. Double-click on it and a new result should be produced,
just like when you run it using the normal dialog and calling it from the toolbox.

:abbr:`★★ (Advanced level)` Advanced
-------------------------------------
:abbr:`★★ (Moderate level)` Follow Along
------------------------------------------

You can also modify the algorithm.

#. Copy the algorithm call
#. Open the :menuselection:`Plugins --> Python console`
#. Paste your copy to run the analysis; change the text at will.
#. To display the resulting file, type::

iface.addVectorLayer('/path/filename.shp', 'Layer name in legend', 'ogr')

Otherwise, you can use::

processing.runAndloadResults

#. Paste your copy to run the analysis; change the parameters at will.
#. To display the resulting file, you may want to replace ``processing.run`` in the pasted command
with ``processing.runAndLoadResults``.
32 changes: 20 additions & 12 deletions docs/user_manual/processing/console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,10 @@ appended to the given file path.

Unlike when an algorithm is executed from the toolbox, outputs are not
added to the map canvas if you execute that same algorithm from the
Python console using :meth:`run() <qgis.core.QgsProcessingAlgorithm.run>`,
but ``runAndLoadResults()`` will do that.

The :meth:`run() <qgis.core.QgsProcessingAlgorithm.run>` method returns
a dictionary with one or more output names (the
ones shown in the algorithm description) as keys and the file paths of
those outputs as values:
Python console using the :meth:`run() <qgis.core.QgsProcessingAlgorithm.run>` method.
That method returns a dictionary with one or more output names
(the ones shown in the algorithm description) as keys
and the file paths of those outputs as values:

.. code-block:: python
:linenos:
Expand All @@ -331,11 +328,22 @@ those outputs as values:
>>> myresult['OUTPUT']
/data/buffers.shp
You can load feature output by passing the corresponding file paths to
the ``load()`` method.
Or you could use ``runAndLoadResults()`` instead of
:meth:`run() <qgis.core.QgsProcessingAlgorithm.run>` to load
them immediately.
You can then load the output in the project as any common layer:

.. code-block:: python
:linenos:
>>> buffered_layer = myresult['OUTPUT']
>>> QgsProject.instance().addMapLayer(buffered_layer)
To immediately load the processing outputs in the project,
you can use the ``runAndLoadResults()`` method instead of ``run()``.

.. code-block:: python
:linenos:
>>> processing.runAndLoadResults("native:buffer", {parameters:values})
If you want to open an algorithm dialog from the console you can use the
``createAlgorithmDialog`` method. The only mandatory parameter is the algorithm
Expand Down

0 comments on commit 28e0b3b

Please sign in to comment.