QGIS API Documentation 4.3.0-Master (91d4a333d5d)
Loading...
Searching...
No Matches
QgsMapLayerUtils Class Reference

Contains utility functions for working with map layers. More...

#include <qgsmaplayerutils.h>

Static Public Member Functions

static QgsRectangle combinedExtent (const QList< QgsMapLayer * > &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext)
 Returns the combined extent of a list of layers.
static QgsAbstractDatabaseProviderConnection * databaseConnection (const QgsMapLayer *layer)
 Creates and returns the (possibly nullptr) database connection for a layer.
static bool isOpenStreetMapLayer (QgsMapLayer *layer)
 Returns true if the layer is served by OpenStreetMap server.
static bool isOpenStreetMapUri (const QString &uri, const QString &providerType)
 Returns true if the uri for a given provider is served by OpenStreetMap server.
static QString launderLayerName (const QString &name)
 Launders a layer's name, converting it into a format which is general suitable for file names or database layer names.
static bool layerRefersToUri (const QgsMapLayer *layer, const QString &uri, Qgis::SourceHierarchyLevel level=Qgis::SourceHierarchyLevel::Object)
 Returns true if a layer and uri point to the same resource at the specified hierarchy level.
static bool layerSourceMatchesPath (const QgsMapLayer *layer, const QString &path)
 Returns true if the source of the specified layer matches the given path.
static QString layerToolTip (const QgsMapLayer *layer)
 Returns the consistent tooltip for the given layer.
static QString layerTypeToString (Qgis::LayerType type)
 Returns the translated name of the type for a given layer type.
static QgsSaveStyleResult saveLayerStyleToDatabase (QgsMapLayer *layer, const QString &providerKey, const QString &targetDataSource, const QString &name, const QString &description, bool useAsDefault, const QString &uiFileContent, const Qgis::SaveStyleFormats formats=Qgis::SaveStyleFormats(Qgis::SaveStyleFormat::QML|Qgis::SaveStyleFormat::SLD), QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories)
 Saves QML and SLD representations of layer's style to a table in the database identified by providerKey and associates it with targetDataSource which can be different from the layer's own data source.
static QList< QgsMapLayer * > sortLayersByType (const QList< QgsMapLayer * > &layers, const QList< Qgis::LayerType > &order)
 Sorts a list of map layers by their layer type, respecting the order of types specified.
static bool updateLayerSourcePath (QgsMapLayer *layer, const QString &newPath)
 Updates a layer's data source, replacing its data source with a path referring to newPath.

Detailed Description

Contains utility functions for working with map layers.

Since
QGIS 3.20

Definition at line 38 of file qgsmaplayerutils.h.

Member Function Documentation

◆ combinedExtent()

QgsRectangle QgsMapLayerUtils::combinedExtent ( const QList< QgsMapLayer * > & layers,
const QgsCoordinateReferenceSystem & crs,
const QgsCoordinateTransformContext & transformContext )
static

Returns the combined extent of a list of layers.

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

Definition at line 40 of file qgsmaplayerutils.cpp.

◆ databaseConnection()

QgsAbstractDatabaseProviderConnection * QgsMapLayerUtils::databaseConnection ( const QgsMapLayer * layer)
static

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

Ownership is transferred to the caller.

Since
QGIS 3.22

Definition at line 101 of file qgsmaplayerutils.cpp.

◆ isOpenStreetMapLayer()

bool QgsMapLayerUtils::isOpenStreetMapLayer ( QgsMapLayer * layer)
static

Returns true if the layer is served by OpenStreetMap server.

Since
QGIS 3.40

Definition at line 200 of file qgsmaplayerutils.cpp.

◆ isOpenStreetMapUri()

bool QgsMapLayerUtils::isOpenStreetMapUri ( const QString & uri,
const QString & providerType )
static

Returns true if the uri for a given provider is served by OpenStreetMap server.

Since
QGIS 4.2

Definition at line 210 of file qgsmaplayerutils.cpp.

◆ launderLayerName()

QString QgsMapLayerUtils::launderLayerName ( const QString & name)
static

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 '_'.
Since
QGIS 3.28

Definition at line 188 of file qgsmaplayerutils.cpp.

◆ layerRefersToUri()

bool QgsMapLayerUtils::layerRefersToUri ( const QgsMapLayer * layer,
const QString & uri,
Qgis::SourceHierarchyLevel level = Qgis::SourceHierarchyLevel::Object )
static

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.
Exceptions
QgsNotSupportedExceptionif the layer's provider does not implement the QgsProviderMetadata::ProviderMetadataCapability::UrisReferToSame capability.
Since
QGIS 4.0

Definition at line 138 of file qgsmaplayerutils.cpp.

◆ layerSourceMatchesPath()

bool QgsMapLayerUtils::layerSourceMatchesPath ( const QgsMapLayer * layer,
const QString & path )
static

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.

Since
QGIS 3.22

Definition at line 129 of file qgsmaplayerutils.cpp.

◆ layerToolTip()

QString QgsMapLayerUtils::layerToolTip ( const QgsMapLayer * layer)
static

Returns the consistent tooltip for the given layer.

Since
QGIS 4.2

Definition at line 259 of file qgsmaplayerutils.cpp.

◆ layerTypeToString()

QString QgsMapLayerUtils::layerTypeToString ( Qgis::LayerType type)
static

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

Since
QGIS 4.0

Definition at line 232 of file qgsmaplayerutils.cpp.

◆ saveLayerStyleToDatabase()

QgsSaveStyleResult QgsMapLayerUtils::saveLayerStyleToDatabase ( QgsMapLayer * layer,
const QString & providerKey,
const QString & targetDataSource,
const QString & name,
const QString & description,
bool useAsDefault,
const QString & uiFileContent,
const Qgis::SaveStyleFormats formats = Qgis::SaveStyleFormats( Qgis::SaveStyleFormat::QML | Qgis::SaveStyleFormat::SLD ),
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories )
static

Saves QML and SLD representations of layer's style to a table in the database identified by providerKey and associates it with targetDataSource which can be different from the layer's own data source.

Unlike QgsMapLayer::saveStyleToDatabaseV2(), this method allows saving the style from one layer to a different layer in a database. The target layer is identified by the targetDataSource argument, which is a URI that specifies the layer in the database to which the style should be associated.

Parameters
layerSource layer whose style will be exported.
providerKeyProvider key for the target database.
targetDataSourceURI of the target layer in the database.
nameStyle name.
descriptionA description of the style.
useAsDefaultSet to true if style should be used as the default style for the layer.
uiFileContentOptional UI file content associated with the style.
formatsFlag specifying which style formats to save.
categoriesThe style categories to be saved.
Returns
Flags representing whether QML or SLD storing was successful.
Since
QGIS 4.4

Definition at line 296 of file qgsmaplayerutils.cpp.

◆ sortLayersByType()

QList< QgsMapLayer * > QgsMapLayerUtils::sortLayersByType ( const QList< QgsMapLayer * > & layers,
const QList< Qgis::LayerType > & order )
static

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.

Since
QGIS 3.26

Definition at line 172 of file qgsmaplayerutils.cpp.

◆ updateLayerSourcePath()

bool QgsMapLayerUtils::updateLayerSourcePath ( QgsMapLayer * layer,
const QString & newPath )
static

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.

Since
QGIS 3.22

Definition at line 157 of file qgsmaplayerutils.cpp.


The documentation for this class was generated from the following files: