Class: QgsFeatureRequest

class qgis.core.QgsFeatureRequest

Bases: sip.wrapper

construct a default request: for all features get attributes and geometries

QgsFeatureRequest(fid: int) construct a request with feature ID filter

QgsFeatureRequest(fids: object) construct a request with feature ID filter

QgsFeatureRequest(rectangle: QgsRectangle) Construct a request with rectangle bounding box filter.

When a destination CRS is set using setDestinationCrs(), rectangle is expected to be in the same CRS as the destinationCrs(). Otherwise, rectangle should use the same CRS as the source layer/provider.

QgsFeatureRequest(expr: QgsExpression, context: QgsExpressionContext = QgsExpressionContext()) construct a request with a filter expression

QgsFeatureRequest(rh: QgsFeatureRequest) copy constructor

This class wraps a request for features to a vector layer (or directly its vector data provider). The request may apply a filter to fetch only a particular subset of features. Currently supported filters: - no filter - all features are returned - feature id - only feature that matches given feature id is returned - feature ids - only features that match any of the given feature ids are returned - filter expression - only features that match the given filter expression are returned

Additionally a spatial rectangle can be set in combination: Only features that intersect given rectangle should be fetched. For the sake of speed, the intersection is often done only using feature’s bounding box. There is a flag ExactIntersect that makes sure that only intersecting features will be returned.

For efficiency, it is also possible to tell provider that some data is not required: - NoGeometry flag - SubsetOfAttributes flag - SimplifyMethod for geometries to fetch

The options may be chained, e.g.:

QgsFeatureRequest().setFilterRect(QgsRectangle(0,0,1,1)).setFlags(QgsFeatureRequest.ExactIntersect)

Examples:

      # fetch all features:
      QgsFeatureRequest()
      # fetch all features, only one attribute
      QgsFeatureRequest().setSubsetOfAttributes(['myfield'], layer.fields())
      # fetch all features, without geometries
      QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry)
      # fetch only features from particular extent
      QgsFeatureRequest().setFilterRect(QgsRectangle(0,0,1,1))
      # fetch only one feature
      QgsFeatureRequest().setFilterFid(45)

.. autoautosummary:: qgis.core.QgsFeatureRequest
    :enums:
    :nosignatures:

.. autoautosummary:: qgis.core.QgsFeatureRequest
    :methods:
    :nosignatures:

.. autoautosummary:: qgis.core.QgsFeatureRequest
    :signals:
    :nosignatures:

.. autoautosummary:: qgis.core.QgsFeatureRequest
    :attributes:
ALL_ATTRIBUTES = '#!allattributes!#'
ExactIntersect = 4
FilterExpression = 2
FilterFid = 1
FilterFids = 3
FilterNone = 0
class FilterType

Bases: int

class Flag

Bases: int

class Flags

Bases: sip.wrapper

QgsFeatureRequest.Flags(Union[QgsFeatureRequest.Flags, QgsFeatureRequest.Flag]) QgsFeatureRequest.Flags(QgsFeatureRequest.Flags)

GeometryAbortOnInvalid = 2
GeometryNoCheck = 0
GeometrySkipInvalid = 1
class InvalidGeometryCheck

Bases: int

NoFlags = 0
NoGeometry = 1
class OrderBy

Bases: sip.wrapper

Create a new empty order by

QgsFeatureRequest.OrderBy(other: Iterable[QgsFeatureRequest.OrderByClause]) Create a new order by from a list of clauses

QgsFeatureRequest.OrderBy(QgsFeatureRequest.OrderBy)

Represents a list of OrderByClauses, with the most important first and the least important last.

New in version 2.14.

dump(self) → str

Dumps the content to an SQL equivalent syntax

Return type

str

list(self) → List[QgsFeatureRequest.OrderByClause]

Gets a copy as a list of OrderByClauses

This is only required in Python where the inheritance is not properly propagated and this makes it usable.

Return type

List[QgsFeatureRequest.OrderByClause]

load(self, elem: QDomElement)

Deserialize from XML

Parameters

elem (QDomElement) –

save(self, elem: QDomElement)

Serialize to XML

Parameters

elem (QDomElement) –

usedAttributeIndices(self, fields: QgsFields) → object

Returns a set of used, validated attribute indices

New in version 3.8.

Parameters

fields (QgsFields) –

Return type

object

usedAttributes(self) → Set[str]

Returns a set of used attributes

Note

The returned attributes names are NOT guaranteed to be valid.

Return type

Set[str]

class OrderByClause(expression: str, ascending: bool = True)

Bases: sip.wrapper

Creates a new OrderByClause for a QgsFeatureRequest

Parameters
  • expression (str) – The expression to use for ordering

  • ascending (bool = True) – If the order should be ascending (1,2,3) or descending (3,2,1) If the order is ascending, by default nulls are last If the order is descending, by default nulls are first

