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()

closed as too broad by PolyGeo Jun 26 at 7:22

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

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()
  • Is there a way to get each layer's filename? (e.g. with absolute path) If yes, I can do what I want. – Rinita Feb 17 '14 at 16:27
  • try each_layer.source(), each_layer.name() will return the name of the layer as it is in the TOC. – Matt Feb 17 '14 at 16:42
  • Actually could be each_file.dataUrl(), have a look round the QGIS API yourself here qgis.org/api/2.0 – Matt Feb 17 '14 at 16:46
  • Thank you very much!! I have my solution. Posting it right now! – Rinita Feb 18 '14 at 16:32

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