Use Script Runner with QGis

The QGis Python ScriptRunner plugin provides a go-between tool for the QGis automatization process, between the Python Interactive Console and the complicated plugins developments. Provides a script management dialog box that you can easily load, create, edit and run scripts to automate QGis on large scale.

Firstly, you have to install and activate the plugin from the manager extensions.


Therefore, you will find the Script Runner launch menu under the   extensions menu

Before starting , you must configure the text editor  for editing your scripts.

  • Open   Script Runner ,
  • click the Preferences icon (the wheel toothed )
  • In  General Options check the box   Edit Scripts Using …
  • Click on the button      and point to the text editing program selected 

You can use the Windows Notepad that you find in C: /Windows/System32/notepad.exe, but you can also use more powerful tools such as Notepad ++ ( free and available on the site  https://notepad-plus-plus.org/download/v6.9.2.html
If you follow the default installation you will enter C: / Program Files (x86) / Notepad ++ / notepad ++. exe in the Script Runner window .

Need to know

The entry point of any script in   Script Runner   is the function:

def run_script ( iface ):

run_script   with at least one argument, the object   iface . You have no  obligation to use this object in your code but, inevitably, it has to be passed as first or only argument.

Anyway, when you click on the icon New Script, you will find this function already declared.

This structure allows you to use directly the QGisInterface class without explicit declaration. But without any other statement, you are limited to the methods contained in this class.

However, you can add imports of any module and use them in your code. Creating a new script with Script Runner   already has four default imports:

  • from PyQt4.QtCore import *
  • from PyQt4.QtGui import *
  • from core import *
  • from gui import *

that you can leave or remove to your convenience , and also complete with any other Python module needed .

How does it work ?

The operation  Script Runner  is very simple. You have the toolbar:

For creating a new file. py, add an existing Python file, execute the selected script in the list of script files , reload a file modified , etc.

And a contextual menu (right click on a script from the list on the left) that you allows mainly to open the Python file in your text editing program and create or modify it.

Once saved the contents of the text editor, do not forget to reload the script for   script runner   take into consideration the changes you have just made.

When you run a script with script runner, all the print commands are directed to the script runner output console:

Error messages are also directed towards this window .

Finally, the setting of your scripts

One last thing to know about how your    script runner  operation is how to pass arguments to your script.

The first method is to return these arguments in the launching command of the script:

def run_script ( iface , data_path , buffer_size ):

In this example, you look for two arguments, a file path, and a buffer value.

At the time of script execution, a window will ask you to enter the parameters:

Another way of passing the arguments is to create a parameter string

def run_script ( iface , ** myargs ):

In this case, you will have to enter the different arguments as name/value pairs

In your code you access the different arguments as :

def run_script ( iface , ** myargs ):

data_path = myargs [ ‘ data_path ‘]

my_buffer_size = myargs [ ‘ buffer_size ‘]

 

Leave a Reply

Your email address will not be published. Required fields are marked *