Class: QgsVirtualLayerDefinition

Manipulates 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.

class qgis.core.QgsVirtualLayerDefinition[source]

Bases: object

__init__(filePath: str | None = '')

Constructor with an optional file path

Parameters:

filePath (Optional[str] = '')

__init__(a0: QgsVirtualLayerDefinition)
Parameters:

a0 (QgsVirtualLayerDefinition)

class SourceLayer[source]

Bases: object

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: Optional[str], ref: Optional[str]) Constructor variant to build a live layer reference

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

QgsVirtualLayerDefinition.SourceLayer(a0: QgsVirtualLayerDefinition.SourceLayer)

encoding(self) str[source]

Optional encoding for the provider

Return type:

str

isReferenced(self) bool[source]

Is it a live layer or not ?

Return type:

bool

name(self) str[source]

Name of the layer

Return type:

str

provider(self) str[source]

Provider key

Return type:

str

reference(self) str[source]

The reference (id) of the live layer

Return type:

str

source(self) str[source]

The source url used by the provider to build the layer

Return type:

str

addSource(self, name: str | None, ref: str | None)[source]

Add a live layer source layer

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

Parameters:
  • name (Optional[str])

  • ref (Optional[str])

fields(self) QgsFields[source]

Gets field definitions

Return type:

QgsFields

filePath(self) str[source]

Gets the file path. May be empty

Return type:

str

static fromUrl(url: QUrl) QgsVirtualLayerDefinition[source]

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[source]

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

Return type:

str

geometrySrid(self) int[source]

Gets the SRID of the geometry

Return type:

int

geometryWkbType(self) Qgis.WkbType[source]

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

Return type:

Qgis.WkbType

hasDefinedGeometry(self) bool[source]

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

Return type:

bool

hasReferencedLayers(self) bool[source]

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

Return type:

bool

hasSourceLayer(self, name: str | None) bool[source]

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

Parameters:

name (Optional[str])

Return type:

bool

isLazy(self) bool[source]

Returns the lazy mode.

Return type:

bool

Returns:

True if the loading is delayed, False otherwise.

See also

setLazy()

Added in version 3.2.

query(self) str[source]

Gets the SQL query

Return type:

str

setFields(self, fields: QgsFields)[source]

Sets field definitions

Parameters:

fields (QgsFields)

setFilePath(self, filePath: str | None)[source]

Sets the file path

Parameters:

filePath (Optional[str])

setGeometryField(self, geometryField: str | None)[source]

Sets the name of the geometry field

Parameters:

geometryField (Optional[str])

setGeometrySrid(self, srid: int)[source]

Sets the SRID of the geometry

Parameters:

srid (int)

setGeometryWkbType(self, t: Qgis.WkbType)[source]

Sets the type of the geometry

Parameters:

t (Qgis.WkbType)

setLazy(self, lazy: bool)[source]

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()

Added in version 3.2.

setQuery(self, query: str | None)[source]

Sets the SQL query

Parameters:

query (Optional[str])

setSubsetString(self, subsetString: str | None)[source]

Sets the subsetString

Added in version 3.16.

Parameters:

subsetString (Optional[str])

setUid(self, uid: str | None)[source]

Sets the name of the field with unique identifiers

Parameters:

uid (Optional[str])

sourceLayers(self) List[QgsVirtualLayerDefinition.SourceLayer]

Gets access to the source layers

Return type:

List[QgsVirtualLayerDefinition.SourceLayer]

subsetString(self) str[source]

Returns the subset string.

Added in version 3.16.

Return type:

str

toString(self) str[source]

Converts the definition into a string that can be read by the virtual layer provider

Return type:

str

toUrl(self) QUrl[source]

Convert the definition into a QUrl

Return type:

QUrl

uid(self) str[source]

Gets the name of the field with unique identifiers

Return type:

str