[Qgis-developer] Command to avoid one layer replacing last layer

Aaron Racicot aaronr at z-pulley.com
Wed Nov 25 19:54:40 EST 2009


Angelica,

Try replacing:

layers = [cl]
self.canvas.setLayerSet(layers)

With:

layers = self.canvas.mapRenderer().layerSet()
layers.insert(0,cl)
self.canvas.setLayerSet(layers)

This way you keep the current layers already loaded and then add a new
layer.  This only addresses getting them both loaded, but would require that
the raster is loaded first and the vector second to get the affect you are
after.  If you want to re-order the layers you need to re-order the
layerset.

Hope that helps...

A

--------------------------
Aaron Racicot
Z-Pulley Inc.

-----Original Message-----
From: qgis-developer-bounces at lists.osgeo.org
[mailto:qgis-developer-bounces at lists.osgeo.org] On Behalf Of Echavarria
Gregory, Maria Angelica
Sent: Wednesday, November 25, 2009 4:01 PM
To: qgis-developer at lists.osgeo.org
Subject: [Qgis-developer] Command to avoid one layer replacing last layer

Developers Group,

I have developed my PyQGIS application using OSGeo4W 1.0 Kore python 2.5
bindings for win32.  I have a method that loads vector layers (ogr's
shapefiles) and another method that loads rasters (png's with pgw's) to my
canvas. Both work nice but loading either layer replaces the other and
biceversa. The idea is that the raster stays below the shapefile as if the
raster was the background... like it happens in the Stand Alone QGis when
loading multiple vectors and rasters using the buttons... My question is: is
there a command line that I am missing? or could the problem be my canvas
background color? below are my two methods in their simplest way:


def MyRasterLoad(self, file, fileName):
        """Load any raster layer specifying its file string"""
        layer = QgsRasterLayer(file, fileName)
        if not layer.isValid():
            return
        QgsMapLayerRegistry.instance().addMapLayer(layer)
        self.canvas.setExtent(layer.extent())
        cl = QgsMapCanvasLayer(layer)
        layers = [cl]
        self.canvas.setLayerSet(layers)


def MyWorldLoad(self, file, fileName, ext=bool):
        """add the base map layer and zoom to its extent or add graticule
for the delimited area"""
        layer = QgsVectorLayer(file, fileName, "ogr")
        if not layer.isValid():
            return
        symbols = layer.renderer().symbols()
        symbol = symbols[0]
        symbol.setFillColor(QColor.fromRgb(0,100,0))
        QgsMapLayerRegistry.instance().addMapLayer(layer);
        if ext == True:
            self.canvas.setExtent(layer.extent())
        if ext == False:
            rect = QgsRectangle(float(self.x_min), float(self.y_min),
float(self.x_max), float(self.y_max))
            self.canvas.setExtent(rect)
        cl = QgsMapCanvasLayer(layer)
        layers = [cl]
        self.canvas.setLayerSet(layers)

Thanks so much!,
Angelica.

_______________________________________________
Qgis-developer mailing list
Qgis-developer at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/qgis-developer



More information about the Qgis-developer mailing list