mercredi 1 avril 2009

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.

>>>mc=iface.getMapCanvas()
>>> mycurrentlayer=mc.currentLayer()
>>> print mycurrentlayer.getLayerID()
lakes20090310215511168
>>> print mycurrentlayer.name()
lakes

Firts of all, put the canvas in the variable mc.
>>>mc=iface.getMapCanvas()
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

1 commentaire: