Class: QgsLayerDefinition

class qgis.core.QgsLayerDefinition

Bases: sip.wrapper

The QgsLayerDefinition class holds generic methods for loading/exporting QLR files.

QLR files are an export of the layer xml including the style and datasource location. There is no link to the QLR file once loaded. Consider the QLR file a mini project file for layers and styles. QLR files also store the layer tree info for the exported layers, including group information.

Methods

exportLayerDefinition

Exports the selected layer tree nodes to a QLR file.

exportLayerDefinitionLayers

Returns the given layer as a layer definition document Layer definitions store the data source as well as styling and custom properties.

loadLayerDefinition

Loads the QLR at path into QGIS.

loadLayerDefinitionLayers

Creates new layers from a layer definition document.

class DependencySorter

Bases: sip.wrapper

Class used to work with layer dependencies stored in a XML project or layer definition file

QgsLayerDefinition.DependencySorter(doc: QDomDocument) Constructor

Parameters:

doc – The XML document containing maplayer elements

QgsLayerDefinition.DependencySorter(fileName: str) Constructor

Parameters:

fileName – The filename where the XML document is stored

QgsLayerDefinition.DependencySorter(QgsLayerDefinition.DependencySorter)

hasCycle(self) bool

Whether some cyclic dependency has been detected

Return type:

bool

hasMissingDependency(self) bool

Whether some dependency is missing

Return type:

bool

isLayerDependent(self, layerId: str) bool

Returns whether the layer associated with the``layerId`` is dependent from another layer

New in version 3.32.

Parameters:

layerId (str) –

Return type:

bool

sortedLayerIds(self) List[str]

Gets the layer IDs in an order where they can be loaded incrementally without dependency break

Return type:

List[str]

sortedLayerNodes(self) List[QDomNode]

Gets the layer nodes in an order where they can be loaded incrementally without dependency break

Return type:

List[QDomNode]

exportLayerDefinition(path: str, selectedTreeNodes: Iterable[QgsLayerTreeNode]) Tuple[bool, str]

Exports the selected layer tree nodes to a QLR file.

This method uses the QgsProject.instance()’s file path setting to determine whether absolute or relative paths are written. Use the variant with an explicit argument for file path type for control over this setting.

Parameters:
  • path (str) – file path for exported QLR file

  • selectedTreeNodes (Iterable[QgsLayerTreeNode]) – layer tree nodes to include in the QLR file

Returns:

  • True if the export was successful

  • errorMessage: will be set to any error messages generated during the export

exportLayerDefinition(path: str, selectedTreeNodes: Iterable[QgsLayerTreeNode], pathType: Qgis.FilePathType) -> Tuple[bool, str] Exports the selected layer tree nodes to a QLR file.

Parameters:
  • path – file path for exported QLR file

  • selectedTreeNodes – layer tree nodes to include in the QLR file

  • pathType – specifies whether absolute or relative file paths should be used.

Return type:

Tuple[bool, str]

Returns:

  • True if the export was successful

  • errorMessage: will be set to any error messages generated during the export

New in version 3.22.

exportLayerDefinition(doc: QDomDocument, selectedTreeNodes: Iterable[QgsLayerTreeNode], context: QgsReadWriteContext) -> Tuple[bool, str] Exports the selected layer tree nodes to a QLR XML document.

exportLayerDefinitionLayers(layers: Iterable[QgsMapLayer], context: QgsReadWriteContext) QDomDocument

Returns the given layer as a layer definition document Layer definitions store the data source as well as styling and custom properties.

Layer definitions can be used to load a layer and styling all from a single file.

This is a low-level routine that does not write layer tree.

Parameters:
Return type:

QDomDocument

loadLayerDefinition(path: str, project: QgsProject, rootGroup: QgsLayerTreeGroup) Tuple[bool, str]

Loads the QLR at path into QGIS. New layers are added to given project into layer tree specified by rootGroup

loadLayerDefinition(doc: QDomDocument, project: QgsProject, rootGroup: QgsLayerTreeGroup, context: QgsReadWriteContext) -> Tuple[bool, str] Loads the QLR from the XML document. New layers are added to given project into layer tree specified by rootGroup

Parameters:
Return type:

Tuple[bool, str]

loadLayerDefinitionLayers(document: QDomDocument, context: QgsReadWriteContext) List[QgsMapLayer]

Creates new layers from a layer definition document. This is a low-level routine that does not resolve layer ID conflicts, dependencies and joins

loadLayerDefinitionLayers(qlrfile: str) -> List[QgsMapLayer] Creates new layers from a layer definition file (.QLR) This is a low-level routine that does not resolve layer ID conflicts, dependencies and joins

Parameters:
Return type:

List[QgsMapLayer]