first QGIS routine: get some layers

Get the current layer

Often, you’ll have to perform operations on the active layer.
Before performing these operations, you first have to identify it and get it.
Once you have it, you can perform all the operations you’ll want.
In the following lines, i’ve included print statements so as to check the content of my variables.

>>> mycurrentlayer=mc.currentLayer()
>>> print mycurrentlayer.getLayerID()
>>> print

Firts of all, put the canvas in the variable mc.
Once you have your canvas, get its current layer called mycurrentlayer.
>>> mycurrentlayer=mc.currentLayer()

You can see some methods have been applied to the layer:

  • the method getLayerID()
  • the method name()

The layer name is only useful when displayed.
The layer id is often used whe performing operations on the layer.

Get a layer according according to its position in the legend

>>> mc=iface.getMapCanvas()
>>> myfirstlayer=mc.getZpos(1)

The method getZpos allows you to get a layer according to its position in the legend.

This kind of routine is often used when you need to get layers with their indexes

Une réflexion sur « first QGIS routine: get some layers »

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.