Class: QgsMapLayerUtils

class qgis.core.QgsMapLayerUtils

Bases: sip.wrapper

Contains utility functions for working with map layers.

New in version 3.20:

Methods

combinedExtent

Returns the combined extent of a list of layers.

databaseConnection

Creates and returns the (possibly None) database connection for a layer.

launderLayerName

Launders a layer's name, converting it into a format which is general suitable for file names or database layer names.

layerSourceMatchesPath

Returns True if the source of the specified layer matches the given path.

sortLayersByType

Sorts a list of map layers by their layer type, respecting the order of types specified.

updateLayerSourcePath

Updates a layer's data source, replacing its data source with a path referring to newPath.

combinedExtent(layers: Iterable[QgsMapLayer], crs: QgsCoordinateReferenceSystem, transformContext: QgsCoordinateTransformContext) QgsRectangle

Returns the combined extent of a list of layers.

The crs argument specifies the desired coordinate reference system for the combined extent.

Parameters:
Return type:

QgsRectangle

databaseConnection(layer: QgsMapLayer) QgsAbstractDatabaseProviderConnection

Creates and returns the (possibly None) database connection for a layer. Ownership is transferred to the caller.

New in version 3.22.

Parameters:

layer (QgsMapLayer) –

Return type:

QgsAbstractDatabaseProviderConnection

launderLayerName(name: str) str

Launders a layer’s name, converting it into a format which is general suitable for file names or database layer names.

Specifically this method:

  • Converts the name to lowercase

  • Replaces spaces by underscore characters

  • Removes any characters which are not alphanumeric or ‘_’.

New in version 3.28.

Parameters:

name (str) –

Return type:

str

layerSourceMatchesPath(layer: QgsMapLayer, path: str) bool

Returns True if the source of the specified layer matches the given path.

This method can be used to test whether a layer is associated with a file path.

New in version 3.22.

Parameters:
Return type:

bool

sortLayersByType(layers: Iterable[QgsMapLayer], order: Iterable[Qgis.LayerType]) List[QgsMapLayer]

Sorts a list of map layers by their layer type, respecting the order of types specified.

Layer types which appear earlier in the order list will result in matching layers appearing earlier in the result list.

New in version 3.26.

Parameters:
Return type:

List[QgsMapLayer]

updateLayerSourcePath(layer: QgsMapLayer, newPath: str) bool

Updates a layer’s data source, replacing its data source with a path referring to newPath.

Returns True if the layer was updated, or False if the layer was not updated (e.g. it uses a data provider which does not specify paths in a layer URI.

New in version 3.22.

Parameters:
Return type:

bool