QgsFeatureRequest.OrderByClause(expression: str, ascending: bool, nullsfirst: bool) Creates a new OrderByClause for a QgsFeatureRequest

Parameters
  • expression – The expression to use for ordering

  • ascending – If the order should be ascending (1,2,3) or descending (3,2,1)

  • nullsfirst – If True, NULLS are at the beginning, if False, NULLS are at the end

QgsFeatureRequest.OrderByClause(expression: QgsExpression, ascending: bool = True) Creates a new OrderByClause for a QgsFeatureRequest

Parameters
  • expression – The expression to use for ordering

  • ascending – If the order should be ascending (1,2,3) or descending (3,2,1) If the order is ascending, by default nulls are last If the order is descending, by default nulls are first

QgsFeatureRequest.OrderByClause(expression: QgsExpression, ascending: bool, nullsfirst: bool) Creates a new OrderByClause for a QgsFeatureRequest

Parameters
  • expression – The expression to use for ordering

  • ascending – If the order should be ascending (1,2,3) or descending (3,2,1)

  • nullsfirst – If True, NULLS are at the beginning, if False, NULLS are at the end

QgsFeatureRequest.OrderByClause(QgsFeatureRequest.OrderByClause)

The OrderByClause class represents an order by clause for a QgsFeatureRequest.

It can be a simple field or an expression. Multiple order by clauses can be added to a QgsFeatureRequest to fine tune the behavior if a single field or expression is not enough to completely specify the required behavior.

If expression compilation is activated in the settings and the expression can be translated for the provider in question, it will be evaluated on provider side. If one of these two premises does not apply, the ordering will take place locally which results in increased memory and CPU usage.

If the ordering is done on strings, the order depends on the system’s locale if the local fallback implementation is used. The order depends on the server system’s locale and implementation if ordering is done on the server.

In case the fallback code needs to be used, a limit set on the request will be respected for the features returned by the iterator but internally all features will be requested from the provider.

New in version 2.14.

ascending(self) → bool

Order ascending

Return type

bool

Returns

If ascending order is requested

dump(self) → str

Dumps the content to an SQL equivalent

Return type

str

expression(self) → QgsExpression

The expression

Return type

QgsExpression

Returns

the expression

nullsFirst(self) → bool

Set if NULLS should be returned first

Return type

bool

Returns

if NULLS should be returned first

prepare(self, context: QgsExpressionContext) → bool

Prepare the expression with the given context.

New in version 3.0.

Parameters

context (QgsExpressionContext) –

Return type

bool

setAscending(self, ascending: bool)

Set if ascending order is requested

Parameters

ascending (bool) –

setNullsFirst(self, nullsFirst: bool)

Set if NULLS should be returned first

Parameters

nullsFirst (bool) –

SubsetOfAttributes = 2
acceptFeature(self, feature: QgsFeature) → bool

Check if a feature is accepted by this requests filter

Parameters

feature (QgsFeature) – The feature which will be tested

Return type

bool

Returns

True, if the filter accepts the feature

New in version 2.1.

addOrderBy(self, expression: str, ascending: bool = True) → QgsFeatureRequest

Adds a new OrderByClause, appending it as the least important one.

Parameters
  • expression (str) – The expression to use for ordering

  • ascending (bool = True) – If the order should be ascending (1,2,3) or descending (3,2,1) If the order is ascending, by default nulls are last If the order is descending, by default nulls are first

New in version 2.14.

addOrderBy(self, expression: str, ascending: bool, nullsfirst: bool) -> QgsFeatureRequest Adds a new OrderByClause, appending it as the least important one.

Parameters
  • expression – The expression to use for ordering

  • ascending – If the order should be ascending (1,2,3) or descending (3,2,1)

  • nullsfirst – If True, NULLS are at the beginning, if False, NULLS are at the end

New in version 2.14.

Return type

QgsFeatureRequest

combineFilterExpression(self, expression: str) → QgsFeatureRequest

Modifies the existing filter expression to add an additional expression filter. The filter expressions are combined using AND, so only features matching both the existing expression and the additional expression will be returned.

New in version 2.14.

Parameters

expression (str) –

Return type

QgsFeatureRequest

connectionTimeout(self) → int

Returns the timeout (in milliseconds) for how long we should wait for a connection if none is available from the pool at this moment. A negative value (which is set by default) will wait forever.

Note

Only works if the provider supports this option.

Deprecated since version Use: timeout() instead.

New in version 3.0.

Return type

int

destinationCrs(self) → QgsCoordinateReferenceSystem

Returns the destination coordinate reference system for feature’s geometries, or an invalid QgsCoordinateReferenceSystem if no reprojection will be done and all features will be left with their original geometry.

