I have a plugin that creates a memory layer and displays it on the canvas. If I run the plugin a second time after removing the memory layer from the layer list panel without closing QGIS the memory layer is produced along with the previously created layer. It seems as though the memory layer name cannot be reused until QGIS is closed. Is there a way to clear this layer from the memory to rerun the plugin?
###Create new layer
vecLineURI = "LineString?crs=epsg:4326&field=id:integer"
vecLine = QgsVectorLayer(vecLineURI,"point2line","memory")
pr = vecLine.dataProvider()
###Start editing
vecLine.startEditing()
###Add Feature
feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromWkt(lneWKT))
feat.setAttributes([1])
pr.addFeatures( [feat] )
####Update Extents
vecLine.updateExtents()
###Commit Changes
vecLine.commitChanges()
###Load layer
QgsMapLayerRegistry.instance().addMapLayer(vecLine)
I was removing the layer at the start of the plugin open...
layerMap = QgsMapLayerRegistry.instance().mapLayers()
for name, layer in layerMap.iteritems():
if "point2line" in name:
QgsMapLayerRegistry.instance().removeMapLayer(layer)
...but this only removed items from the layer panel not from memory.
2 Answers 2
To remove the memory layer from QGIS and from memory use:
QgsMapLayerRegistry.instance().removeMapLayer(myMemoryLayer.id())
If you are working with memory layers outside of QGIS and you want to remove it from memory you will have to add it then remove it like:
QgsMapLayerRegistry.instance().addMapLayer(myMemoryLayer)
QgsMapLayerRegistry.instance().removeMapLayer(myMemoryLayer.id())
-
Even though the layer has been removed from the registry, the duplicate is still produced. I edited the original statement to be a bit more clear, I hope.anon1234– anon12342017年01月05日 17:00:24 +00:00Commented Jan 5, 2017 at 17:00
-
Did you try removing it by code besides just removing it from the UI? During the beginning of your plugin you could search the layers in the map to see if the memory layer is there, if so then remove it, then continue with your expected plugin process.artwork21– artwork212017年01月05日 17:20:02 +00:00Commented Jan 5, 2017 at 17:20
-
I was able to get the suggestion to function within the code, such that the items in the registry are removed at the start of a new plugin session. However, the second run still produces 2 outputs. The plugin produces a single memory layer with a hardcoded name. It seems to be stored in memory and not replaced by the new plugin run, then loading 2 layers on the canvas.anon1234– anon12342017年01月05日 17:53:19 +00:00Commented Jan 5, 2017 at 17:53
-
Are you able to post related parts of your code in the question related to this (when the layer is created, when it is added, when it is removed....etc)?artwork21– artwork212017年01月05日 18:01:50 +00:00Commented Jan 5, 2017 at 18:01
QgsMapLayerRegistry has been depreciated as of QGIS 3 as per this answer
The way to do this using QGIS 3.# is as follows:
First add the in memory layer to the project
QgsProject.instance().addMapLayer(myMemoryLayer)
Then remove the map layer from the project
QgsProject.instance().removeMapLayer(myMemoryLayer.id())
QgsMapLayerRegistry.instance().removeMapLayer(layer)
, try adding the following below it:del vecLine
.del vecLine
after you load the layer withQgsMapLayerRegistry.instance().addMapLayer(vecLine)
? I would have thought memory layers could be cleared from memory but I never needed to try this...