Skip to content

Class LayerUtils

ClassList > LayerUtils

Inherits the following classes: QObject

Public Functions

Type Name
LayerUtils (QObject * parent=nullptr)
Q_INVOKABLE QSet< QVariant > uniqueValuesForVectorLayerFieldIndex (QgsVectorLayer * layer, int fieldIndex)

Public Static Functions

Type Name
Q_INVOKABLE bool addFeature (QgsVectorLayer * layer, QgsFeature feature)
QgsMapLayer * createBasemap (const QString & style=QString())
Q_INVOKABLE FeatureIterator createFeatureIterator (QgsVectorLayer * layer)
Q_INVOKABLE FeatureIterator createFeatureIteratorFromExpression (QgsVectorLayer * layer, const QString & expression)
Q_INVOKABLE FeatureIterator createFeatureIteratorFromRectangle (QgsVectorLayer * layer, const QgsRectangle & rectangle)
Q_INVOKABLE QgsVectorLayer * createMemoryLayer (const QString & name, const QgsFields & fields=QgsFields(), Qgis::WkbType geometryType=Qgis::WkbType::NoGeometry, const QgsCoordinateReferenceSystem & crs=QgsCoordinateReferenceSystem())
QgsRasterLayer * createOnlineElevationLayer ()
QgsAbstractVectorLayerLabeling * defaultLabeling (QgsVectorLayer * layer, QgsTextFormat textFormat=QgsTextFormat())
QgsSymbol * defaultSymbol (QgsVectorLayer * layer, const QString & attachmentField=QString(), const QString & colorField=QString())
Q_INVOKABLE bool deleteFeature (QgsProject * project, QgsVectorLayer * layer, const QgsFeatureId fid, bool flushBuffer=true)
Q_INVOKABLE QgsFeature duplicateFeature (QgsVectorLayer * layer, QgsFeature feature)
Q_INVOKABLE QString fieldType (const QgsField & field)
Q_INVOKABLE bool hasMValue (QgsVectorLayer * layer)
Q_INVOKABLE bool isAtlasCoverageLayer (QgsVectorLayer * layer)
Q_INVOKABLE bool isFeatureAdditionLocked (QgsMapLayer * layer)
Q_INVOKABLE QgsRasterLayer * loadRasterLayer (const QString & uri, const QString & name=QString(), const QString & provider=QStringLiteral("gdal"))
Q_INVOKABLE QgsVectorLayer * loadVectorLayer (const QString & uri, const QString & name=QString(), const QString & provider=QStringLiteral("ogr"))
Q_INVOKABLE QgsVectorLayer * memoryLayerFromJsonString (const QString & name, const QString & string, const QgsCoordinateReferenceSystem & crs=QgsCoordinateReferenceSystem())
Q_INVOKABLE QString saveVectorLayerAs (QgsVectorLayer * layer, const QString & filePath, const QString & driverName=QString(), const QString & filterExpression=QString())
Q_INVOKABLE void selectFeaturesInLayer (QgsVectorLayer * layer, const QList< int > & fids, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection)
void setDefaultLabeling (QgsVectorLayer * layer, QgsProject * project=nullptr)
void setDefaultRenderer (QgsVectorLayer * layer, QgsProject * project=nullptr, const QString & attachmentField=QString(), const QString & colorField=QString())

Public Functions Documentation

function LayerUtils

explicit LayerUtils::LayerUtils (
    QObject * parent=nullptr
) 

function uniqueValuesForVectorLayerFieldIndex

Q_INVOKABLE QSet< QVariant > LayerUtils::uniqueValuesForVectorLayerFieldIndex (
    QgsVectorLayer * layer,
    int fieldIndex
) 

Returns a list of unique values for a given fieldIndex from the layer.


Public Static Functions Documentation

function addFeature

static Q_INVOKABLE bool LayerUtils::addFeature (
    QgsVectorLayer * layer,
    QgsFeature feature
) 

Adds a feature into the layer.

Note:

The function will not call startEditing() and commitChanges()


function createBasemap

static QgsMapLayer * LayerUtils::createBasemap (
    const QString & style=QString()
) 

Creates an online basemap layer.


function createFeatureIterator

static Q_INVOKABLE FeatureIterator LayerUtils::createFeatureIterator (
    QgsVectorLayer * layer
) 

Returns a feature iterator to get all features within the provided layer.


function createFeatureIteratorFromExpression

static Q_INVOKABLE FeatureIterator LayerUtils::createFeatureIteratorFromExpression (
    QgsVectorLayer * layer,
    const QString & expression
) 

Returns a feature iterator to get features matching a given expression within the provided layer.


function createFeatureIteratorFromRectangle

static Q_INVOKABLE FeatureIterator LayerUtils::createFeatureIteratorFromRectangle (
    QgsVectorLayer * layer,
    const QgsRectangle & rectangle
) 

Returns a feature iterator to get features overlapping a given rectangle within the provided layer.


function createMemoryLayer

static Q_INVOKABLE QgsVectorLayer * LayerUtils::createMemoryLayer (
    const QString & name,
    const QgsFields & fields=QgsFields(),
    Qgis::WkbType geometryType=Qgis::WkbType::NoGeometry,
    const QgsCoordinateReferenceSystem & crs=QgsCoordinateReferenceSystem()
) 

Creates a new memory layer using the specified parameters.

