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?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityI 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?
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.
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.