4

I built my own map canvas inside a plugin in QGIS and I want to add two layers (one by one) in order to overlay the two. I have this code:

fileCI = "....\plugins\teste\test.shp"
fileInfo = QFileInfo(fileCI)
layer = QgsVectorLayer(fileCI, fileInfo.baseName(), "ogr")

if not layer.isValid():
    return

if self.canvas.layerCount()==0:

    # add layer to the registry
    QgsMapLayerRegistry.instance().addMapLayer(layer, False)

    # set extent to the extent of our layer
    self.canvas.setExtent(layer.extent())

    # set the map canvas layer set
    self.canvas.setLayerSet([QgsMapCanvasLayer(layer)])
    self.canvas.setVisible(True)    

else:
    layers = self.canvas.layers()
    layers.insert(1,layer)

    # add layer to the registry
    QgsMapLayerRegistry.instance().addMapLayers(layers,False) 

    for i in range(0,self.canvas.layerCount()+1):
        self.canvas.setLayerSet([QgsMapCanvasLayer(layers[i])])
        self.canvas.setVisible(True)

    for lay in layers:
        lay.triggerRepaint()   

But when I add the first vector file, ok, when I add the second the map canvas stay empty.

How do I do to overlay the two layers?

2

I find a way of overlay vector files (if there is a vector file already open) and a raster file.

orto = '...image.tif'
fileCV = '.../layer1.shp'
layers =  []

# display file in canvas
fileInfo = QFileInfo(fileCV)
layer = QgsVectorLayer(fileCV, fileInfo.baseName(), "ogr")

# raster layer
rfileInfo = QFileInfo(orto)
rlayer = QgsRasterLayer(orto, rfileInfo.baseName())          

if self.canvas.layerCount()==0:

    QgsMapLayerRegistry.instance().addMapLayer(rlayer)
    self.canvas.setExtent(rlayer.extent())   

    # list of layers append raster
    cl_raster= QgsMapCanvasLayer(rlayer)
    layers.append(cl_raster)        

    # add vector layer
    if not layer.isValid():
        return

    QgsMapLayerRegistry.instance().addMapLayer(layer)     
    v_layer = QgsMapCanvasLayer(layer)
    layers.insert(0, v_layer) 

    # show layers
    self.canvas.setLayerSet(layers)            

else:
    existent_layers = self.canvas.layers()
    for i in existent_layers:
        if i.type() == QgsMapLayer.VectorLayer:
            l = QgsMapCanvasLayer(i)
    layers.insert(0, l)


    QgsMapLayerRegistry.instance().addMapLayer(rlayer)
    self.canvas.setExtent(rlayer.extent())   

    # list of layers append raster
    cl_raster= QgsMapCanvasLayer(rlayer)
    layers.insert(2,cl_raster)             

    # add layer to the registry
    QgsMapLayerRegistry.instance().addMapLayer(layer) 

    v_layer = QgsMapCanvasLayer(layer)
    layers.insert(1, v_layer) 

    # show layers
    self.canvas.setLayerSet(layers) 

Thanks for the help!

1

I don't know if I understand, but... I don't know the reason you try to control canvas layerSet if your goal seems only to show these layers.

I would try simplifing removin if...else and leaning only:

QgsMapLayerRegistry.instance().addMapLayer(layer)

observe that I removed the False parameter so the same registry emit event that allow canvas to automatically update.

you can also add the following row:

self.canvas.setExtent(layer.extent())

to set the extent to the loaded layer as you did in the if statement

  • Thanks for the answer. The if statement is used because I can have or not a layer already open in canvas. If I remove if statement I assume that I have always a certain number of layers. I don't know if I'm confusing. – Bárbara Duarte Jun 23 '15 at 14:12
  • "I can have or not A layer already open in canvas": A layer or a the just loaded layer? – Luigi Pirelli Jun 23 '15 at 14:52
  • I think this is a little confuse to understand but I try to explain: I have a button to open layers. What I want is open one layer (first) to map canvas, and then with the same button, open another layer (second) in order to overlay the first one. My error is that I open the first and when add the second one all disappear. – Bárbara Duarte Jun 23 '15 at 14:57
  • I don't know if that helps but the second layer is added to qgis canvas, so the two layers appears in qgis canvas. In my own canvas only the last one appears. The problem is that I want to keep the first one and add the second to overlay. – Bárbara Duarte Jun 23 '15 at 15:53
  • you have to play with transparencies or styling of each layer (or combine them). The layer are stacked following the registry order. – Luigi Pirelli Jun 24 '15 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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