Skip to content

Class QfLayerUtils

ClassList > QfLayerUtils

Inherits the following classes: QObject

Public Functions

Type Name
QfLayerUtils (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 QfFeatureIterator createFeatureIterator (QgsVectorLayer * layer)
Q_INVOKABLE QfFeatureIterator createFeatureIteratorFromExpression (QgsVectorLayer * layer, const QString & expression)
Q_INVOKABLE QfFeatureIterator 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 QString guessFriendlyHeightField (QgsVectorLayer * layer)
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 QfLayerUtils

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

function uniqueValuesForVectorLayerFieldIndex

Q_INVOKABLE QSet< QVariant > QfLayerUtils::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 QfLayerUtils::addFeature (
    QgsVectorLayer * layer,
    QgsFeature feature
) 

Adds a feature into the layer.

Note:

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


function createBasemap

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

Creates an online basemap layer.


function createFeatureIterator

static Q_INVOKABLE QfFeatureIterator QfLayerUtils::createFeatureIterator (
    QgsVectorLayer * layer
) 

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


function createFeatureIteratorFromExpression

static Q_INVOKABLE QfFeatureIterator QfLayerUtils::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 QfFeatureIterator QfLayerUtils::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 * QfLayerUtils::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 * QfLayerUtils::createOnlineElevationLayer () 

Creats an online raster elevation layer.


function defaultLabeling

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

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


function defaultSymbol

static QgsSymbol * QfLayerUtils::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 QfLayerUtils::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 QfLayerUtils::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 QfLayerUtils::fieldType (
    const QgsField & field
) 

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


function guessFriendlyHeightField

static Q_INVOKABLE QString QfLayerUtils::guessFriendlyHeightField (
    QgsVectorLayer * layer
) 

Guesses the name of the field in layer most likely to carry a per-feature height/extrusion value, or an empty string when no suitable field is found.


function hasMValue

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

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


function isAtlasCoverageLayer

static Q_INVOKABLE bool QfLayerUtils::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 QfLayerUtils::isFeatureAdditionLocked (
    QgsMapLayer * layer
) 

Returns TRUE if the layer permission state prevents feature addition.


function loadRasterLayer

static Q_INVOKABLE QgsRasterLayer * QfLayerUtils::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 * QfLayerUtils::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 * QfLayerUtils::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 QfLayerUtils::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 QfLayerUtils::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 QfLayerUtils::setDefaultLabeling (
    QgsVectorLayer * layer,
    QgsProject * project=nullptr
) 

Sets the default labeling for a given layer.


function setDefaultRenderer

static void QfLayerUtils::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/qflayerutils.h