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)

.. rubric:: Enums

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

.. rubric:: Methods

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

.. rubric:: Signals

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

.. rubric:: Attributes

.. 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: object) 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

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.

load(self, elem: QDomElement)

Deserialize from XML

save(self, elem: QDomElement)

Serialize to XML

usedAttributes(self) → Set[str]

Returns a set of used attributes

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

Bases: sip.wrapper

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

Returns:If ascending order is requested
dump(self) → str

Dumps the content to an SQL equivalent

expression(self) → QgsExpression

The expression

Returns:the expression
nullsFirst(self) → bool

Set if NULLS should be returned first

Returns:if NULLS should be returned first
prepare(self, context: QgsExpressionContext) → bool

Prepare the expression with the given context.

New in version 3.0.

setAscending(self, ascending: bool)

Set if ascending order is requested

setNullsFirst(self, nullsFirst: bool)

Set if NULLS should be returned first

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

Check if a feature is accepted by this requests filter

Parameters:feature – The feature which will be tested
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 – 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

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.

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.

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.

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.

disableFilter(self) → QgsFeatureRequest

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

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.

filterExpression(self) → QgsExpression

Returns the filter expression if set.

filterFid(self) → int

Gets the feature ID that should be fetched.

filterFids(self) → object

Gets feature IDs that should be fetched.

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

filterType(self) → QgsFeatureRequest.FilterType

Returns the filter type which is currently set on this request

Returns:Filter type
flags(self) → QgsFeatureRequest.Flags
invalidGeometryCheck(self) → QgsFeatureRequest.InvalidGeometryCheck

Returns the invalid geometry checking behavior.

New in version 3.0.

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.

orderBy(self) → QgsFeatureRequest.OrderBy

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

New in version 2.14.

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.

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.

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.

setExpressionContext(self, context: QgsExpressionContext) → QgsFeatureRequest

Sets the expression context used to evaluate filter expressions.

New in version 2.12.

setFilterExpression(self, expression: str) → QgsFeatureRequest

Set the filter expression. {QgsExpression}

Parameters:expression – expression string
setFilterFid(self, fid: int) → QgsFeatureRequest

Sets feature ID that should be fetched.

setFilterFids(self, fids: object) → QgsFeatureRequest

Sets feature IDs that should be fetched.

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

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

Sets flags that affect how features will be fetched

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.

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.

setLimit(self, limit: int) → QgsFeatureRequest

Set the maximum number of features to request.

Parameters:limit – maximum number of features, or -1 to request all features.

See also

limit()

New in version 2.14.

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.

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

Set a list of order by clauses.

New in version 2.14.

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.

setSimplifyMethod(self, simplifyMethod: QgsSimplifyMethod) → QgsFeatureRequest

Set a simplification method for geometries that will be fetched

New in version 2.2.

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

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.

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.

simplifyMethod(self) → QgsSimplifyMethod

Gets simplification method for geometries that will be fetched

New in version 2.2.

subsetOfAttributes(self) → List[int]

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

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.

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.