QGIS API Documentation  3.14.0-Pi (9f7028fd23)
Public Member Functions | Properties | List of all members
QgsFeature Class Reference

#include <qgsfeature.h>

Public Member Functions

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

Properties

QgsAttributes attributes
 
QgsFields fields
 
QgsGeometry geometry
 
QgsFeatureId id
 

Detailed Description

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

Note
QgsFeature objects are implicitly shared.

Definition at line 55 of file qgsfeature.h.

Constructor & Destructor Documentation

◆ QgsFeature() [1/3]

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

Constructor for QgsFeature.

Parameters
idfeature id

Definition at line 37 of file qgsfeature.cpp.

◆ QgsFeature() [2/3]

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

Constructor for QgsFeature.

Parameters
fieldsfeature's fields
idfeature id

Definition at line 42 of file qgsfeature.cpp.

◆ QgsFeature() [3/3]

QgsFeature::QgsFeature ( const QgsFeature rhs)

Copy constructor.

Definition at line 49 of file qgsfeature.cpp.

◆ ~QgsFeature()

QgsFeature::~QgsFeature ( )
virtual

Definition at line 80 of file qgsfeature.cpp.

Member Function Documentation

◆ attribute() [1/2]

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

Lookup attribute value from attribute name.

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

Parameters
nameThe 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
Alternatively in Python:
feature[name]
See also
setFields

Definition at line 262 of file qgsfeature.cpp.

◆ attribute() [2/2]

QVariant QgsFeature::attribute ( int  fieldIdx) const

Lookup attribute value from its index.

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

Parameters
fieldIdxThe index of the attribute to get
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
Alternatively in Python:
feature[fieldIdx]
See also
setFields

Definition at line 254 of file qgsfeature.cpp.

◆ attributes()

QgsAttributes QgsFeature::attributes ( ) const

Returns the feature's attributes.

Returns
list of feature's attributes
See also
setAttributes
Note
Alternatively in Python: iterate feature, eg.
[attr for attr in feature]
Since
QGIS 2.9

◆ clearGeometry()

void QgsFeature::clearGeometry ( )

Removes any geometry associated with the feature.

See also
setGeometry()
hasGeometry()
Since
QGIS 3.0

Definition at line 151 of file qgsfeature.cpp.

◆ deleteAttribute() [1/2]

bool QgsFeature::deleteAttribute ( const QString &  name)

Removes an attribute value by field name.

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

Parameters
nameThe 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
Alternatively in Python:
del feature[name]
See also
setFields

Definition at line 243 of file qgsfeature.cpp.

◆ deleteAttribute() [2/2]

void QgsFeature::deleteAttribute ( int  field)

Deletes an attribute and its value.

Parameters
fieldthe index of the field
See also
setAttribute
Note
For Python: raises a KeyError exception if the field is not found
Alternatively in Python:
del feature[field]

Definition at line 95 of file qgsfeature.cpp.

◆ fieldNameIndex()

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

Utility method to get attribute index from name.

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

Parameters
fieldNamename of field to get attribute index of
Returns
-1 if field does not exist or field map is not associated.
See also
setFields

Definition at line 277 of file qgsfeature.cpp.

◆ fields()

QgsFields QgsFeature::fields ( ) const

Returns the field map associated with the feature.

See also
setFields

◆ geometry()

QgsGeometry QgsFeature::geometry ( ) const

Returns the geometry associated with this feature.

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

See also
hasGeometry()
setGeometry()

◆ hasGeometry()

bool QgsFeature::hasGeometry ( ) const

Returns true if the feature has an associated geometry.

See also
geometry()
Since
QGIS 3.0.

Definition at line 197 of file qgsfeature.cpp.

◆ id()

QgsFeatureId QgsFeature::id ( ) const

Gets the feature ID for this feature.

Returns
feature ID
See also
setId()

◆ initAttributes()

void QgsFeature::initAttributes ( int  fieldCount)

Initialize this feature with the given number of fields.

Discard any previously set attribute data.

Parameters
fieldCountNumber of fields to initialize

Definition at line 202 of file qgsfeature.cpp.

◆ isValid()

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.

See also
setValid

Definition at line 183 of file qgsfeature.cpp.

◆ operator QVariant()

QgsFeature::operator QVariant ( ) const
inline

Allows direct construction of QVariants from features.

Definition at line 544 of file qgsfeature.h.

◆ operator!=()

bool QgsFeature::operator!= ( const QgsFeature other) const

Compares two features.

Definition at line 75 of file qgsfeature.cpp.

◆ operator=()

QgsFeature & QgsFeature::operator= ( const QgsFeature rhs)

Assignment operator.

Definition at line 54 of file qgsfeature.cpp.

◆ operator==()

bool QgsFeature::operator== ( const QgsFeature other) const

Compares two features.

Definition at line 60 of file qgsfeature.cpp.

◆ setAttribute() [1/2]

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

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
nameThe name of the field to set
valueThe value to set
Returns
false if attribute name could not be converted to index (C++ only)
Note
For Python: raises a KeyError exception instead of returning false
Alternatively in Python:
feature[name] = attr
See also
setFields

Definition at line 231 of file qgsfeature.cpp.

◆ setAttribute() [2/2]

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

Set an attribute's value by field index.

The feature will be valid if it was successful.

Parameters
fieldthe index of the field to set
attrthe value of the attribute
Returns
false, if the field index does not exist
Note
For Python: raises a KeyError exception instead of returning false
Alternatively in Python:
feature[field] = attr
See also
setAttributes

Definition at line 211 of file qgsfeature.cpp.

◆ setAttributes()

void QgsFeature::setAttributes ( const QgsAttributes attrs)

Sets the feature's attributes.

The feature will be valid after.

Parameters
attrsattribute list
See also
setAttribute
attributes

Definition at line 127 of file qgsfeature.cpp.

◆ setFields()

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

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

Parameters
fieldsThe attribute fields which this feature holds
initAttributesIf true, attributes are initialized. Clears any data previously assigned. C++: Defaults to false Python: Defaults to true
See also
fields
Since
QGIS 2.9

Definition at line 162 of file qgsfeature.cpp.

◆ setGeometry() [1/2]

void QgsFeature::setGeometry ( const QgsGeometry geometry)

Set the feature's geometry.

The feature will be valid after.

Parameters
geometrynew feature geometry
See also
geometry()
clearGeometry()

Definition at line 137 of file qgsfeature.cpp.

◆ setGeometry() [2/2]

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)>
See also
geometry()
clearGeometry()
Since
QGIS 3.6

Definition at line 144 of file qgsfeature.cpp.

◆ setId()

void QgsFeature::setId ( QgsFeatureId  id)

Sets the feature ID for this feature.

Parameters
idfeature id
See also
id

Definition at line 112 of file qgsfeature.cpp.

◆ setValid()

void QgsFeature::setValid ( bool  validity)

Sets the validity of the feature.

Parameters
validityset to true if feature is valid
See also
isValid

Definition at line 188 of file qgsfeature.cpp.

Property Documentation

◆ attributes

QgsAttributes QgsFeature::attributes
readwrite

Definition at line 69 of file qgsfeature.h.

◆ fields

QgsFields QgsFeature::fields
readwrite

Definition at line 70 of file qgsfeature.h.

◆ geometry

QgsGeometry QgsFeature::geometry
readwrite

Definition at line 71 of file qgsfeature.h.

◆ id

QgsFeatureId QgsFeature::id
readwrite

Definition at line 68 of file qgsfeature.h.


The documentation for this class was generated from the following files:
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
QgsLineString
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
QgsGeometry
Definition: qgsgeometry.h:122