Class: QgsMapLayerUtils

Contains utility functions for working with map layers.

Added in version 3.20.

List of all members, including inherited members

Static Methods

combinedExtent

Returns the combined extent of a list of layers.

databaseConnection

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

isOpenStreetMapLayer

Returns True if the layer is served by OpenStreetMap server.

launderLayerName

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

layerRefersToUri

Returns True if a layer and uri point to the same resource at the specified hierarchy level.

layerSourceMatchesPath

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

layerTypeToString

Returns the translated name of the type for a given layer type.

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.

class qgis.core.QgsMapLayerUtils[source]

Bases: object

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

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

static databaseConnection(layer: QgsMapLayer | None) QgsAbstractDatabaseProviderConnection | None[source]

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

Added in version 3.22.

Parameters:

layer (Optional[QgsMapLayer])

Return type:

Optional[QgsAbstractDatabaseProviderConnection]

static isOpenStreetMapLayer(layer: QgsMapLayer | None) bool[source]

Returns True if the layer is served by OpenStreetMap server.

Added in version 3.40.

Parameters:

layer (Optional[QgsMapLayer])

Return type:

bool

static launderLayerName(name: str | None) str[source]

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 ‘_’.

Added in version 3.28.

Parameters:

name (Optional[str])

Return type:

str

static layerRefersToUri(layer: QgsMapLayer | None, uri: str | None, level: Qgis.SourceHierarchyLevel = Qgis.SourceHierarchyLevel.Object) bool[source]

Returns True if a layer and uri point to the same resource at the specified hierarchy level.

This method parses the underlying connection parameters of the layer and uri to check if they share the same scope defined by level.

Note

This method is only valid for a uri from the same data provider as layer.

Warning

Not all providers implement this functionality. Check whether the provider’s metadata capabilities returns the QgsProviderMetadata.ProviderMetadataCapability.UrisReferToSame to determine whether a specific provider metadata object supports this method.

Raises:

QgsNotSupportedException – if the layer’s provider does not implement the QgsProviderMetadata.ProviderMetadataCapability.UrisReferToSame capability.

Added in version 4.0.

Parameters:
Return type:

bool

static layerSourceMatchesPath(layer: QgsMapLayer | None, path: str | None) bool[source]

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.

Added in version 3.22.

Parameters:
Return type:

bool

static layerTypeToString(type: Qgis.LayerType) str[source]

Returns the translated name of the type for a given layer type.

Added in version 4.0.

Parameters:

type (Qgis.LayerType)

Return type:

str

static 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.

Added in version 3.26.

Parameters:
Return type:

list[QgsMapLayer]

static updateLayerSourcePath(layer: QgsMapLayer | None, newPath: str | None) bool[source]

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.

Added in version 3.22.

Parameters:
Return type:

bool