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.
QgsMapLayerRegistry.instance().removeMapLayer(layer)
, try adding the following below it:del vecLine
. – Joseph Jan 6 '17 at 10:04del 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... – Joseph Jan 6 '17 at 14:34