Class: QgsVirtualLayerDefinition

class qgis.core.QgsVirtualLayerDefinition

Bases: sip.wrapper

Class to manipulate the definition of a virtual layer

It is used to extract parameters from an initial virtual layer definition as well as to store the complete, expanded definition once types have been detected.

QgsVirtualLayerDefinition(filePath: str = ‘’) Constructor with an optional file path

QgsVirtualLayerDefinition(QgsVirtualLayerDefinition)

Methods

addSource

Add a live layer source layer

fields

Gets field definitions

filePath

Gets the file path.

fromUrl

Constructor to build a definition from a QUrl The path part of the URL is extracted as well as the following optional keys: layer_ref=layer_id[:name] represents a live layer referenced by its ID.

geometryField

Gets the name of the geometry field.

geometrySrid

Gets the SRID of the geometry

geometryWkbType

Gets the type of the geometry QgsWkbTypes.NoGeometry to hide any geometry QgsWkbTypes.Unknown for unknown types

hasDefinedGeometry

Convenient method to test if the geometry is defined (not NoGeometry and not Unknown)

hasReferencedLayers

Convenience method to test whether the definition has referenced (live) layers

hasSourceLayer

Convenience method to test if a given source layer is part of the definition

isLazy

Returns the lazy mode.

query

Gets the SQL query

setFields

Sets field definitions

setFilePath

Sets the file path

setGeometryField

Sets the name of the geometry field

setGeometrySrid

Sets the SRID of the geometry

setGeometryWkbType

Sets the type of the geometry

setLazy

Sets the lazy mode.

setQuery

Sets the SQL query

setSubsetString

Sets the subsetString

setUid

Sets the name of the field with unique identifiers

sourceLayers

Gets access to the source layers

subsetString

Returns the subset string.

toString

Convert into a QString that can be read by the virtual layer provider

toUrl

Convert the definition into a QUrl

uid

Gets the name of the field with unique identifiers

class SourceLayer

Bases: sip.wrapper

A SourceLayer is either a reference to a live layer in the registry or all the parameters needed to load it (provider key, source, etc.)

QgsVirtualLayerDefinition.SourceLayer(name: str, ref: str) Constructor variant to build a live layer reference

QgsVirtualLayerDefinition.SourceLayer(name: str, source: str, provider: str, encoding: str) Constructor variant to build a layer with a provider and a source

QgsVirtualLayerDefinition.SourceLayer(QgsVirtualLayerDefinition.SourceLayer)

encoding(self) str

Optional encoding for the provider

Return type:

str

isReferenced(self) bool

Is it a live layer or not ?

Return type:

bool

name(self) str

Name of the layer

Return type:

str

provider(self) str

Provider key

Return type:

str

reference(self) str

The reference (id) of the live layer

Return type:

str

source(self) str

The source url used by the provider to build the layer

Return type:

str

addSource(self, name: str, ref: str)

Add a live layer source layer

addSource(self, name: str, source: str, provider: str, encoding: str = ‘’) Add a layer with a source, a provider and an encoding

Parameters:
  • name (str) –

  • ref (str) –

fields(self) QgsFields

Gets field definitions

Return type:

QgsFields

filePath(self) str

Gets the file path. May be empty

Return type:

str

fromUrl(url: QUrl) QgsVirtualLayerDefinition

Constructor to build a definition from a QUrl The path part of the URL is extracted as well as the following optional keys: layer_ref=layer_id[:name] represents a live layer referenced by its ID. An optional name can be given layer=provider:source[:name[:encoding]] represents a layer given by its provider key, its source url (URL-encoded). An optional name and encoding can be given geometry=column_name[:type:srid] gives the definition of the geometry column. Type can be either a WKB type code or a string (point, linestring, etc.) srid is an integer uid=column_name is the name of a column with unique integer values. nogeometry is a flag to force the layer to be a non-geometry layer query=sql represents the SQL query. Must be URL-encoded field=column_name:[int|real|text] represents a field with its name and its type subsetstring=subset_string represents the subsetstring

Parameters:

url (QUrl) –

Return type:

QgsVirtualLayerDefinition

geometryField(self) str

Gets the name of the geometry field. Empty if no geometry field

Return type:

str

geometrySrid(self) int

Gets the SRID of the geometry

Return type:

int

geometryWkbType(self) QgsWkbTypes.Type

Gets the type of the geometry QgsWkbTypes.NoGeometry to hide any geometry QgsWkbTypes.Unknown for unknown types

Return type:

QgsWkbTypes.Type

hasDefinedGeometry(self) bool

Convenient method to test if the geometry is defined (not NoGeometry and not Unknown)

Return type:

bool

hasReferencedLayers(self) bool

Convenience method to test whether the definition has referenced (live) layers

Return type:

bool

hasSourceLayer(self, name: str) bool

Convenience method to test if a given source layer is part of the definition

Parameters:

name (str) –

Return type:

bool

isLazy(self) bool

Returns the lazy mode.

Return type:

bool

Returns:

True if the loading is delayed, False otherwise.

See also

setLazy()

New in version 3.2.

query(self) str

Gets the SQL query

Return type:

str

setFields(self, fields: QgsFields)

Sets field definitions

Parameters:

fields (QgsFields) –

setFilePath(self, filePath: str)

Sets the file path

Parameters:

filePath (str) –

setGeometryField(self, geometryField: str)

Sets the name of the geometry field

Parameters:

geometryField (str) –

setGeometrySrid(self, srid: int)

Sets the SRID of the geometry

Parameters:

srid (int) –

setGeometryWkbType(self, t: QgsWkbTypes.Type)

Sets the type of the geometry

Parameters:

t (QgsWkbTypes.Type) –

setLazy(self, lazy: bool)

Sets the lazy mode. If lazy is True, then the loading is delayed until an explicit reloading of the layer.

Parameters:

lazy (bool) – True to delay the loading, False otherwise

See also

isLazy()

New in version 3.2.

setQuery(self, query: str)

Sets the SQL query

Parameters:

query (str) –

setSubsetString(self, subsetString: str)

Sets the subsetString

New in version 3.16.

Parameters:

subsetString (str) –

setUid(self, uid: str)

Sets the name of the field with unique identifiers

Parameters:

uid (str) –

sourceLayers(self) List[QgsVirtualLayerDefinition.SourceLayer]

Gets access to the source layers

Return type:

List[QgsVirtualLayerDefinition.SourceLayer]

subsetString(self) str

Returns the subset string.

New in version 3.16.

Return type:

str

toString(self) str

Convert into a QString that can be read by the virtual layer provider

Return type:

str

toUrl(self) QUrl

Convert the definition into a QUrl

Return type:

QUrl

uid(self) str

Gets the name of the field with unique identifiers

Return type:

str