.

La ligne de commande QGIS

Les outils de traitement intègrent un outil très pratique qui vous permet de lancer des algorithmes sans avoir à ouvrir la boite à outils. Il suffit juste de saisir le nom de l’algorithme que vous voulez exécuter.

Il s’agit de l’outil Ligne de commande qui se matérialise par une simple zone de texte à complètement automatique où saisir le nom de la commande à lancer.

../../../_images/commander1.png

The QGIS Commander win

The Commander is started from the Analysis menu or, more practically, by pressing Shift + Ctrl + M (you can change that default keyboard shortcut in the QGIS configuration if you prefer a different one). Apart from executing Processing algorithms, the Commander gives you access to most of the functionality in QGIS, which means that it gives you a practical and efficient way of running QGIS tasks and allows you to control QGIS with reduced usage of buttons and menus.

Moreover, the Commander is configurable, so you can add your custom commands and have them just a few keystrokes away, making it a powerful tool to help you become more productive in your daily work with QGIS.

Commandes disponibles

Les commandes disponibles sont classées en différentes catégories :

  • Algorithmes. Ils apparaissent sous la forme Processing algorithm: <nom de l'algorithme>.

  • Menu items. These are shown as Menu item: <menu entry text>. All menus items available from the QGIS interface are available, even if they are included in a submenu.
  • Fonctions Python. Vous pouvez créer de courtes fonctions Python qui feront ensuite partie de la liste des commandes disponibles. Elles se présentent sous la forme Function: <nom de la fonction>.

Pour lancer une des commandes ci-dessus, commencez à taper puis sélectionnez la commande depuis la liste qui apparaît alors, filtrée dynamiquement par le texte que vous tapez.

In the case of calling a Python function, you can select the entry in the list, which is prefixed by Function: (for instance, Function: removeall), or just directly type the function name (``removeall in the previous example). There is no need to add brackets after the function name.

Créer des fonctions personnalisées

Custom functions are added by entering the corresponding Python code in the commands.py file that is found in the .qgis/sextante/commander directory in your user folder. It is just a simple Python file where you can add the functions that you need.

The file is created with a few example functions the first time you open the Commander. If you haven’t launched the Commander yet, you can create the file yourself. To edit the commands file, use your favorite text editor. You can also use a built-in editor by calling the edit command from the Commander. It will open the editor with the commands file, and you can edit it directly and then save your changes.

Par exemple, vous pouvez ajouter la fonction suivante, qui supprime toutes les couches :

from qgis.gui import *

def removeall():
    mapreg = QgsMapLayerRegistry.instance()
    mapreg.removeAllMapLayers()

Une fois la fonction ajoutée, elle sera disponible depuis la Ligne de commandes et vous pourrez l’appeler en tapant removeall. Il n’y a rien d’autre à faire à part écrire la fonction elle-même.

Les fonctions peuvent recevoir des paramètres. Ajoutez``*args`` à la définition de votre fonction pour accepter des paramètres. Lors de l’appel à cette fonction depuis la Ligne de commande, les paramètres doivent être passés en les séparant pas des espaces.

Voici un exemple de fonction qui charge une couche et prend comme paramètre le nom de la couche à charger.

import processing

def load(*args):
  processing.load(args[0])

If you want to load the layer in /home/myuser/points.shp, type load /home/myuser/points.shp in the Commander text box.