New in version 3.0.

Return type

QgsCoordinateReferenceSystem

disableFilter(self) → QgsFeatureRequest

Disables filter conditions. The spatial filter (filterRect) will be kept in place.

Return type

QgsFeatureRequest

Returns

The object the method is called on for chaining

New in version 2.12.

expressionContext(self) → QgsExpressionContext

Returns the expression context used to evaluate filter expressions.

New in version 2.12.

Return type

QgsExpressionContext

filterExpression(self) → QgsExpression

Returns the filter expression if set.

Return type

QgsExpression

filterFid(self) → int

Gets the feature ID that should be fetched.

Return type

int

filterFids(self) → object

Gets feature IDs that should be fetched.

Return type

object

filterRect(self) → QgsRectangle

Returns the rectangle from which features will be taken. If the returned rectangle is null, then no filter rectangle is set.

When a destination CRS is set using setDestinationCrs(), the rectangle will be in the same CRS as the destinationCrs(). Otherwise, the rectangle will use the same CRS as the source layer/provider.

See also

setFilterRect()

Return type

QgsRectangle

filterType(self) → QgsFeatureRequest.FilterType

Returns the filter type which is currently set on this request

Return type

QgsFeatureRequest.FilterType

Returns

Filter type

flags(self) → QgsFeatureRequest.Flags
Return type

QgsFeatureRequest.Flags

invalidGeometryCheck(self) → QgsFeatureRequest.InvalidGeometryCheck

Returns the invalid geometry checking behavior.

New in version 3.0.

Return type

QgsFeatureRequest.InvalidGeometryCheck

limit(self) → int

Returns the maximum number of features to request, or -1 if no limit set.

See also

setLimit()

New in version 2.14.

Return type

int

orderBy(self) → QgsFeatureRequest.OrderBy

Returns a list of order by clauses specified for this feature request.

New in version 2.14.

Return type

QgsFeatureRequest.OrderBy

requestMayBeNested(self) → bool

In case this request may be run nested within another already running iteration on the same connection, set this to True.

If this flag is True, this request will be able to make use of “spare” connections to avoid deadlocks.

For example, this should be set on requests that are issued from an expression function.

New in version 3.4.

Return type

bool

setConnectionTimeout(self, connectionTimeout: int) → QgsFeatureRequest

Sets the timeout (in milliseconds) for how long we should wait for a connection if none is available from the pool at this moment. A negative value (which is set by default) will wait forever.

Note

Only works if the provider supports this option.

Deprecated since version Use: setTimeout() instead.

New in version 3.0.

Parameters

connectionTimeout (int) –

Return type

QgsFeatureRequest

setDestinationCrs(self, crs: QgsCoordinateReferenceSystem, context: QgsCoordinateTransformContext) → QgsFeatureRequest

Sets the destination crs for feature’s geometries. If set, all geometries will be reprojected from their original coordinate reference system to this desired reference system. If crs is an invalid QgsCoordinateReferenceSystem then no reprojection will be done and all features will be left with their original geometry.

When a crs is set using setDestinationCrs(), then any filterRect() set on the request is expected to be in the same CRS as the destination CRS.

The feature geometry transformation to the destination CRS is performed after all filter expressions are tested and any virtual fields are calculated. Accordingly, any geometric expressions used in filterExpression() will be performed in the original source CRS. This ensures consistent results are returned regardless of the destination CRS. Similarly, virtual field values will be calculated using the original geometry in the source CRS, so these values are not affected by any destination CRS transform present in the feature request.

See also

destinationCrs()

New in version 3.0.

Parameters
Return type

QgsFeatureRequest

setExpressionContext(self, context: QgsExpressionContext) → QgsFeatureRequest

Sets the expression context used to evaluate filter expressions.

New in version 2.12.

Parameters

context (QgsExpressionContext) –

Return type

QgsFeatureRequest

setFilterExpression(self, expression: str) → QgsFeatureRequest

Set the filter expression. {QgsExpression}

Parameters

expression (str) – expression string

Return type

QgsFeatureRequest

setFilterFid(self, fid: int) → QgsFeatureRequest

Sets feature ID that should be fetched.

Parameters

fid (int) –

Return type

QgsFeatureRequest

setFilterFids(self, fids: object) → QgsFeatureRequest

Sets feature IDs that should be fetched.

Parameters

fids (object) –

Return type

QgsFeatureRequest

setFilterRect(self, rectangle: QgsRectangle) → QgsFeatureRequest

Sets the rectangle from which features will be taken. An empty rectangle removes the filter.

When a destination CRS is set using setDestinationCrs(), rectangle is expected to be in the same CRS as the destinationCrs(). Otherwise, rectangle should use the same CRS as the source layer/provider.

