2

I am working on creating a plugin on QGIS 3.10. This plugin loads two layers in my QGIS project. I would like to zoom in the layer extent of AEE_layer. Unfortunately despite the code that specifies to zoom the canvas on the AEE_layer extent, it does not work. The canvas systematically zooms in on the extent of all the layers that are loaded in my project.

project = QgsProject.instance()
canvas = iface.mapCanvas()
                
project.write('C:/CartoIni/Projet/Projet.qgs')

groupe_aire = root.addGroup("Aires d'étude")

AEE_layer = QgsVectorLayer("C:/Users/Thysma/Documents/Stage/Data/AEE.shp", 'AEE','ogr')
QgsProject.instance().addMapLayer(AEE_layer, False)
groupe_aire.addLayer(AEE_layer)

reg_layer = QgsVectorLayer("C:/Users/Thysma/Documents/Stage/Data/REGION.shp", 'Limites régionales','ogr')
QgsProject.instance().addMapLayer(reg_layer, False)
groupe_aire.addLayer(reg_layer)

extent = AEE_layer.extent()
canvas.setExtent(extent)
canvas.refresh()
11
  • 2
    Try by replacing : canvas = iface.mapCanvas(), and welcome to GIS SE ! – J. Monticolo Mar 29 at 13:01
  • With iface.mapCanvas() the problem is the same – Mathys WOHL Mar 30 at 7:26
  • 1
    What is the result of AEE_layer.extent() ? – J. Monticolo Mar 30 at 8:21
  • 2
    With a canvas CRS configured to Lambert 93 (EPSG: 2154), I tried in the QGIS Python console iface.mapCanvas().setExtent(QgsRectangle(610783.06441810820251703, 6745293.08760993927717209, 623023.31778933946043253, 6757083.76099352352321148)) and it zooms perfectly to the Orléans - Olivet area. Check your canvas coordinate system. – J. Monticolo Apr 1 at 9:20
  • 1
    is the layer loaded in the QGIS main canvas with your plugin ? – J. Monticolo Apr 1 at 13:21
4

You have to load layers and before set the new extent, tell to your canvas to wait before rendering. An important thing too is to set your project or canvas CRS. In the code, I left the two but only one work.

Try the code below :

project = QgsProject.instance()
canvas = iface.mapCanvas()

# modifications ##
crs_l93 = QgsCoordinateReferenceSystem("EPSG:2154")
project.setCrs(crs_l93)
canvas.setDestinationCrs(crs_l93)
# ################

project.write('C:/CartoIni/Projet/Projet.qgs')

root = QgsProject.instance().layerTreeRoot()  # add the root object, as your original code
groupe_aire = root.addGroup("Aires d'étude")

AEE_layer = QgsVectorLayer("C:/Users/Thysma/Documents/Stage/Data/AEE.shp", 'AEE','ogr')
QgsProject.instance().addMapLayer(AEE_layer, False)
groupe_aire.addLayer(AEE_layer)

reg_layer = QgsVectorLayer("C:/Users/Thysma/Documents/Stage/Data/REGION.shp", 'Limites régionales','ogr')
QgsProject.instance().addMapLayer(reg_layer, False)
groupe_aire.addLayer(reg_layer)

canvas.waitWhileRendering()  # modification here
extent = AEE_layer.extent()
canvas.setExtent(extent)
canvas.refresh()
EDIT: if the first code doesn't work

You can set the extent once the canvas is refreshed a first time :

def set_aee_extent():
    extent = AEE_layer.extent()
    canvas.setExtent(extent)
    # disconnect the signal to avoid calling set_aee_extent again
    canvas.mapCanvasRefreshed.disconnect(set_aee_extent)
    canvas.refresh()

project = QgsProject.instance()
canvas = iface.mapCanvas()

# modifications ##
crs_l93 = QgsCoordinateReferenceSystem("EPSG:2154")
project.setCrs(crs_l93)
canvas.setDestinationCrs(crs_l93)
# ################

project.write("C:/CartoIni/Projet/Projet.qgs")

root = QgsProject.instance().layerTreeRoot()  # add the root object, as your original code
groupe_aire = root.addGroup("Aires d'étude")

AEE_layer = QgsVectorLayer("C:/Users/Thysma/Documents/Stage/Data/AEE.shp", 'AEE','ogr')
reg_layer = QgsVectorLayer("C:/Users/Thysma/Documents/Stage/Data/REGION.shp", 'Limites régionales','ogr')

canvas.mapCanvasRefreshed.connect(set_aee_extent)

canvas.setLayers([AEE_layer, reg_layer])
groupe_aire.addLayer(AEE_layer)
groupe_aire.addLayer(reg_layer)
4

Try replacing QgsMapCanvas() with iface.mapCanvas()

AEE_layer = QgsVectorLayer("C:/Users/Thysma/Documents/Stage/Data/AEE.shp", 'AEE','ogr')
QgsProject.instance().addMapLayer(AEE_layer, False)

canvas = iface.mapCanvas()
extent = AEE_layer.extent()
canvas.setExtent(extent)
canvas.refresh()

Check out https://gis.stackexchange.com/a/363373/107424

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.