QgsFeature class

The feature class encapsulates a single feature including its id, geometry and a list of field/values attributes.

Constructors, destructors, conversion operators

QgsFeature(QgsFeatureId id = QgsFeatureId())
Constructor for QgsFeature.
QgsFeature(const QgsFields& fields, QgsFeatureId id = QgsFeatureId())
Constructor for QgsFeature.
QgsFeature(const QgsFeature& rhs)
Copy constructor.
operator QVariant() const
Allows direct construction of QVariants from features.

Public functions

auto attribute(const QString& name) const -> QVariant
Lookup attribute value from attribute name.
auto attribute(int fieldIdx) const -> QVariant
Lookup attribute value from its index.
auto attributes() const -> QgsAttributes
Returns the feature's attributes.
void clearGeometry()
Removes any geometry associated with the feature.
void deleteAttribute(int field)
Deletes an attribute and its value.
auto deleteAttribute(const QString& name) -> bool
Removes an attribute value by field name.
auto fieldNameIndex(const QString& fieldName) const -> int
Utility method to get attribute index from name.
auto fields() const -> QgsFields
Returns the field map associated with the feature.
auto geometry() const -> QgsGeometry
Returns the geometry associated with this feature.
auto hasGeometry() const -> bool
Returns true if the feature has an associated geometry.
auto id() const -> QgsFeatureId
Gets the feature ID for this feature.
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
auto isValid() const -> bool
Returns the validity of this feature.
auto operator!=(const QgsFeature& other) const -> bool
Compares two features.
auto operator=(const QgsFeature& rhs) -> QgsFeature&
Assignment operator.
auto operator==(const QgsFeature& other) const -> bool
Compares two features.
auto setAttribute(int field, const QVariant& attr) -> bool
Set an attribute's value by field index.
auto setAttribute(const QString& name, const QVariant& value) -> bool
Insert a value into attribute.
void setAttributes(const QgsAttributes& attrs)
Sets the feature's attributes.
void setFields(const QgsFields& fields, bool initAttributes = false)
Assign a field map with the feature to allow attribute access by attribute name.
void setGeometry(const QgsGeometry& geometry)
Set the feature's geometry.
void setGeometry(std::unique_ptr<QgsAbstractGeometry> geometry)
Set the feature's geometry.
void setId(QgsFeatureId id)
Sets the feature ID for this feature.
void setValid(bool validity)
Sets the validity of the feature.

Function documentation

QgsFeature::QgsFeature(QgsFeatureId id = QgsFeatureId())

Constructor for QgsFeature.

Parameters
id feature id

QgsFeature::QgsFeature(const QgsFields& fields, QgsFeatureId id = QgsFeatureId())

Constructor for QgsFeature.

Parameters
fields feature's fields
id feature id

QVariant QgsFeature::attribute(const QString& name) const

Lookup attribute value from attribute name.

Parameters
name The name of the attribute to get
Returns The value of the attribute (C++: Invalid variant if no such name exists )

Field map must be associated using setFields() before this method can be used.

QVariant QgsFeature::attribute(int fieldIdx) const

Lookup attribute value from its index.

Parameters
fieldIdx The index of the attribute to get
Returns The value of the attribute (C++: Invalid variant if no such index exists )

Field map must be associated using setFields() before this method can be used.

QgsAttributes QgsFeature::attributes() const

Returns the feature's attributes.

Returns list of feature's attributes

void QgsFeature::clearGeometry()

Removes any geometry associated with the feature.

void QgsFeature::deleteAttribute(int field)

Deletes an attribute and its value.

Parameters
field the index of the field

bool QgsFeature::deleteAttribute(const QString& name)

Removes an attribute value by field name.

Parameters
name The name of the field to delete
Returns false if attribute name could not be converted to index (C++ only)

Field map must be associated using setFields() before this method can be used.

int QgsFeature::fieldNameIndex(const QString& fieldName) const

Utility method to get attribute index from name.

Parameters
fieldName name of field to get attribute index of
Returns -1 if field does not exist or field map is not associated.

Field map must be associated using setFields() before this method can be used.

QgsFields QgsFeature::fields() const

Returns the field map associated with the feature.

QgsGeometry QgsFeature::geometry() const

Returns the geometry associated with this feature.

If the feature has no geometry, an empty QgsGeometry object will be returned.

bool QgsFeature::hasGeometry() const

Returns true if the feature has an associated geometry.

QgsFeatureId QgsFeature::id() const

Gets the feature ID for this feature.

Returns feature ID

void QgsFeature::initAttributes(int fieldCount)

Initialize this feature with the given number of fields.

Parameters
fieldCount Number of fields to initialize

Discard any previously set attribute data.

bool QgsFeature::isValid() const

Returns the validity of this feature.

This is normally set by the provider to indicate some problem that makes the feature invalid or to indicate a null feature.

bool QgsFeature::setAttribute(int field, const QVariant& attr)

Set an attribute's value by field index.

Parameters
field the index of the field to set
attr the value of the attribute
Returns false, if the field index does not exist

The feature will be valid if it was successful.

bool QgsFeature::setAttribute(const QString& name, const QVariant& value)

Insert a value into attribute.

Parameters
name The name of the field to set
value The value to set
Returns false if attribute name could not be converted to index (C++ only)

Returns false if attribute name could not be converted to index. Field map must be associated using setFields() before this method can be used. The feature will be valid if it was successful

void QgsFeature::setAttributes(const QgsAttributes& attrs)

Sets the feature's attributes.

Parameters
attrs attribute list

The feature will be valid after.

void QgsFeature::setFields(const QgsFields& fields, bool initAttributes = false)

Assign a field map with the feature to allow attribute access by attribute name.

Parameters
fields The attribute fields which this feature holds
initAttributes If true, attributes are initialized. Clears any data previously assigned. C++: Defaults to false Python: Defaults to true

void QgsFeature::setGeometry(const QgsGeometry& geometry)

Set the feature's geometry.

Parameters
geometry new feature geometry

The feature will be valid after.

void QgsFeature::setGeometry(std::unique_ptr<QgsAbstractGeometry> geometry)

Set the feature's geometry.

Ownership of the geometry is transferred to the feature. The feature will be made valid after calling this method.

This method is a shortcut for calling: feature.setGeometry( QgsGeometry( geometry ) )

  • Example:

    # Sets a feature's geometry to a point geometry
    feature.setGeometry( QgsPoint( 210, 41 ) )
    print(feature.geometry())
    # output: <QgsGeometry: Point (210 41)>
    
    # Sets a feature's geometry to a line string
    feature.setGeometry( QgsLineString( [ QgsPoint( 210, 41 ), QgsPoint( 301, 55 ) ] ) )
    print(feature.geometry())
    # output: <QgsGeometry: LineString (210 41, 301 55)>

void QgsFeature::setId(QgsFeatureId id)

Sets the feature ID for this feature.

Parameters
id feature id

void QgsFeature::setValid(bool validity)

Sets the validity of the feature.

Parameters
validity set to true if feature is valid