3

I have more than 100 layers loaded in map canvas. Some layers don't have any feature. I would like to delete all layer with zero feature count.

Is it possible to get a script which can delete all these layers?

1
  • 1
    Yes it's possible. What have you got so far? May 24 '16 at 3:44
7

Better than a script, there's a plugin to achieve it, and it's called "Remove empty layers from the map."

http://plugins.qgis.org/plugins/RemoveEmptyLayers/

Disclaimer: I'm the plugin's author.

2

Without knowing exactly where you're up to it's a bit difficult, but here's the nuts and bolts:

layermap = QgsMapLayerRegistry.instance().mapLayers()
RemoveLayers = []
for name, layer in layermap.iteritems():
    if layer.isValid():
        if layer.type() == QgsMapLayer.VectorLayer:
            if layer.featureCount() == 0:
                RemoveLayers.append(layer.id())
if len(RemoveLayers) > 0:
    QgsMapLayerRegistry.instance().removeMapLayers( RemoveLayers )

I was also going to comment on http://plugins.qgis.org/plugins/RemoveEmptyLayers/ but as the author has already answered that one I'll just upvote his answer.

Also have a read of How to remove a vector QgsVectorLayer from QGis python plugin which is related.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.