Friday, 23 January 2015

pyqgis - Removing a vector layer from QGIS



Considering my program is generating a shapefile that has to be added to the "Layers" tab in QGIS interface. However, once the utility of the layer is completed, I need to remove the layer in the middle of the code. I want to remove only the active layer and not all.


To put it in easier words, is there an opposite to self.iface.addVectorLayer() ?



Answer



This question may be seen as a duplicate with this post, in which case I will happily remove my answer.



The solution, provided by @andytilia, involves using the following command for versions of QGIS from 1.8 to 2.99.


QgsMapLayerRegistry.instance().removeMapLayers( [vl.id()] )

The solution involves using the following command for QGIS 3.X.


QgsProject.instance().removeMapLayers( [vl.id()] )

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...