See also

filterRect()

Parameters

rectangle (QgsRectangle) –

Return type

QgsFeatureRequest

setFlags(self, flags: Union[QgsFeatureRequest.Flags, QgsFeatureRequest.Flag]) → QgsFeatureRequest

Sets flags that affect how features will be fetched

Parameters

flags (Union[QgsFeatureRequest.Flags) –

Return type

QgsFeatureRequest

setInvalidGeometryCallback(self, Callable[..., None]) → QgsFeatureRequest

Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid or GeometrySkipInvalid. This function will be called using the feature with invalid geometry as a parameter.

See also

invalidGeometryCallback()

New in version 3.0.

Return type

QgsFeatureRequest

setInvalidGeometryCheck(self, check: QgsFeatureRequest.InvalidGeometryCheck) → QgsFeatureRequest

Sets invalid geometry checking behavior.

Note

Invalid geometry checking is not performed when retrieving features directly from a QgsVectorDataProvider.

New in version 3.0.

Parameters

check (QgsFeatureRequest.InvalidGeometryCheck) –

Return type

QgsFeatureRequest

setLimit(self, limit: int) → QgsFeatureRequest

Set the maximum number of features to request.

Parameters

limit (int) – maximum number of features, or -1 to request all features.

See also

limit()

New in version 2.14.

Return type

QgsFeatureRequest

setNoAttributes(self) → QgsFeatureRequest

Set that no attributes will be fetched. To revert a call to setNoAttributes and fetch all or some available attributes, the SubsetOfAttributes flag should be removed from the request.

New in version 3.4.

Return type

QgsFeatureRequest

setOrderBy(self, orderBy: QgsFeatureRequest.OrderBy) → QgsFeatureRequest

Set a list of order by clauses.

New in version 2.14.

Parameters

orderBy (QgsFeatureRequest.OrderBy) –

Return type

QgsFeatureRequest

setRequestMayBeNested(self, requestMayBeNested: bool) → QgsFeatureRequest

In case this request may be run nested within another already running iteration on the same connection, set this to True.

If this flag is True, this request will be able to make use of “spare” connections to avoid deadlocks.

For example, this should be set on requests that are issued from an expression function.

New in version 3.4.

Parameters

requestMayBeNested (bool) –

Return type

QgsFeatureRequest

setSimplifyMethod(self, simplifyMethod: QgsSimplifyMethod) → QgsFeatureRequest

Set a simplification method for geometries that will be fetched

New in version 2.2.

Parameters

simplifyMethod (QgsSimplifyMethod) –

Return type

QgsFeatureRequest

setSubsetOfAttributes(self, attrs: Iterable[int]) → QgsFeatureRequest

Set a subset of attributes that will be fetched.

An empty attributes list indicates that no attributes will be fetched. To revert a call to setSubsetOfAttributes and fetch all available attributes, the SubsetOfAttributes flag should be removed from the request.

setSubsetOfAttributes(self, attrNames: Iterable[str], fields: QgsFields) -> QgsFeatureRequest Sets a subset of attributes by names that will be fetched

setSubsetOfAttributes(self, attrNames: Iterable[str], fields: QgsFields) -> QgsFeatureRequest Sets a subset of attributes by names that will be fetched

Parameters

attrs (Iterable[int]) –

Return type

QgsFeatureRequest

setTimeout(self, timeout: int) → QgsFeatureRequest

Sets the timeout (in milliseconds) for the maximum time we should wait during feature requests before a feature is returned. A negative value (which is set by default) will wait forever.

Note

Only works if the provider supports this option.

New in version 3.4.

Parameters

timeout (int) –

Return type

QgsFeatureRequest

setTransformErrorCallback(self, Callable[..., None]) → QgsFeatureRequest

Sets a callback function to use when encountering a transform error when iterating features and a destinationCrs() is set. This function will be called using the feature which encountered the transform error as a parameter.

See also

transformErrorCallback()

New in version 3.0.

Return type

QgsFeatureRequest

simplifyMethod(self) → QgsSimplifyMethod

Gets simplification method for geometries that will be fetched

New in version 2.2.

Return type

QgsSimplifyMethod

subsetOfAttributes(self) → List[int]

Returns the subset of attributes which at least need to be fetched

Return type

List[int]

Returns

A list of attributes to be fetched

timeout(self) → int

Returns the timeout (in milliseconds) for the maximum time we should wait during feature requests before a feature is returned. A negative value (which is set by default) will wait forever.

Note

Only works if the provider supports this option.

New in version 3.4.

Return type

int

transformContext(self) → QgsCoordinateTransformContext

Returns the transform context, for use when a destinationCrs() has been set and reprojection is required

See also

destinationCrs()

New in version 3.0.

Return type

QgsCoordinateTransformContext