5

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.

5
  • After the line QgsMapLayerRegistry.instance().removeMapLayer(layer), try adding the following below it: del vecLine.
    – Joseph
    Jan 6 '17 at 10:04
  • @Joseph...the variable vecLine does not exist at the time of the for loop. The plugin works the first time but then fails due to calling a variable before defining it. Sounds like you think it is a Python issue and not a memory layer within QGIS?
    – anon1234
    Jan 6 '17 at 14:28
  • Ahh I missed that, try adding del vecLine after you load the layer with QgsMapLayerRegistry.instance().addMapLayer(vecLine)? I would have thought memory layers could be cleared from memory but I never needed to try this...
    – Joseph
    Jan 6 '17 at 14:34
  • I tried having the user add a name to each new plugin run but it still produced 1 layer per plugin iteration during the QGIS session. All the layers are "Memory Storage" storage type. Seems as the memory storage stores the number of layers created not the individual layers...
    – anon1234
    Jan 6 '17 at 14:59
  • 1
    For now, the work around is to save each new output as a file on disk until I figure out how to clear the memory.
    – anon1234
    Jan 10 '17 at 19:02
4

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())
4
  • 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
    Jan 5 '17 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
    Jan 5 '17 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
    Jan 5 '17 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
    Jan 5 '17 at 18:01
1

QgsMapLayerRegistry has been depreciated as of QGIS 3 as per this answer

QGIS API Documentation

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())

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .