[Qgis-developer] Basic question: How to select a layer in the GUI and get the handle in the Python code?

Ole Nielsen ole.moller.nielsen at gmail.com
Wed Dec 7 21:54:48 EST 2011


Thank you for the reply - it is useful and I am getting closer. However, it
is not that I want to specify the name, rather I'd like to be able to
browser the active layers (or maybe even layer files) and select two layers
for analysis. So I am maybe looking for a file or layer selection combo and
a hint on how to make it work.

I just need to pick two existing layers from the UI and get handles to
them. After the processing, I need to specify a filename to store the
result to and will also need help with  that.
I have very limited experience with UIs so please forgive me my basic
questions.

Cheers and thanks
Ole

On Wed, Dec 7, 2011 at 8:57 PM, Alexander Bruy <alexander.bruy at gmail.com>wrote:

> Hi Ole,
>
> 2011/12/7 Ole Nielsen <ole.moller.nielsen at gmail.com>:
> > Sorry, I was perhaps not as clear as I could have been. I have already
> > written plugins using the currentLayer option. What I need now is to
> combine
> > two layers for a specific analysis. So I imagine being able to select
> them
> > through the plugin GUI selection combos and return the two layer handles
> to
> > the Python code in the same format as what is provided by current layer.
> > However, I have no idea how to start doing this.
>
> Ah, understand. That's not so difficult. You need to get layer name
> from combobox,
> then find layer by name. Here is small function that did this work for you
>
> def getVectorLayerByName( layerName ):
>  layerMap = QgsMapLayerRegistry.instance().mapLayers()
>  for name, layer in layerMap.iteritems():
>    if layer.type() == QgsMapLayer.VectorLayer and layer.name() ==
> layerName:
>      if layer.isValid():
>        return layer
>      else:
>        return None
>
> So in your code you can simply do something like this
>
> layerA = getVectorLayerByName( self.cmbLayerA.currentText() )
> layerB = getVectorLayerByName( self.cmbLayerB.currentText() )
>
> Hope this helps
>
> --
> Alexander Bruy
> NextGIS
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20111208/08bf517f/attachment.html


More information about the Qgis-developer mailing list