Class: QgsFeatureSource

An interface for objects which provide features via a getFeatures method.

Note

This is an abstract class, with methods which must be implemented by a subclass.

The following methods must be implemented: featureCount(), fields(), getFeatures(), sourceCrs(), sourceName(), wkbType()

Class Hierarchy

Inheritance diagram of qgis.core.QgsFeatureSource

Subclasses

QgsVectorLayer

Represents a vector layer which manages a vector based dataset.

QgsProcessingFeatureSource

QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource, modifying results according to the settings in a QgsProcessingContext.

QgsVectorDataProvider

Base class for vector data providers.

QgsVectorLayerSelectedFeatureSource

QgsFeatureSource subclass for the selected features from a QgsVectorLayer.

Enums

FeatureAvailability

Possible return value for QgsFeatureSource.hasFeatures() to determine if a source is empty.

SpatialIndexPresence

Enumeration of spatial index presence states.

Abstract Methods

featureCount

Returns the number of features contained in the source, or -1 if the feature count is unknown.

fields

Returns the fields associated with features in the source.

getFeatures

Returns an iterator for the features in the source.

sourceCrs

Returns the coordinate reference system for features in the source.

sourceName

Returns a friendly display name for the source.

wkbType

Returns the geometry type for features returned by this source.

Methods

materialize

Materializes a request (query) made against this feature source, by running it over the source and returning a new memory based vector layer containing the result.

Virtual Methods

In PyQGIS, only methods marked as virtual can be safely overridden in a Python subclass of QgsFeatureSource. See the FAQ for more details.

allFeatureIds

Returns a list of all feature IDs for features present in the source.

hasFeatures

Determines if there are any features available in the source.

hasSpatialIndex

Returns an enum value representing the presence of a valid spatial index on the source, if it can be determined.

maximumValue

Returns the maximum value for an attribute column or an invalid variant in case of error.

minimumValue

Returns the minimum value for an attribute column or an invalid variant in case of error.

sourceExtent

Returns the extent of all geometries from the source.

sourceExtent3D

Returns the 3D extent of all geometries from the source.

uniqueValues

Returns the set of unique values contained within the specified fieldIndex from this source.

class qgis.core.QgsFeatureSource[source]

Bases: object

class FeatureAvailability(*values)

Bases: IntEnum

Possible return value for QgsFeatureSource.hasFeatures() to determine if a source is empty.

It is implemented as a three-value logic, so it can return if there are features available for sure, if there are no features available for sure or if there might be features available but there is no guarantee for this.

Note

Prior to QGIS 3.36 this was available as QgsFeatureSource.FeatureAvailability

Added in version 3.36.

  • NoFeaturesAvailable: There are certainly no features available in this source

  • FeaturesAvailable: There is at least one feature available in this source

  • FeaturesMaybeAvailable: There may be features available in this source

class SpatialIndexPresence(*values)

Bases: IntEnum

Enumeration of spatial index presence states.

Note

Prior to QGIS 3.36 this was available as QgsFeatureSource.SpatialIndexPresence

Added in version 3.36.

  • Unknown: Spatial index presence cannot be determined, index may or may not exist

    Available as QgsFeatureSource.SpatialIndexUnknown in older QGIS releases.

  • NotPresent: No spatial index exists for the source

    Available as QgsFeatureSource.SpatialIndexNotPresent in older QGIS releases.

  • Present: A valid spatial index exists for the source

    Available as QgsFeatureSource.SpatialIndexPresent in older QGIS releases.

virtual allFeatureIds(self) Any[source]

Returns a list of all feature IDs for features present in the source.

Return type:

Any

abstract featureCount(self) int

Returns the number of features contained in the source, or -1 if the feature count is unknown.

Return type:

int

abstract fields(self) QgsFields[source]

Returns the fields associated with features in the source.

Return type:

QgsFields

abstract getFeatures(self, request: QgsFeatureRequest = QgsFeatureRequest()) QgsFeatureIterator[source]

Returns an iterator for the features in the source. An optional request can be used to optimise the returned iterator, eg by restricting the returned attributes or geometry.

Parameters:

request (QgsFeatureRequest = QgsFeatureRequest())

Return type:

QgsFeatureIterator

virtual hasFeatures(self) Qgis.FeatureAvailability[source]

Determines if there are any features available in the source.

Added in version 3.2.

Return type:

Qgis.FeatureAvailability

virtual hasSpatialIndex(self) Qgis.SpatialIndexPresence[source]

Returns an enum value representing the presence of a valid spatial index on the source, if it can be determined.

If QgsFeatureSource.SpatialIndexUnknown is returned then the presence of an index cannot be determined.

Added in version 3.10.1.

Return type:

Qgis.SpatialIndexPresence

materialize(self, request: QgsFeatureRequest, feedback: QgsFeedback | None = None) QgsVectorLayer | None[source]

Materializes a request (query) made against this feature source, by running it over the source and returning a new memory based vector layer containing the result. All settings from feature request will be honored.

If a subset of attributes has been set for the request, then only those selected fields will be present in the output layer.

The CRS for the output layer will match the input layer, unless QgsFeatureRequest.setDestinationCrs() has been called with a valid QgsCoordinateReferenceSystem. In this case the output layer will match the QgsFeatureRequest.destinationCrs() CRS.

The returned layer WKB type will match wkbType(), unless the QgsFeatureRequest.NoGeometry flag is set on the request. In that case the returned layer will not be a spatial layer.

An optional feedback argument can be used to cancel the materialization before it has fully completed.

The returned value is a new instance and the caller takes responsibility for its ownership.

Parameters:
Return type:

Optional[QgsVectorLayer]

virtual maximumValue(self, fieldIndex: int) Any[source]

Returns the maximum value for an attribute column or an invalid variant in case of error. The base class implementation uses a non-optimised approach of looping through all features in the source.

See also

minimumValue()

See also

uniqueValues()

Parameters:

fieldIndex (int)

Return type:

Any

virtual minimumValue(self, fieldIndex: int) Any[source]

Returns the minimum value for an attribute column or an invalid variant in case of error. The base class implementation uses a non-optimised approach of looping through all features in the source.

See also

maximumValue()

See also

uniqueValues()

Parameters:

fieldIndex (int)

Return type:

Any

abstract sourceCrs(self) QgsCoordinateReferenceSystem[source]

Returns the coordinate reference system for features in the source.

Return type:

QgsCoordinateReferenceSystem

virtual sourceExtent(self) QgsRectangle[source]

Returns the extent of all geometries from the source. The base class implementation uses a non-optimised approach of looping through all features in the source.

Return type:

QgsRectangle

virtual sourceExtent3D(self) QgsBox3D[source]

Returns the 3D extent of all geometries from the source. The base class implementation uses a non-optimised approach of looping through all features in the source.

Added in version 3.36.

Return type:

QgsBox3D

abstract sourceName(self) str[source]

Returns a friendly display name for the source. The returned value can be an empty string.

Return type:

str

virtual uniqueValues(self, fieldIndex: int, limit: int = -1) Set[Any]

Returns the set of unique values contained within the specified fieldIndex from this source. If specified, the limit option can be used to limit the number of returned values. The base class implementation uses a non-optimised approach of looping through all features in the source.

See also

minimumValue()

See also

maximumValue()

Parameters:
  • fieldIndex (int)

  • limit (int = -1)

Return type:

Set[Any]

abstract wkbType(self) Qgis.WkbType[source]

Returns the geometry type for features returned by this source.

Return type:

Qgis.WkbType