Feature request #10764

Python API to handle layer groups without iface

Added by David AMAR almost 10 years ago. Updated almost 10 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:-
Pull Request or Patch supplied:No Resolution:fixed/implemented
Easy fix?:No Copied to github as #:19147

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 Magnifier (1.75 KB) David AMAR, 2014-07-01 02:53 AM

History

#1 Updated by Martin Dobias almost 10 years ago

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

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.

#2 Updated by David AMAR almost 10 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 PDF