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

Export 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(doc: QDomDocument)

Bases: sip.wrapper

Constructor

Parameters

doc (QDomDocument) – 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)

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

hasCycle(self)bool

Whether some cyclic dependency has been detected

Return type

bool

hasMissingDependency(self)bool

Whether some dependency is missing

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]

Export the selected layer tree nodes to a QLR file

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

Parameters
Return type

Tuple[bool, str]

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]