1

I'm trying to download the location plan for each point on my network. This will allow me to fill in a data sheet with a GIS photo with the manhole in the center.

I've managed to find out how to download the current QGIS canvas and adjust the zoom level.

My problem is that the capture is made before QGIS moves my point. I did some tests by putting my canvas in an unknown location before launching my python script. The resulting image is the image of the location i chose at random before qgis centers the canvas on my point.

The consequence is that when I switch to prod the canvas capture is not just because it's on the previous point.

I hope i've made myself clear.

Here's a test file: https://www.grosfichiers.com/4DEs3Rdment

from qgis.core import *
from PyQt5.QtCore import QDateTime, QTimer
import os
from qgis.utils import iface

# Récupérer l'instance du projet
project = QgsProject.instance()
regards = project.mapLayersByName("fiche_regard fiche_reg")[0]

# Créer un répertoire si nécessaire
output_dir = "R:/Commun/Eliezer/developpement en cours/fiche_SAUR/"
if not os.path.exists(output_dir):
    os.makedirs(output_dir)
   
# Récupérer les entités (features) de la couche et les convertir en liste

def capture_plan(nom_image):
       
        point_geom = QgsGeometry.fromPointXY(QgsPointXY(495521.80476182291749865, 6913526.50558853521943092))
        # Récupérer les coordonnées du point
        point = point_geom.asPoint()
        iface.mapCanvas().saveAsImage(os.path.join(output_dir, f"{nom_image}.jpg"))
        buffer_size = 21
        extent = iface.mapCanvas().extent()
        extent.setXMinimum(point.x() - buffer_size)
        extent.setXMaximum(point.x() + buffer_size)
        extent.setYMinimum(point.y() - buffer_size)
        extent.setYMaximum(point.y() + buffer_size)
            # Appliquer l'extension et rafraîchir la carte
        iface.mapCanvas().setExtent(extent)
        iface.mapCanvas().refresh()
        QgsApplication.processEvents()  # Forcer le traitement des événements

       
capture_plan("PAR_REG_578")
3
  • maybe try a time delay /pause after the refresh. import time.sleep and then time.sleep(5) to sleep for 5 seconds. See if that solves the problem. at least it will eliminate one cause of the problem.
    – nr_aus
    Commented Nov 5 at 8:17
  • I have added a sleep(5) to the code ant the results are the same. I separated the setExtent() and saveAsImage() functions and the results are the same. I exported the new extents between the setExtent(0 and saveAsImage() and the new extents are correct. It's as if the canvas stays as it was until the end of the code.
    – Cary H
    Commented Nov 5 at 14:15
  • This is similar - gis.stackexchange.com/questions/231446/…
    – Cary H
    Commented Nov 5 at 14:34

2 Answers 2

0

I found references to iface.mapCanvas().mapCanvasRefreshed.connect(saveMap) here - @Nathan W I do not know how to pass a variable into the method to change the filename.

from qgis.core import *
from PyQt5.QtCore import QDateTime, QTimer
import os
import time
from qgis.utils import iface

# Récupérer l'instance du projet
project = QgsProject.instance()
regards = project.mapLayersByName("Temp")[0]

# Créer un répertoire si nécessaire
output_dir = r"E:\SSI_Projects\2024_Projects\ImageTest"
if not os.path.exists(output_dir):
    os.makedirs(output_dir)
   
# Récupérer les entités (features) de la couche et les convertir en liste

def zoom_extents(zx,zy):
        global nom_image
        point_geom = QgsGeometry.fromPointXY(QgsPointXY(zx, zy))
        # Récupérer les coordonnées du point
        point = point_geom.asPoint()
        
        buffer_size = 21
        extent = iface.mapCanvas().extent()
        extent.setXMinimum(point.x() - buffer_size)
        extent.setXMaximum(point.x() + buffer_size)
        extent.setYMinimum(point.y() - buffer_size)
        extent.setYMaximum(point.y() + buffer_size)
            # Appliquer l'extension et rafraîchir la carte
        iface.mapCanvas().setExtent(extent)
        iface.mapCanvas().refresh()
        
        e = iface.mapCanvas().extent()
        nom_image = "PAR_REG_578"
        iface.mapCanvas().mapCanvasRefreshed.connect(saveMap)
        return e.xMaximum(),e.yMaximum(),e.xMinimum(),e.yMinimum()
        
def saveMap():
    
    iface.mapCanvas().saveAsImage(os.path.join(output_dir, f"{nom_image}.jpg"))
    iface.mapCanvas().mapCanvasRefreshed.disconnect(saveMap)
    return
        

zoom_x = 19792163.971
zoom_y = 243117.141

result = zoom_extents(zoom_x, zoom_y)
print(result)
0

i found the cause of the problem. the problem was with the iteration. and the order of execution. i used your answers to get the slowdown of the iteration and i asked to rename the photo with the id of the previous iteration. this allows the script to find the right order and rename the photos taken properly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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