Script using PyQgis is doing error, probably access memory

116 Views Asked by At

I have some troubles about a script using PyQgis. So, I have a main.py with :

import qgis
from qgis.core import QgsApplication
qgs = QgsApplication([], False)
qgs.setPrefixPath("C:/xx", True)
qgs.initQgis()
from Module import function1
from Module import function2


return1 = function1(x, y, z)
print(return1)
function2(x, y, z, return1)

I wrote function1 to find automatically an object with PyQgis within a shapefile (vector layer). Sometimes this object is not the good one, so I added a step to do a manual check from a canvas. If the object is not the good one, the operator have to click on the good one. This return the "id" of the object. Hence, I use :

def function1(x,y,z):
project = QgsProject.instance()
project.read('C:/abc.qgz')
*[...]
[load vlayer]
[find automaticaly an the object]
[...]*
    class IdentifyTool(QgsMapToolIdentify):
        def __init__(self, canvas, layer):
            super().__init__(canvas)
            self.layer = layer
            self.selected_feature_id = None

        def canvasReleaseEvent(self, event):
            results = super().identify(event.x(), event.y(), [self.layer], QgsMapToolIdentify.TopDownAll)
            for result in results:
                feature = result.mFeature
                print("ID of selected object:", feature['id'])
                self.selected_feature_id = feature['id']

    app = QgsApplication([], False)
    app.initQgis()

    canvas = QgsMapCanvas()
    canvas.setWindowTitle('xx')
    canvas.setCanvasColor(Qt.white)
    canvas.setLayers([vlayer])

    identify_tool = IdentifyTool(canvas, vlayer)
    canvas.setMapTool(identify_tool)

    canvas.show()
    app.exec_()

    return identify_tool.selected_feature_id

def function2(x,y,z,return1):
    print('test0')
    project = QgsProject.instance()
    print('test 0 bis')
    project.read('C:/abc.qgz')
    print('test1')

Therefore, return1 is properly returned. "test0" & "test 0 bis" are correcly returned. But ('test1') is not because I have :

Process finished with exit code -1073741819 (0xC0000005)

Any idea is welcome!

0

There are 0 best solutions below