Class: QgsFeature

class qgis.core.QgsFeature

Bases: sip.wrapper

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

Note

QgsFeature objects are implicitly shared.

QgsFeature(id: int = FID_NULL) Constructor for QgsFeature

Parameters:

id – unique feature ID

QgsFeature(fields: QgsFields, id: int = FID_NULL) Constructor for QgsFeature

Parameters:
  • fields – feature’s fields

  • id – unique feature ID

QgsFeature(rhs: QgsFeature) Copy constructor

Methods

approximateMemoryUsage

Returns the approximate RAM usage of the feature, in bytes.

attribute

Lookup attribute value by attribute name.

attributeCount

Returns the number of attributes attached to the feature.

attributeMap

Returns the feature's attributes as a map of field name to value.

attributes

Returns the feature's attributes.

clearGeometry

Removes any geometry associated with the feature.

deleteAttribute

Clear's an attribute's value by its index.

embeddedSymbol

Returns the feature's embedded symbology, or None if the feature has no embedded symbol.

fieldNameIndex

Utility method to get attribute index from name.

fields

Returns the field map associated with the feature.

geometry

Returns the geometry associated with this feature.

hasGeometry

Returns True if the feature has an associated geometry.

id

Returns the feature ID for this feature.

initAttributes

Initialize this feature with the given number of fields.

isUnsetValue

Returns True if the attribute at the specified index is an unset value.

isValid

Returns the validity of this feature.

padAttributes

Resizes the attributes attached to this feature by appending the specified count of NULL values to the end of the existing attributes.

resizeAttributes

Resizes the attributes attached to this feature to the given number of fields.

setAttribute

Sets an attribute's value by field index.

setAttributes

Sets the feature's attributes.

setEmbeddedSymbol

Sets the feature's embedded symbol.

setFields

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

setGeometry

Set the feature's geometry.

setId

Sets the feature id for this feature.

setValid

Sets the validity of the feature.

Attributes

staticMetaObject

approximateMemoryUsage(self) int

Returns the approximate RAM usage of the feature, in bytes.

This method takes into account the size of variable elements (strings, geometry, …), but the value returned should be considered as a lower bound estimation.

New in version 3.16.

Return type:

int

attribute(self, name: str) object

Lookup attribute value by attribute name.

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

Alternatively, in Python it is possible to directly retrieve a field’s value via the field’s name:

fields = QgsFields()
fields.append(QgsField('my_id', QVariant.Int))
fields.append(QgsField('name', QVariant.String))
feature = QgsFeature(fields)
feature.setAttributes([11, 'my feature'])

# print the "name" field value
print(feature['name'])
# print the "my_id" field value
print(feature['my_id'])
Parameters:

name (str) – The name of the attribute to get

Returns:

The value of the attribute

Raises:

KeyError – if the field is not found

See also

setFields()

attribute(self, fieldIdx: int) -> object Lookup attribute value from its index.

Alternatively, in Python it is possible to directly retrieve a field’s value via its index:

feature = QgsFeature()
feature.setAttributes([11, 'my feature', 55.5])

# will print 11
print(feature[0])

# will print 'my feature'
print(feature[1])

# will print 55.5
print(feature[2])
Parameters:

fieldIdx – The index of the attribute to get

Return type:

object

Returns:

The value of the attribute

Raises:

KeyError – if the field is not found

See also

setFields()

attributeCount(self) int

Returns the number of attributes attached to the feature.

New in version 3.18.

Return type:

int

attributeMap(self) Dict[str, object | None]

Returns the feature’s attributes as a map of field name to value.

Note

The fields definition must be associated with the feature using setFields() before this method can be used.

Raises:

ValueError – if the field definition is unset or the size of the fields does not match the size of the feature’s attributes()

See also

attributes()

See also

setAttributes()

New in version 3.22.2.

Return type:

Dict[str, Optional[object]]

attributes(self) object

Returns the feature’s attributes.

Alternatively, in Python it is possible to directly iterate over a feature in order to retrieve its attributes:

feature = QgsFeature()
feature.setAttributes([11, 'string value', 55.5])
for attr in feature:
    print(attr)

See also

setAttributes()

See also

attributeMap()

Return type:

object

clearGeometry(self)

Removes any geometry associated with the feature.

See also

setGeometry()

See also

hasGeometry()

deleteAttribute(self, field: int)

Clear’s an attribute’s value by its index.

Parameters:

field (int) – the index of the field

Alternatively, in Python it is possible to directly del an attribute via its index:

feature = QgsFeature()
feature.setAttributes([11, 'my feature', 55.5])

# will print [11, 'my feature', 55.5]
print(feature.attributes())

# clear the second attribute
del feature[1]

# will now print [11, NONE]
print(feature.attributes())
Raises:

KeyError – if the field is not found

See also

setAttribute()

deleteAttribute(self, name: str) -> bool Clear’s an attribute’s value by its field name.

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

Alternatively, in Python it is possible to directly del an attribute via its name:

fields = QgsFields()
fields.append(QgsField('my_id', QVariant.Int))
fields.append(QgsField('name', QVariant.String))

feature = QgsFeature(fields)
feature.setAttributes([11, 'my feature'])

# will print [11, 'my feature']
print(feature.attributes())

# clear the 'name' attribute
del feature['name']

# will now print [11, NULL]
print(feature.attributes())
Parameters:

name – The name of the field to clear

Raises:

KeyError – if attribute name could not be matched.

