[Qgis-developer] select a loaded raster layer from qgis legend into python plugin
Matthias Kuhn
matthias.kuhn at gmx.ch
Thu Jan 17 08:24:43 PST 2013
Hi Otto,
Try this
class MyPluginDialog (QDialog,Ui_MyPluginDialog)::
def __init__(self):
self.setupUi(self)
self.initLayerCombobox( self.ui.InRastA, 'key_of_default_layer' )
def initLayerCombobox(self,combobox, default):
combobox.clear()
reg = QgsMapLayerRegistry.instance()
for ( key, layer ) in reg.mapLayers().iteritems():
if layer.type() == QgsMapLayer.RasterLayer and ( layer.usesProvider() and layer.providerKey() == 'gdal' ):
combobox.addItem( layer.name(), key )
idx = combobox.findData( default )
if idx != -1:
combobox.setCurrentIndex( idx )
Matthias
On 01/17/2013 05:07 PM, Otto Dassau wrote:
> Hi Matthias,
>
> thanks for your help. I guess it is similar to what I found?
>
> def getRasterLayersNames():
> layerList = []
> layerMap = QgsMapLayerRegistry.instance().mapLayers()
> for name, layer in layerMap.iteritems():
> if layer.type() == QgsMapLayer.RasterLayer and
> ( layer.usesProvider() and layer.providerKey() ==
> 'gdal' ): layerList.append( unicode( layer.name()))
> return layerList
>
> If I understand correctly your function selects all raster layers from
> QgsMapLayerRegistry. Now I would like to visualize the layer list in the
> combobox (InRastA) of the plugin dialog file. I found something that works
> for vector layers:
>
> def manageGui(self):
> myList = []
> self.ui.InRastA.clear()
> myList = initLayerCombobox()
> self.ui.InRastA.addItems( myList )
> return
>
> But is doesn't work for raster that way, if I use your function. How is it
> possible to make the raster layers selectable / show up in the plugin dialog?
>
> Regards
> Otto
>
> Am Thu, 17 Jan 2013 16:21:51 +0100
> schrieb Matthias Kuhn <matthias.kuhn at gmx.ch>:
>
>> Hi Otto,
>>
>> here a small extract from a plugin I wrote:
>>
>> Just check for the layer type to only list raster layers
>>
>> def initLayerCombobox(self,combobox, default):
>> reg = QgsMapLayerRegistry.instance()
>> for ( key, layer ) in reg.mapLayers().iteritems():
>> combobox.addItem( layer.name(), key )
>>
>> idx = combobox.findData( default )
>> if idx != -1:
>> combobox.setCurrentIndex( idx )
>>
>> Regards
>>
>> On Thu, 2013-01-17 at 15:54 +0100, Otto Dassau wrote:
>>> Hi,
>>>
>>> I would like to write a small python plugin to analyse some raster data,
>>> but I haven't managed to select a loaded layer yet. I searched the
>>> howtos and found several simple examples about:
>>>
>>> # get the currently active layer
>>> layer = self.iface.mapCanvas().currentLayer()
>>>
>>> but as I said I would like to select one of the loaded raster layers
>>> from the qgis legend using a QtGui.QComboBox in the plugin dialog. At
>>> the moment I tried the getRasterLayersNames() function I found in
>>> several other plugins but it seems I am missing something, because the
>>> raster layers are not selectable. Can someone point me to a simple
>>> example to understand the general procedure?
>>>
>>> Thanks a lot
>>> Otto
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
More information about the Qgis-developer
mailing list