[Qgis-developer] Display raster layer on own Map Canvas

Daniel Klich daniel.klich at gmx.de
Wed Sep 19 05:36:53 PDT 2012


Hello,

I am new to python and the QGIS API and writing a plugin in python to 
simulate/model insect dispersal.

I am using .ui files and Qt Designer. And i placed a QWidget on the form 
and promote it to a new class: set QgsMapCanvas as class name and set 
qgis.gui as header file.

I load a raster layer, set canvas color and so on. But the layer only 
displays if i use QgsMapLayerRegistry.instance().addMapLayer(self.layer).
Without these code line my plugin only displays the rubber band, but not 
the raster layer. But if i use ...addMapLayer() it also loads the layer 
in QGIS. I want to have a behaviour like in the GDAL - Georeferenzierung 
plugin. There you could add a raster layer and the plugin displays it 
only in the plugin window, not in the QGIS MainWindow.

Here is the method code:

def addRasterLayer(self):
         fileName = "NE2_50M_SR.tif"
         fileInfo = QFileInfo(fileName)
         baseName = fileInfo.baseName()
         self.layer = QgsRasterLayer(fileName, baseName)
         if not self.layer.isValid():
           #error

         #only with this line of code the raster layer will be displayed:
         QgsMapLayerRegistry.instance().addMapLayer(self.layer)

         self.canvas = self.dlg.ui.mapCanvasWidget

         self.canvas.setCanvasColor(Qt.white)
         self.canvas.setExtent(self.layer.extent())
         self.canvas.setLayerSet( [ QgsMapCanvasLayer(self.layer) ] )

         r = QgsRubberBand(self.dlg.ui.mapCanvasWidget, True)
         points = [ [ QgsPoint(-10,-10), QgsPoint(0,10), 
QgsPoint(10,-10) ] ]
         r.setToGeometry(QgsGeometry.fromPolygon(points), None)

         self.canvas.setCurrentLayer (self.layer)
         self.canvas.setVisible(True);
         self.canvas.refresh()




best regards,

Daniel


More information about the Qgis-developer mailing list