1

I have this code below which works well, creating an intersection layer.

But if it works the first time, at the second time it blocks, because the intersection layer is locked by QGIS (but I remove it from the interface before). And if I run it a third time, it works...

import os
import shutil
 
monCheminDeBase = r'C:\\Users\\georg\\Downloads\\'
 
project = QgsProject.instance()
project.removeAllMapLayers()
project.clear()
iface.mapCanvas().refresh()
 
peaks = QgsVectorLayer(monCheminDeBase + 'peaks_selection/peaks_selection.shp', 'Sommets', 'ogr')
iris = QgsVectorLayer(monCheminDeBase + 'iris/iris.shp', 'IRIS', 'ogr')
 
QgsProject.instance().addMapLayer(peaks)
QgsProject.instance().addMapLayer(iris)
 
# Directory for created layer
_peaks_iris = monCheminDeBase + '_peaks_iris'
if os.path.isdir(_peaks_iris) == True:
    shutil.rmtree(_peaks_iris)
if os.path.isdir(_peaks_iris) == False:
    os.mkdir(_peaks_iris)
 
# Intersect peaks and IRIS
peaks_iris_path = _peaks_iris + r'\\peaks_iris.shp'
 
processing.run('qgis:intersection', { \
"INPUT": peaks, \
"OVERLAY": iris, \
"INPUT_FIELDS": ["OSM_ID", "NAME", "OTHER_TAGS"], \
"OVERLAY_FIELDS": ["CODE_IRIS", "NOM_COM"], \
"OVERLAY_FIELDS_PREFIX": "", \
"OUTPUT": peaks_iris_path})
 
# Remove layers
project.removeMapLayer(peaks)
project.removeMapLayer(iris)
 
# Open the new intersected layer
peaks_iris = QgsVectorLayer(peaks_iris_path, "Sommets", "ogr")
project.addMapLayer(peaks_iris)
 
# Register layer
mes_sommets = project.mapLayersByName("Sommets")[0]

It seems to be the dbf file, whick is locking even if I remove the layer from the QGIS interface.

Do have you an idea for how to do this?

3
  • Have you tried deleting the output file?
    – Bera
    Commented Oct 16 at 6:53
  • Yes, no changes
    – Georgie
    Commented Oct 16 at 16:03
  • Something very strange: if I run my code in 3 parts (before the rmtree, then the rmtree, then after rmtree), it works. I tried added 2 time sleeps to simulate these 3 parts, it does not work... Crazy
    – Georgie
    Commented Oct 16 at 16:05

2 Answers 2

1

After removing the layers:

project.removeMapLayer(peaks)
project.removeMapLayer(iris)

You need to refresh the Map canvas:

iface.mapCanvas().refresh()

Clear the the Layer variables:

peaks = None
iris = None

Check if the layers are still in the project layer list:

layers = project.mapLayers()
if 'peaks' not in layers and 'iris' not in layers:
    print("Layers successfully removed!")

Try the above and let me know if it works for you.

1
  • No, it does not change anything
    – Georgie
    Commented Oct 16 at 10:25
0

I just did a little:

for g in globals():
    del g

at the begining of my code!

1
  • But it seems not work for all kind of "process". Here for example, it works for "qgis:intersection". But it does not work for "qgis:buffer". But I found this topic with a good solution for other processes like "qgis:buffer" or "qgis:reprojectlayer" (better to set your process in a def function, and adding a little delay with "QTimer.singleShot", THX Ben W): gis.stackexchange.com/questions/433300/…
    – Georgie
    Commented Oct 18 at 8:57

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.