See also

setFields()

embeddedSymbol(self) QgsSymbol

Returns the feature’s embedded symbology, or None if the feature has no embedded symbol.

New in version 3.20.

Return type:

QgsSymbol

fieldNameIndex(self, fieldName: str) int

Utility method to get attribute index from name.

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

Parameters:

fieldName (str) – name of field to get attribute index of

Return type:

int

Returns:

-1 if field does not exist or field map is not associated.

See also

setFields()

fields(self) QgsFields

Returns the field map associated with the feature.

See also

setFields()

Return type:

QgsFields

geometry(self) QgsGeometry

Returns the geometry associated with this feature. If the feature has no geometry, an empty QgsGeometry object will be returned.

See also

hasGeometry()

See also

setGeometry()

Return type:

QgsGeometry

hasGeometry(self) bool

Returns True if the feature has an associated geometry.

See also

geometry()

Return type:

bool

id(self) int

Returns the feature ID for this feature.

See also

setId()

Return type:

int

initAttributes(self, fieldCount: int)

Initialize this feature with the given number of fields.

Discards any previously set attribute data.

Parameters:

fieldCount (int) – Number of fields to initialize

isUnsetValue(self, fieldIdx: int) bool

Returns True if the attribute at the specified index is an unset value.

Raises:

KeyError – if the field is not found

New in version 3.28.

Parameters:

fieldIdx (int) –

Return type:

bool

isValid(self) bool

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.

See also

setValid()

Return type:

bool

padAttributes(self, count: int)

Resizes the attributes attached to this feature by appending the specified count of NULL values to the end of the existing attributes.

New in version 3.18.

Parameters:

count (int) –

resizeAttributes(self, fieldCount: int)

Resizes the attributes attached to this feature to the given number of fields.

If the new fieldCount is greater than the original number of fields then the additional attributes will be filled with NULL values. All existing attributes will remain unchanged.

If the new fieldCount is less than the original number of fields then the unwanted values will be discarded from the end of the existing attributes.

See also

initAttributes()

See also

padAttributes()

New in version 3.18.

Parameters:

fieldCount (int) –

setAttribute(self, field: int, attr: Any) bool

Sets an attribute’s value by field index.

If the attribute was successfully set then the feature will be automatically marked as valid (see isValid()).

Alternatively, in Python it is possible to directly set a field’s value via the field’s index:

fields = QgsFields()
fields.append(QgsField('my_id', QVariant.Int))
fields.append(QgsField('name', QVariant.String))
feature = QgsFeature(fields)

# set the "name" field value
feature[1] = "my name"
# set the "my_id" field value
feature[0] = 55
Parameters:
  • field (int) – the index of the field to set

  • attr (Any) – the value of the attribute

Raises:

KeyError – if the field index does not exist

See also

setAttributes()

setAttribute(self, name: str, value: Any) Insert a value into attribute, by field name.

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

Calling this method will automatically set the feature as valid (see isValid()).

Alternatively, in Python it is possible to directly set a field’s value via the field’s name:

fields = QgsFields()
fields.append(QgsField('my_id', QVariant.Int))
fields.append(QgsField('name', QVariant.String))
feature = QgsFeature(fields)

# set the "name" field value
feature['name'] = "my name"
# set the "my_id" field value
feature['my_id'] = 55
Parameters:
  • name – The name of the field to set

  • value – The value to set

Raises:

KeyError – if the attribute name could not could not be matched.

See also

setFields()

Return type:

bool

setAttributes(self, attrs: object)

Sets the feature’s attributes.

Calling this method will automatically set the feature as valid (see isValid()).

The number of provided attributes need to exactly match the number of the feature’s fields.

Parameters:

attrs (object) – List of attribute values

Warning

If the number of provided attributes does not exactly match the number of the feature’s fields then it will not be possible to add this feature to the corresponding data provider.

See also

setAttribute()

See also

attributes()

setEmbeddedSymbol(self, symbol: QgsSymbol)

Sets the feature’s embedded symbol.

Ownership of symbol is transferred to the feature.

New in version 3.20.

Parameters:

symbol (QgsSymbol) –

setFields(self, fields: QgsFields, initAttributes: bool = True)

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

Parameters:
  • fields (QgsFields) – The attribute fields which this feature holds

  • initAttributes (bool = True) – If True, attributes are initialized. Clears any data previously assigned.

See also

fields()

setGeometry(self, geometry: QgsGeometry)

Set the feature’s geometry.

Calling this method will automatically set the feature as valid (see isValid()).

Parameters:

geometry (QgsGeometry) – new feature geometry

See also

geometry()

See also

clearGeometry()

setGeometry(self, geometry: QgsAbstractGeometry) Set the feature’s geometry.

Ownership of the geometry is transferred to the feature.

Calling this method will automatically set the feature as valid (see isValid()).

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

See also

geometry()

See also

clearGeometry()

New in version 3.6.

setId(self, id: int)

Sets the feature id for this feature.

Parameters:

id (int) – feature id

See also

id()

Warning

Feature IDs will be automatically changed whenever a feature is added to vector layer or data provider. This method is not designed to allow a specific feature ID to be assigned to a feature which will be added to a layer or data provider, and the results will be unpredictable

setValid(self, validity: bool)

Sets the validity of the feature.

Parameters:

validity (bool) – set to True if feature is valid

See also

isValid()

staticMetaObject = <PyQt5.QtCore.QMetaObject object>