7

I am very new to GIS, QGIS and Python. I am using QGIS 2.0 and I would like to develop a QGIS plugin that would display the raster layers of the main window in order to allow for some specific interaction. I have a very hard time getting this to work! I generated the skeleton of my plugin through plugin builder, and changed the [moduleName]2dialog.py file to try to display the main window layers.

I have two questions :

  1. Is it possible to get the layers?
  2. Can I draw it inside a GqsMapCanvas inside my plugin GUI, or do I need another object?

This is the code I have for now, which generates an error :

from PyQt4 import QtCore, QtGui
from qgis import core, gui, utils
from qgis.gui import QgsMapCanvasLayer

mc = iface.mapCanvas()
layers = mc.mapRenderer().layerSet()
if not layers:
    self.ui.displayFileNameTextEdit.setText("No layer to display.")
else:
    for layer in layers: #layer is one id
        self.ui.qgsMapCanvas.setExtent(layer.extent())
        self.ui.qgsMapCanvas.setLayerSet( [ QgsMapCanvasLayer(layer) ] )
        self.ui.qgsMapCanvas.refresh()

2 Answers 2

4

I have solved my problem, thanks to Matt and to this link : http://osgeo-org.1560.x6.nabble.com/Display-raster-layer-on-own-Map-Canvas-td5002935.html

Here is how I do it, inside the [moduleName]2dialog.py file generated by the plugin builder plugin :

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QFile, QFileInfo
from qgis import core, gui, utils
from qgis.core import QgsRasterLayer
from qgis.gui import QgsMapCanvasLayer
from qgis.utils import iface

layers = iface.legendInterface().layers()
for each_layer in layers:
    fileName = each_layer
    fileInfo = QFileInfo(fileName)
    baseName = fileInfo.baseName()
    self.layer = QgsRasterLayer(fileName, baseName)

    QgsMapLayerRegistry.instance().addMapLayer(self.layer)
    self.canvas = self.ui.qgsMapCanvas
    self.canvas.setCanvasColor(QtCore.Qt.white)
    self.canvas.setExtent(self.layer.extent())
    self.canvas.setLayerSet( [ QgsMapCanvasLayer(self.layer) ] )
    r = QgsRubberBand(self.ui.qgsMapCanvas, True)
    self.canvas.setCurrentLayer(self.layer)
    self.canvas.setVisible(True)
    self.canvas.refresh()
2

You can get a list of the layers in the table of contents and perform actions like so:

legend = self.iface.legendInterface()
layers = legend.layers()
for each_layer in layers:
    if each_layer.type() == "RasterLayer":
        print each_layer.id()
        print each_layer.name()
4
  • Is there a way to get each layer's filename? (e.g. with absolute path) If yes, I can do what I want.
    – Rinita
    Commented Feb 17, 2014 at 16:27
  • try each_layer.source(), each_layer.name() will return the name of the layer as it is in the TOC.
    – Matt
    Commented Feb 17, 2014 at 16:42
  • Actually could be each_file.dataUrl(), have a look round the QGIS API yourself here qgis.org/api/2.0
    – Matt
    Commented Feb 17, 2014 at 16:46
  • Thank you very much!! I have my solution. Posting it right now!
    – Rinita
    Commented Feb 18, 2014 at 16:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.