Parameters:

  • name layer name
  • fields fields for layer
  • geometryType optional layer geometry type
  • crs optional layer CRS for layers with geometry

function createOnlineElevationLayer

static QgsRasterLayer * LayerUtils::createOnlineElevationLayer () 

Creats an online raster elevation layer.


function defaultLabeling

static QgsAbstractVectorLayerLabeling * LayerUtils::defaultLabeling (
    QgsVectorLayer * layer,
    QgsTextFormat textFormat=QgsTextFormat()
) 

Returns the default vector layer labeling for a given layer and textFormat.


function defaultSymbol

static QgsSymbol * LayerUtils::defaultSymbol (
    QgsVectorLayer * layer,
    const QString & attachmentField=QString(),
    const QString & colorField=QString()
) 

Returns the default symbol for a given layer.

Parameters:

  • layer the vector layer used to create the default symbol

function deleteFeature

static Q_INVOKABLE bool LayerUtils::deleteFeature (
    QgsProject * project,
    QgsVectorLayer * layer,
    const QgsFeatureId fid,
    bool flushBuffer=true
) 

Deletes a vector layer feature, including related features tied to relationships.

Parameters:

  • project the project holding information on relationships
  • layer the layer from which the feature will be deleted
  • fid the feature ID to be deleted
  • flushBuffer set to TRUE to immediately save the edit buffer

function duplicateFeature

static Q_INVOKABLE QgsFeature LayerUtils::duplicateFeature (
    QgsVectorLayer * layer,
    QgsFeature feature
) 

Duplicates a given feature within the provided vector layer. If successful, the function will return the duplicated feature with attribute values saved updated to match what was saved into the layer dataset.


function fieldType

static Q_INVOKABLE QString LayerUtils::fieldType (
    const QgsField & field
) 

Returns the QVariant typeName of a field. This is a stable identifier (compared to the provider field name).


function hasMValue

static Q_INVOKABLE bool LayerUtils::hasMValue (
    QgsVectorLayer * layer
) 

Returns TRUE if the vector layer geometry has an M value.


function isAtlasCoverageLayer

static Q_INVOKABLE bool LayerUtils::isAtlasCoverageLayer (
    QgsVectorLayer * layer
) 

Returns TRUE if the vector layer is used as an atlas coverage layer in any of the print layouts of the currently opened project.

Parameters:

  • layer the vector layer to check against print layouts

function isFeatureAdditionLocked

static Q_INVOKABLE bool LayerUtils::isFeatureAdditionLocked (
    QgsMapLayer * layer
) 

Returns TRUE if the layer permission state prevents feature addition.


function loadRasterLayer

static Q_INVOKABLE QgsRasterLayer * LayerUtils::loadRasterLayer (
    const QString & uri,
    const QString & name=QString(),
    const QString & provider=QStringLiteral("gdal")
) 

Loads a raster layer.

Parameters:

  • uri the data source uri
  • name the layer name
  • provider the data provider name

function loadVectorLayer

static Q_INVOKABLE QgsVectorLayer * LayerUtils::loadVectorLayer (
    const QString & uri,
    const QString & name=QString(),
    const QString & provider=QStringLiteral("ogr")
) 

Loads a vector layer.

Parameters:

  • uri the data source uri
  • name the layer name
  • provider the data provider name

function memoryLayerFromJsonString

static Q_INVOKABLE QgsVectorLayer * LayerUtils::memoryLayerFromJsonString (
    const QString & name,
    const QString & string,
    const QgsCoordinateReferenceSystem & crs=QgsCoordinateReferenceSystem()
) 

Attempts to parse a GeoJSON string to a memory vector layer containing the collection of features. The geometry type will be taken from the first parsed feature.

Parameters:

  • name layer name
  • string the GeoJSON string
  • crs optional layer CRS for layers with geometry

function saveVectorLayerAs

static Q_INVOKABLE QString LayerUtils::saveVectorLayerAs (
    QgsVectorLayer * layer,
    const QString & filePath,
    const QString & driverName=QString(),
    const QString & filterExpression=QString()
) 

Saves a vector layer into an on-disk dataset a given path using the OGR provider.

Parameters:

  • layer the vector layer to save features from
  • filePath the file path where the dataset will be writen
  • driverName an optional OGR driver name (if left empty, the file path extension will drive the OGR driver)
  • filterExpression an optional filter expression used to save a subset of features from the layer (note that only the global, project, and layer expression context scopes are used)

Returns:

If successful, finalized file path will be returned, otherwise an empty string will be returned


function selectFeaturesInLayer

static Q_INVOKABLE void LayerUtils::selectFeaturesInLayer (
    QgsVectorLayer * layer,
    const QList< int > & fids,
    Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection
) 

Selects features in a layer This method is required since QML cannot perform the conversion of a feature ID to a QgsFeatureId

Parameters:

  • layer the vector layer
  • fids the list of feature IDs
  • behavior the selection behavior

function setDefaultLabeling

static void LayerUtils::setDefaultLabeling (
    QgsVectorLayer * layer,
    QgsProject * project=nullptr
) 

Sets the default labeling for a given layer.


function setDefaultRenderer

static void LayerUtils::setDefaultRenderer (
    QgsVectorLayer * layer,
    QgsProject * project=nullptr,
    const QString & attachmentField=QString(),
    const QString & colorField=QString()
) 

Sets the default symbology render for a given layer.



The documentation for this class was generated from the following file src/core/utils/layerutils.h