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?