QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
Public Member Functions | List of all members
QgsFeature Class Reference

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

#include <qgsfeature.h>

Public Member Functions

 QgsFeature (QgsFeatureId id=QgsFeatureId())
 Constructor for QgsFeature. More...
 
 QgsFeature (const QgsFields &fields, QgsFeatureId id=QgsFeatureId())
 Constructor for QgsFeature. More...
 
 QgsFeature (const QgsFeature &rhs)
 Copy constructor. More...
 
virtual ~QgsFeature ()
 Destructor. More...
 
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...
 
const QgsGeometryconstGeometry () const
 Gets a const pointer to the geometry object associated with this feature. More...
 
void deleteAttribute (int field)
 Deletes an attribute and its value. More...
 
bool deleteAttribute (const QString &name)
 Removes an attribute value by field name. More...
 
int fieldNameIndex (const QString &fieldName) const
 Utility method to get attribute index from name. More...
 
const QgsFieldsfields () const
 Returns the field map associated with the feature. More...
 
QgsGeometrygeometry ()
 Get the geometry object associated with this feature. More...
 
Q_DECL_DEPRECATED QgsGeometrygeometryAndOwnership ()
 Get the geometry object associated with this feature, and transfer ownership of the geometry to the caller. More...
 
QgsFeatureId id () const
 Get 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...
 
QgsFeatureoperator= (QgsFeature const &rhs)
 Assignment operator. More...
 
bool setAttribute (int field, const QVariant &attr)
 Set an attribute's value by field index. More...
 
bool setAttribute (const QString &name, const QVariant &value)
 Insert a value into attribute. More...
 
void setAttributes (const QgsAttributes &attrs)
 Sets the feature's attributes. More...
 
void setFeatureId (QgsFeatureId id)
 Sets the feature ID for this feature. More...
 
Q_DECL_DEPRECATED void setFields (const QgsFields *fields, bool initAttributes=false)
 Assign a field map with the feature to allow attribute access by attribute name. 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 &geom)
 Set this feature's geometry from another QgsGeometry object. More...
 
void setGeometry (QgsGeometry *geom)
 Set this feature's geometry from a QgsGeometry pointer. More...
 
Q_DECL_DEPRECATED void setGeometryAndOwnership (unsigned char *geom, int length)
 Set this feature's geometry from WKB. More...
 
void setValid (bool validity)
 Sets the validity of the feature. More...
 

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.
Author
Gary E.Sherman

Definition at line 187 of file qgsfeature.h.

Constructor & Destructor Documentation

◆ QgsFeature() [1/3]

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

Constructor for QgsFeature.

Parameters
idfeature id

Definition at line 32 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 37 of file qgsfeature.cpp.

◆ QgsFeature() [3/3]

QgsFeature::QgsFeature ( const QgsFeature rhs)

Copy constructor.

Definition at line 44 of file qgsfeature.cpp.

◆ ~QgsFeature()

QgsFeature::~QgsFeature ( )
virtual

Destructor.

Definition at line 55 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 field is not found
See also
setFields

Definition at line 271 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 field is not found
See also
setFields

Definition at line 263 of file qgsfeature.cpp.

◆ attributes()

QgsAttributes QgsFeature::attributes ( ) const

Returns the feature's attributes.

attributes method.

Returns
list of feature's attributes
See also
setAttributes
Note
added in QGIS 2.9

Definition at line 110 of file qgsfeature.cpp.

◆ constGeometry()

const QgsGeometry * QgsFeature::constGeometry ( ) const

Gets a const pointer to the geometry object associated with this feature.

If the geometry is not going to be modified than this method is preferable to the non-const geometry method.

Note
this is a temporary method for 2.x release cycle. Will be removed in QGIS 3.0.
Returns
const pointer to feature's geometry
See also
geometry
geometryAndOwnership
setGeometry
Note
added in QGIS 2.9
will be removed in QGIS 3.0

Definition at line 82 of file qgsfeature.cpp.

