Class: QgsFeature

class qgis.core.QgsFeature(id: int = 0)

Bases: sip.wrapper

Constructor for QgsFeature

Parameters

id (int = 0) – feature id

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

Parameters
  • fields – feature’s fields

  • id – feature id

QgsFeature(rhs: QgsFeature) Copy constructor

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

Note

QgsFeature objects are implicitly shared.

Methods

attribute

Lookup attribute value from attribute name.

attributes

Returns the feature’s attributes.

clearGeometry

Removes any geometry associated with the feature.

deleteAttribute

Deletes an attribute and its value.

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

Gets the feature ID for this feature.

initAttributes

Initialize this feature with the given number of fields.

isValid

Returns the validity of this feature.

setAttribute

Set an attribute’s value by field index.

setAttributes

Sets the feature’s attributes.

setFields

Assign 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

attribute(self, name: str) → object

Lookup attribute value from attribute name. Field map must be associated using setFields() before this method can be used.

Parameters

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

Returns

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

Note

For Python: raises a KeyError exception if the field is not found

Note

Alternatively in Python: @code feature[name] @endcode

See also

setFields()

attribute(self, fieldIdx: int) -> object Lookup attribute value from its index. Field map must be associated using setFields() before this method can be used.

Parameters

fieldIdx – The index of the attribute to get

Return type

object

Returns

The value of the attribute (C++: Invalid variant if no such index exists )

Note

For Python: raises a KeyError exception if the field is not found

Note

Alternatively in Python: @code feature[fieldIdx] @endcode

See also

setFields()

attributes(self) → object

Returns the feature’s attributes.

Return type

object

Returns

list of feature’s attributes

See also

setAttributes()

Note

Alternatively in Python: iterate feature, eg. @code [attr for attr in feature] @endcode

New in version 2.9.

clearGeometry(self)

Removes any geometry associated with the feature.

See also

setGeometry()

See also

hasGeometry()

New in version 3.0.

deleteAttribute(self, field: int)

Deletes an attribute and its value.

Parameters

field (int) – the index of the field

See also

setAttribute()

Note

For Python: raises a KeyError exception if the field is not found

Note

Alternatively in Python: @code del feature[field] @endcode

deleteAttribute(self, name: str) -> bool Removes an attribute value by field name. Field map must be associated using setFields() before this method can be used.

Parameters

name – The name of the field to delete

Returns

false if attribute name could not be converted to index (C++ only)

Note

For Python: raises a KeyError exception instead of returning false

Note

Alternatively in Python: @code del feature[name] @endcode

See also

setFields()

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

New in version 3.0..

Return type

bool

id(self) → int

Gets the feature ID for this feature.

Return type

int

Returns

feature ID

See also

setId()

initAttributes(self, fieldCount: int)

Initialize this feature with the given number of fields. Discard any previously set attribute data.

Parameters

fieldCount (int) – Number of fields to initialize

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

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

Set an attribute’s value by field index. The feature will be valid if it was successful.

Parameters
  • field (int) – the index of the field to set

  • attr (Any) – the value of the attribute

Returns

false, if the field index does not exist

Note

For Python: raises a KeyError exception instead of returning false

Note

Alternatively in Python: @code feature[field] = attr @endcode

See also

setAttributes()

setAttribute(self, name: str, value: Any) Insert a value into attribute. 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

Parameters
  • name – The name of the field to set

  • value – The value to set

Return type

bool

Returns

false if attribute name could not be converted to index (C++ only)

Note

For Python: raises a KeyError exception instead of returning false

Note

Alternatively in Python: @code feature[name] = attr @endcode

See also

setFields()

setAttributes(self, attrs: object)

Sets the feature’s attributes. The feature will be valid after.

Parameters

attrs (object) – attribute list

See also

setAttribute()

See also

attributes()

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

Assign 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. C++: Defaults to false Python: Defaults to true

See also

fields()

New in version 2.9.

setGeometry(self, geometry: QgsGeometry)

Set the feature’s geometry. The feature will be valid after.

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. The feature will be made valid after calling this method.

This method is a shortcut for calling: .. code-block:: python

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.4.3.

setId(self, id: int)

Sets the feature ID for this feature.

Parameters

id (int) – feature id

See also

id()

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>