Page 11 sur 18
Buffer
Below we set processes with a function in a QTimer.singleShot
, to allow multi-execution, but it is not mandatory according your context.
Buffer with 4326 point layer, so with fake units:
import os import shutil myPath = r'C:\\Users\\georg\\Downloads\\' project = QgsProject.instance() project.removeAllMapLayers() project.clear() iface.mapCanvas().refresh() crs = QgsCoordinateReferenceSystem.fromEpsgId(4326) project.setCrs(crs) project.setEllipsoid('EPSG:4326') peaks = QgsVectorLayer(myPath + 'peaks_selection/peaks_selection.shp', 'Peaks', 'ogr') QgsProject.instance().addMapLayer(peaks) # Buffer function def myBuffer(): # Directory for created layer _peaks_buffer = myPath + '_peaks_buffer' if os.path.isdir(_peaks_buffer) == True: shutil.rmtree(_peaks_buffer) if os.path.isdir(_peaks_buffer) == False: os.mkdir(_peaks_buffer) # Buffer on peaks peaks_buffer_path = _peaks_buffer + r'\\peaks_buffer.shp' processing.run('qgis:buffer', { \ "INPUT": peaks, \ "DISTANCE": 0.05, \ "SEGMENTS": 10, \ "END_CAP_STYLE": 0, \ "DISSOLVE": False, \ "OUTPUT": peaks_buffer_path}) # Open the buffer layer peaks_buffer = QgsVectorLayer(peaks_buffer_path, "Buffer area", "ogr") project.addMapLayer(peaks_buffer) # Register layer my_buffer = project.mapLayersByName("Buffer area")[0] # Buffer below root = project.layerTreeRoot() myBelowLayer = root.findLayer(peaks_buffer.id()) myClone = myBelowLayer.clone() parent = myBelowLayer.parent() parent.insertChildNode(-1, myClone) parent.removeChildNode(myBelowLayer) # Run function with delay QTimer.singleShot(200, myBuffer)