Page 18 sur 23
Buffer
En guise d'entrainement, commencez par créer une zone tampon de 5 mètres autour de nos sommets.
import os import shutil monCheminDeBase = 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(monCheminDeBase + 'peaks_selection/peaks_selection.shp', 'Sommets', 'ogr') QgsProject.instance().addMapLayer(peaks) # Directory for created layer _peaks_buffer = monCheminDeBase + '_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('native:buffer', { \ "INPUT": peaks, \ "DISTANCE": 5, \ "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)
Mais le code ci-dessus fonctionne très mal n'est-ce pas ?
- Essayez de tricher
- Essayez de comprendre pourquoi ça ne marche pas
- Inspectez les unités géométriques utilisées dans votre canvas
- Contrôlez vos logs dans View / Panels / Log Messages
Utilisez plutôt le code suivant pour créer des buffer de 2 kms autour de nos 108 000 adresses des Hautes-Alpes.
import os import shutil monCheminDeBase = r'C:\\Users\\georg\\Downloads\\' project = QgsProject.instance() project.removeAllMapLayers() project.clear() iface.mapCanvas().refresh() crs = QgsCoordinateReferenceSystem.fromEpsgId(2154) project.setCrs(crs) project.setEllipsoid('EPSG:2154') address = QgsVectorLayer(monCheminDeBase + 'adresses/ADRESSE_05.shp', 'Adresses', 'ogr') QgsProject.instance().addMapLayer(address) # Directory for created layer _address_buffer = monCheminDeBase + '_address_buffer' if os.path.isdir(_address_buffer) == True: shutil.rmtree(_address_buffer) if os.path.isdir(_address_buffer) == False: os.mkdir(_address_buffer) # Buffer on address address_buffer_path = _address_buffer + r'\\address_buffer.shp' processing.run('native:buffer', { \ "INPUT": address, \ "DISTANCE": 2000, \ "SEGMENTS": 10, \ "END_CAP_STYLE": 0, \ "DISSOLVE": False, \ "OUTPUT": address_buffer_path}) # Open the buffer layer address_buffer = QgsVectorLayer(address_buffer_path, "Buffer area", "ogr") project.addMapLayer(address_buffer) # Register layer my_buffer = project.mapLayersByName("Buffer area")[0] # Buffer below root = project.layerTreeRoot() myBelowLayer = root.findLayer(address_buffer.id()) myClone = myBelowLayer.clone() parent = myBelowLayer.parent() parent.insertChildNode(-1, myClone) parent.removeChildNode(myBelowLayer)