Skip to content

Class LayerUtils

ClassList > LayerUtils

Inherits the following classes: QObject

Public Functions

Type Name
LayerUtils (QObject * parent=nullptr)

Public Static Functions

Type Name
Q_INVOKABLE bool addFeature (QgsVectorLayer * layer, QgsFeature feature)
QgsMapLayer * createBasemap (const QString & style=QString())
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())
bool deleteFeature (QgsProject * project, QgsVectorLayer * layer, const QgsFeatureId fid, bool shouldWriteChanges=true)
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 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
) 

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 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 bool LayerUtils::deleteFeature (
    QgsProject * project,
    QgsVectorLayer * layer,
    const QgsFeatureId fid,
    bool shouldWriteChanges=true
) 

function duplicateFeature

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