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

Tim Sutton lists at linfiniti.com
Wed Sep 19 07:50:37 PDT 2012


Hi

On Wed, Sep 19, 2012 at 2:36 PM, Daniel Klich <daniel.klich at gmx.de> wrote:
> 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.
>

Try passing False to QgsMapLayerRegistry.instance().addMapLayer(<layer>, [bool])

The bool options should suppress it being added to the main canvas.

Then manually add it to the map canvas by wrapping it as a
mapcanvaslayer and explicitly passing a set of mapcanvaslayers using
QgsMapCanvas.setLayerSet()

Regards

Tim

PS QgsMapLayerRegistry.instance().addMapLayer(<layer>, [bool]) is
deprecated, use addMapLayers(<list>, bool) rather.


> 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
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer



-- 
Tim Sutton - QGIS Project Steering Committee Member (Release  Manager)
==============================================
Please do not email me off-list with technical
support questions. Using the lists will gain
more exposure for your issues and the knowledge
surrounding your issue will be shared with all.

Visit http://linfiniti.com to find out about:
 * QGIS programming and support services
 * Mapserver and PostGIS based hosting plans
 * FOSS Consulting Services
Skype: timlinux
Irc: timlinux on #qgis at freenode.net
==============================================


More information about the Qgis-developer mailing list