Page 10 sur 18
Intersection
Below we set processes with a function in a QTimer.singleShot
, to allow multi-execution, but it is not mandatory according your context.
import os import shutil myPath = r'C:\\Users\\georg\\Downloads\\' project = QgsProject.instance() project.removeAllMapLayers() project.clear() iface.mapCanvas().refresh() peaks = QgsVectorLayer(myPath + 'peaks_selection/peaks_selection.shp', 'Peaks', 'ogr') iris = QgsVectorLayer(myPath + 'iris/iris.shp', 'IRIS', 'ogr') QgsProject.instance().addMapLayer(peaks) QgsProject.instance().addMapLayer(iris) # Intersection function def myIntersection(): # Directory for created layer _peaks_intersection = myPath + '_peaks_intersection' if os.path.isdir(_peaks_intersection) == True: shutil.rmtree(_peaks_intersection) if os.path.isdir(_peaks_intersection) == False: os.mkdir(_peaks_intersection) # Intersect peaks and IRIS peaks_intersection_path = _peaks_intersection + r'\\peaks_intersection.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_intersection_path}) # Remove layers project.removeMapLayer(peaks) project.removeMapLayer(iris) # Open the new intersected layer peaks_intersection = QgsVectorLayer(peaks_intersection_path, 'Peaks', "ogr") project.addMapLayer(peaks_intersection) # Register layer myIntersection = project.mapLayersByName('Peaks')[0] # Run function with delay QTimer.singleShot(200, myIntersection)