Getting file paths of loaded layers in QGIS plugin?
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()
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()