Feature request #10764

Python API to handle layer groups without iface

Added by David AMAR over 2 years ago. Updated over 2 years ago.

Status:Closed Start Date:07/01/2014
Priority:Normal Due date:
Assigned to:- % Done:

0%

Category:-
Target version:Future Release - Nice to have
Platform: Resolution:fixed/implemented
Platform version: Pull Request or Patch supplied:No
Status info: Tag:

Description

My intent is to automate production of raster files to use QgisMapServer as a WMS Server.
In order to manage it, I use a stand-alone python script (without GUI) to handle QGS project files. See attached file modifyqgs.py.

Up to now, I am able to add raster layers to an existing project using QgsMapLayerRegistry.

Now, I want to organize layers using groups but I didn't find any API to acheive it using "headless" API - that is without qgis.utils.iface.legendInterface() component. Still undocumented QgsLayerTreeGroup used by QgsProject may be a future solution?

Regards.

modifyqgs.py (1.8 kB) David AMAR, 07/01/2014 02:53 am

History

Updated by Martin Dobias over 2 years ago

  • Status changed from Open to Closed
  • Resolution set to fixed/implemented

The QgsLayerTree* classes are exactly what you are looking for. Please see the doxygen documentation of the classes (especially QgsLayerTreeNode, QgsLayerTreeLayer, QgsLayerTreeGroup). I will add some more documentation to PyQGIS cookbook later. For now, your entry point is QgsProject.instance().layerTreeRoot() - from there you can start adding child groups and layers.

Updated by David AMAR over 2 years ago

Thank you Martin.

I have just tried these classes and I think there are some points to fix if you confirm them:
- QgsLayerTreeNode, QgsLayerTreeLayer, QgsLayerTreeGroup documentation is not generated by doxygen in official documentation http://qgis.org/api/2.4.

- When I try 'print QgsProject.instance().layerTreeRoot().dump()' in a stand-alone python script, I only see GROUP nodes. Same command in Python Console in QGIS Desktop displays GROUP and LAYER nodes.
Ex:
Results in stand-alone script
---
GROUP: visible=2 expanded=1
GROUP: myGroup visible=2 expanded=1
---

Results in Python Console
---
GROUP: visible=2 expanded=1
GROUP: myGroup visible=2 expanded=1
LAYER: myRaster visible=2 expanded=1 id=myRaster20140702045512566
---

Best Regards.

Also available in: Atom