Azad Rasul
SmartRS

SmartRS

14- Managing Project Layers

Azad Rasul's photo
Azad Rasul

Published on Jun 15, 2021

1 min read

Subscribe to my newsletter and never miss my upcoming articles

import processing
uri = "D:/Python_QGIS/data/places.shp"
vlayer = iface.addVectorLayer(uri, "Places", "ogr")

Useing runAndLoadResults() function to create buffer

processing.runAndLoadResults("native:buffer", {'INPUT':uri,'DISTANCE':0.1,'SEGMENTS':5,'END_CAP_STYLE':0,'JOIN_STYLE':0,'MITER_LIMIT':2,'DISSOLVE':False,'OUTPUT':'memory:'})

project = QgsProject.instance()
print(project.mapLayers())

It returns: {'output_1a4a410f_b372_49ca_87d4_e6aeba988509': , 'places_a7c9b8cb_2aee_47e0_8101_5caf81e9607c': }

Print name of layers with for loop

for id, layer in project.mapLayers().items():
    print(layer.name())

Rename a layer

rename = project.mapLayersByName('Buffered')[0]
rename.setName('Renamed!')

Remove a layer

delete = project.mapLayersByName('Places')[0]
project.removeMapLayer(delete.id())
 
Share this
Proudly part of