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()

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Mathys WOHL
  • 177
  • 8

2 Answers2

5

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)

J. Monticolo
  • 15,695
  • 1
  • 29
  • 64
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

MrXsquared
  • 34,292
  • 21
  • 67
  • 117