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 constructorMethods
Returns the approximate RAM usage of the feature, in bytes.
Lookup attribute value by attribute
name
.Returns the number of attributes attached to the feature.
Returns the feature's attributes.
Removes any geometry associated with the feature.
Deletes an attribute and its value.
Returns the feature's embedded symbology, or
None
if the feature has no embedded symbol.Utility method to get attribute index from name.
Returns the field map associated with the feature.
Returns the geometry associated with this feature.
Returns
True
if the feature has an associated geometry.Returns the feature ID for this feature.
Initialize this feature with the given number of fields.
Returns the validity of this feature.
Resizes the attributes attached to this feature by appending the specified
count
of NULL values to the end of the existing attributes.Resizes the attributes attached to this feature to the given number of fields.
Sets an attribute's value by field index.
Sets the feature's attributes.
Sets the feature's embedded
symbol
.Assigns a field map with the feature to allow attribute access by attribute name.
Set the feature's geometry.
Sets the feature
id
for this feature.Sets the validity of the feature.
Attributes
- 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.- Parameters
name (str) – The name of the attribute to get
- Returns
The value of the attribute
:raises KeyError if the field is not found
Note
Alternatively in Python: @code feature[name] @endcode
See also
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
:raises KeyError if the field is not found
Note
Alternatively in Python: @code feature[fieldIdx] @endcode
See also
- attributeCount(self) int ¶
Returns the number of attributes attached to the feature.
New in version 3.18.
- Return type
int
- attributes(self) object ¶
Returns the feature’s attributes.
See also
Note
Alternatively in Python: iterate feature, eg. @code [attr for attr in feature] @endcode
New in version 2.9.
- Return type
object
- clearGeometry(self)¶
Removes any geometry associated with the feature.
See also
See also
New in version 3.0.
- deleteAttribute(self, field: int)¶
Deletes an attribute and its value.
- Parameters
field (int) – the index of the field
:raises KeyError if the field is not found
Note
Alternatively in Python: @code del feature[field] @endcode
See also
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
:raises KeyError if attribute name could not be converted to index
Note
Alternatively in Python: @code del feature[name] @endcode
See also
- 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
- 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
- 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
See also
- Return type
- hasGeometry(self) bool ¶
Returns
True
if the feature has an associated geometry.See also
New in version 3.0..
- Return type
bool
- 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
See also
- 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
- 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.See also
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
See also
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()
).- 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
Note
Alternatively in Python: @code feature[field] = attr @endcode
See also
setAttribute(self, name: str, value: Any) Insert a value into attribute, by field
name
.Returns
False
if fieldname
could not be matched.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()
).- Parameters
name – The name of the field to set
value – The value to set
:raises KeyError if the attribute name could not be converted to an index
Note
Alternatively in Python: @code feature[name] = attr @endcode
See also
- 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
See also
- 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
New in version 2.9.
- 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
See also
setGeometry(self, geometry:
QgsAbstractGeometry
) Set the feature’sgeometry
.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 ) )
# 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
See also
New in version 3.6.
- setId(self, id: int)¶
Sets the feature
id
for this feature.- Parameters
id (int) – feature id
See also
- setValid(self, validity: bool)¶
Sets the validity of the feature.
- Parameters
validity (bool) – set to
True
if feature is valid
See also
- staticMetaObject = <PyQt5.QtCore.QMetaObject object>¶