2

I am trying to write a plugin that will show an image based on its footprint in the vector layer. Currently, I have a problem with QgsMapCanvas. Layer in QgsMapCanvas disappear when I pan/zoom canvas. Here's my code:

myPlugin.py

def startOblique(self):
        self.dlgSec.canvas_dlg.show()
                
        self.dlgSec.canvas_dlg.enableAntiAliasing(True)
        
        newRaster = QgsRasterLayer(r'D:\basic\FRONT\B002_0891.tif','Raster1')
        print(newRaster)
        print(self.dlgSec.canvas_dlg)

        self.dlgSec.canvas_dlg.setLayers([newRaster])
        self.dlgSec.canvas_dlg.setExtent(newRaster.extent())
        
        self.dlgSec.canvas_dlg.refresh()
        self.dlgSec.canvas_dlg.waitWhileRendering()

def run(self):
        if self.first_start == True:
            self.first_start = False
            self.dlg = test1Dialog()
            self.dlgSec = test2Dialog()
            
        project = QgsProject.instance()
        # show the dialogs
        self.dlg.show()
        self.dlgSec.show()
        
        self.dlg.oblique_starter.clicked.connect(self.startOblique)

When I click oblique_starter button I get this: Oblique Image starter So, everything is fine by now. Raster is on the canvas. When I try to pan/zoom on the canvas raster disappears so I get this: enter image description here

Extra info: When I execute simple code to show canvas and raster layer inside python console on QGIS I can pan and zoom layer and it doesn't disappear:

canvas = QgsMapCanvas()
canvas.enableAntiAliasing(True)

newRaster = QgsRasterLayer(r'D:\basic\FRONT\B002_0891.tif','Raster1')

canvas.setExtent(newRaster.extent())
canvas.setLayers([newRaster])

canvas.resize(QSize(1366, 1064))
canvas.show()

What can I do to prevent the layer from disappearing from the canvas in my plugin?

3
  • 2
    I think mapCanvas is not refreshed, when you zoom or pan. Try to connect extentsChanged signal with a slot, which calls the refresh method.
    – Detlev
    Oct 21, 2019 at 3:36
  • I created such method like this def refresh_canvas(self): self.dlgSec.canvas_dlg.refresh() and signal: self.dlgSec.canvas.extentsChanged.connect(self.refresh_canvas)
    – James Ch
    Oct 21, 2019 at 11:56
  • Unfortunately, it doesn't help. @Detlev
    – James Ch
    Oct 21, 2019 at 12:02

1 Answer 1

0

Seems like QgsMapCanvas layers are cleared every time the extents are changed. I don't know whether it is the desired behaviour. So, I have to reinitiate QgsMapCanvas with layers every time extents change. Like this:

def refreshOblique(self):
        print("extents changed")
        self.dlgSec.canvas_dlg.setLayers(self.layers)
        self.dlgSec.canvas_dlg.refresh()
        self.dlgSec.canvas_dlg.waitWhileRendering()
        print("extent center: {},{}".format(self.dlgSec.canvas_dlg.center().x(), self.dlgSec.canvas_dlg.center().y()))
        print("layers: {}".format(self.dlgSec.canvas_dlg.layers()))

def run(self):
        #previous code
        self.dlgSec.canvas_dlg.extentsChanged.connect(self.refreshOblique)

Without setting setLayers in refreshOblique function I get

layers: []

instead of raster:

 layers: [<qgis._core.QgsRasterLayer object at 0x00000234434853A8>]

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.