◆ deleteAttribute() [1/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

Definition at line 70 of file qgsfeature.cpp.

◆ deleteAttribute() [2/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
See also
setFields

Definition at line 252 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 286 of file qgsfeature.cpp.

◆ fields()

const QgsFields * QgsFeature::fields ( ) const

Returns the field map associated with the feature.

See also
setFields TODO: QGIS 3 - return value, not pointer

Definition at line 188 of file qgsfeature.cpp.

◆ geometry()

QgsGeometry * QgsFeature::geometry ( )

Get the geometry object associated with this feature.

If the geometry is not going to be modified than calling the const constGeometry method is preferable as it avoids a potentially expensive detach operation.

It is possible to modify the geometry in place but this will be removed in 3.0 and therefore setGeometry should be called explicitly.

Note
will be modified to return by value in QGIS 3.0: QgsGeometry geometry() const;
Returns
pointer to feature's geometry
See also
constGeometry
geometryAndOwnership
setGeometry

Definition at line 76 of file qgsfeature.cpp.

◆ geometryAndOwnership()

QgsGeometry * QgsFeature::geometryAndOwnership ( )

Get the geometry object associated with this feature, and transfer ownership of the geometry to the caller.

The caller assumes responsibility for the QgsGeometry*'s destruction.

Returns
pointer to feature's geometry
See also
geometry
setGeometry
Deprecated:
use constGeometry() instead

Definition at line 87 of file qgsfeature.cpp.

◆ id()

QgsFeatureId QgsFeature::id ( ) const

Get the feature ID for this feature.

Returns
feature ID
See also
setFeatureId

Definition at line 65 of file qgsfeature.cpp.

◆ 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 213 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 199 of file qgsfeature.cpp.

◆ operator QVariant()

QgsFeature::operator QVariant ( ) const
inline

Allows direct construction of QVariants from features.

Definition at line 417 of file qgsfeature.h.

◆ operator=()

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

Assignment operator.

Definition at line 49 of file qgsfeature.cpp.

◆ setAttribute() [1/2]

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

Set an attribute's value by field index.

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
See also
setAttributes

Definition at line 222 of file qgsfeature.cpp.

◆ setAttribute() [2/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.

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
See also
setFields

Definition at line 241 of file qgsfeature.cpp.

◆ setAttributes()

void QgsFeature::setAttributes ( const QgsAttributes attrs)

Sets the feature's attributes.

Parameters
attrsattribute list
See also
setAttribute
attributes

Definition at line 115 of file qgsfeature.cpp.

◆ setFeatureId()

void QgsFeature::setFeatureId ( QgsFeatureId  id)

Sets the feature ID for this feature.

Parameters
idfeature id
See also
id

Definition at line 101 of file qgsfeature.cpp.

◆ setFields() [1/2]

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
Deprecated:
use setFields( const QgsFields& fields, bool initAttributes = false ) instead
Note
not available in Python bindings

Definition at line 173 of file qgsfeature.cpp.

◆ setFields() [2/2]

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
Note
added in QGIS 2.9
See also
fields

Definition at line 178 of file qgsfeature.cpp.

◆ setGeometry() [1/2]

void QgsFeature::setGeometry ( const QgsGeometry geom)

Set this feature's geometry from another QgsGeometry object.

This method performs a deep copy of the geometry.

Parameters
geomnew feature geometry
See also
geometry
constGeometry
geometryAndOwnership
setGeometryAndOwnership

Definition at line 124 of file qgsfeature.cpp.

◆ setGeometry() [2/2]

void QgsFeature::setGeometry ( QgsGeometry geom)

Set this feature's geometry from a QgsGeometry pointer.

Ownership of the geometry is transferred to the feature.

Parameters
geomnew feature geometry
Note
not available in python bindings
See also
geometry
constGeometry
geometryAndOwnership
setGeometryAndOwnership

Definition at line 129 of file qgsfeature.cpp.

◆ setGeometryAndOwnership()

void QgsFeature::setGeometryAndOwnership ( unsigned char *  geom,
int  length 
)

Set this feature's geometry from WKB.

Set the pointer to the feature geometry.

This feature assumes responsibility for destroying the created geometry.

Parameters
geomgeometry as WKB
lengthsize of WKB
See also
setGeometry
geometry
constGeometry
geometryAndOwnership
Deprecated:
will be removed in QGIS 3.0

Definition at line 166 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 204 of file qgsfeature.cpp.


The documentation for this class was generated from the following files: