Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have some QGIS projects that I've been using for long time, adding and removing layers, updating reference to deleted or moved files etc.

Opening with a text editor the qgs file I see that it contains numerous references to files, and this references are not showed on the Layer tree pane.

I have here different cases: references to existing file, references to missing files, references to database connections. I got aware f this when I opened my project on another machine, and QGIS reported a missing database connection to vector tables not present in the layer tree view.

Manually deleting references from the qgs file solved the problem, but I'm managing huge projects and it's near to impossible to manually check the project consistency. I've tried the "Remove empty layers" plugin, but the ones I'm trying to remove are not empty layers. Is there any way to 'purge' qgis file?

share|improve this question
    
Did you check by storing relative paths in .qgs file? Its at Setting--> Project Properties-->save paths-->relative Cheers N –  Naresh Sep 17 '12 at 11:41
    
What about "Save as ..." to a new project file? –  Andre Joost Sep 17 '12 at 15:18
1  
That did not work! I'll post to QGIS user mailing list a GQIS project file containing a phantom layer, not present in Layers pane. –  bradypus Sep 17 '12 at 19:59
1  
Could you post that qgs file somewhere we could get to it? I'm unable to duplicate your situation. Perhaps all you need is a simple parser. –  ike Apr 19 '13 at 23:32
1  
@ike Please check here: lists.osgeo.org/pipermail/qgis-user/2012-September/019327.html. You'll find there the test project I sent to the mailing list. To reproduce it you should place a tif file in this relative path: ./Planimetrie/2012/plan01_2012.tif. Please read also the following messages for more details. –  bradypus Apr 20 '13 at 12:57
show 3 more comments

1 Answer 1

The layers in question are in the layer registry but not in the legend.

The following python script removes all layers which are loaded, but not shown in the legend.

For QGIS < 1.9 the following line is required to be run initially:

iface = qgis.utils.iface

Removing the layers:

registryLayers = QgsMapLayerRegistry.instance().mapLayers().keys()
legendLayers = [ layer.id() for layer in iface.legendInterface().layers() ]
layersToRemove = set( registryLayers ) - set( legendLayers )
QgsMapLayerRegistry.instance().removeMapLayers( list( layersToRemove ) )

It should be run from the python console inside QGIS. After applying it you can verify the operation by checking that the layers are gone from File (Project in 2.0) => Project Properties => Identifiable layers. Changes are not saved automatically

By adding the following line at the end, changes will also be saved automatically.

iface.actionSaveProject().trigger()

Please make a Backup copy of your Project file (.qgs) before overwriting your project file after applying this script.

Required Minimum QGIS Version: 1.8

share|improve this answer
    
Ok i am getting errors when i enter it in. Traceback (most recent call last): File "<input>", line 1, in <module> NameError: name 'legendLayerNames' is not defined –  Gerald Apr 22 '13 at 9:13
1  
QGIS 1.8 requires another way of accessing iface. The answer is updated to reflect this. –  Matthias Kuhn Apr 22 '13 at 9:27
1  
I think you have it here. Well it worked in my case. And as a result made QGIS Cloud work better also as it is able to upload the layers now. Excellent. –  Gerald Apr 22 '13 at 10:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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