QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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.
 QgsFeature (const QgsFields &fields, QgsFeatureId id=QgsFeatureId())
 QgsFeature (const QgsFeature &rhs)
 copy ctor needed due to internal pointer
 ~QgsFeature ()
 Destructor.
QVariant attribute (const QString &name) const
 Lookup attribute value from attribute name.
QVariant attribute (int fieldIdx) const
 Lookup attribute value from its index.
const QgsAttributesattributes () const
QgsAttributesattributes ()
void deleteAttribute (int field)
 Deletes an attribute and its value.
bool deleteAttribute (const QString &name)
 Remove an attribute value.
int fieldNameIndex (const QString &fieldName) const
 Utility method to get attribute index from name.
const QgsFieldsfields () const
 Get associated field map.
QgsGeometrygeometry () const
 Get the geometry object associated with this feature.
QgsGeometrygeometryAndOwnership ()
 Get the geometry object associated with this feature The caller assumes responsibility for the QgsGeometry*'s destruction.
QgsFeatureId id () const
 Get the feature id for this feature.
void initAttributes (int fieldCount)
 Initialize this feature with the given number of fields.
bool isValid () const
 Return the validity of this feature.
QgsFeatureoperator= (QgsFeature const &rhs)
 assignment operator needed due to internal pointer
bool setAttribute (int field, const QVariant &attr)
 Set an attribute by id.
bool setAttribute (const QString &name, QVariant value)
 Insert a value into attribute.
void setAttributes (const QgsAttributes &attrs)
void setFeatureId (QgsFeatureId id)
 Set the feature id for this feature.
void setFields (const QgsFields *fields, bool initAttributes=false)
 Assign a field map with the feature to allow attribute access by attribute name.
void setGeometry (const QgsGeometry &geom)
 Set this feature's geometry from another QgsGeometry object (deep copy)
void setGeometry (QgsGeometry *geom)
 Set this feature's geometry (takes geometry ownership)
void setGeometryAndOwnership (unsigned char *geom, size_t length)
 Set this feature's geometry from WKB.
void setValid (bool validity)
 Set the validity of the feature.

Detailed Description

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

Encapsulates a spatial feature with attributes.

Author
Gary E.Sherman

Definition at line 113 of file qgsfeature.h.

Constructor & Destructor Documentation

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

Constructor.

Definition at line 27 of file qgsfeature.cpp.

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

Definition at line 36 of file qgsfeature.cpp.

QgsFeature::QgsFeature ( const QgsFeature rhs)

copy ctor needed due to internal pointer

Definition at line 46 of file qgsfeature.cpp.

QgsFeature::~QgsFeature ( )

Destructor.

Definition at line 89 of file qgsfeature.cpp.

Member Function Documentation

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

Lookup attribute value from attribute name.

Returns invalid variant if attribute name could not be converted to index (C++ only) Field map must be associated to make this work.

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

Definition at line 230 of file qgsfeature.cpp.

QVariant QgsFeature::attribute ( int  fieldIdx) const

Lookup attribute value from its index.

Returns invalid variant if the index does not exist.

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

Definition at line 222 of file qgsfeature.cpp.

const QgsAttributes& QgsFeature::attributes ( ) const
inline

Definition at line 142 of file qgsfeature.h.

QgsAttributes& QgsFeature::attributes ( )
inline

Definition at line 143 of file qgsfeature.h.

void QgsFeature::deleteAttribute ( int  field)

Deletes an attribute and its value.

Parameters
fieldThe index of the field
Note
For Python: raises a KeyError exception if the field is not found

Definition at line 106 of file qgsfeature.cpp.

bool QgsFeature::deleteAttribute ( const QString &  name)

Remove an attribute value.

Returns false if attribute name could not be converted to index. Field map must be associated to make this work.

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

Definition at line 212 of file qgsfeature.cpp.

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

Utility method to get attribute index from name.

Returns -1 if field does not exist or field map is not associated. Field map must be associated to make this work.

Definition at line 239 of file qgsfeature.cpp.

const QgsFields* QgsFeature::fields ( ) const
inline

Get associated field map.

TODO: QGIS 3 - return reference or value, not pointer

Definition at line 231 of file qgsfeature.h.

QgsGeometry * QgsFeature::geometry ( ) const

Get the geometry object associated with this feature.

Definition at line 112 of file qgsfeature.cpp.

QgsGeometry * QgsFeature::geometryAndOwnership ( )

Get the geometry object associated with this feature The caller assumes responsibility for the QgsGeometry*'s destruction.

Definition at line 117 of file qgsfeature.cpp.

QgsFeatureId QgsFeature::id ( ) const

Get the feature id for this feature.

Returns
Feature id

Definition at line 100 of file qgsfeature.cpp.

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 181 of file qgsfeature.cpp.

bool QgsFeature::isValid ( ) const

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

Definition at line 171 of file qgsfeature.cpp.

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

assignment operator needed due to internal pointer

Definition at line 63 of file qgsfeature.cpp.

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

Set an attribute by id.

Parameters
fieldThe index of the field to set
attrThe value of the attribute
Returns
false, if the field id does not exist
Note
For Python: raises a KeyError exception instead of returning false

Definition at line 190 of file qgsfeature.cpp.

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

Insert a value into attribute.

Returns false if attribute name could not be converted to index. Field map must be associated to make this work.

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

Definition at line 202 of file qgsfeature.cpp.

void QgsFeature::setAttributes ( const QgsAttributes attrs)
inline

Definition at line 144 of file qgsfeature.h.

void QgsFeature::setFeatureId ( QgsFeatureId  id)

Set the feature id for this feature.

Set the feature id.

Parameters
idFeature id

Definition at line 128 of file qgsfeature.cpp.

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. When used from python, make sure a copy of the fields is held by python, as ownership stays there. I.e. Do not call feature.setFields( myDataProvider.fields() ) but instead call myFields = myDataProvider.fields() feature.setFields( myFields )
initAttributesIf true, attributes are initialized. Clears any data previously assigned. C++: Defaults to false Python: Defaults to true

TODO: QGIS3 - take reference, not pointer

Definition at line 161 of file qgsfeature.cpp.

void QgsFeature::setGeometry ( const QgsGeometry geom)

Set this feature's geometry from another QgsGeometry object (deep copy)

Definition at line 134 of file qgsfeature.cpp.

void QgsFeature::setGeometry ( QgsGeometry geom)

Set this feature's geometry (takes geometry ownership)

Note
not available in python bindings

Definition at line 139 of file qgsfeature.cpp.

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

Set this feature's geometry from WKB.

Set the pointer to the feature geometry.

This feature assumes responsibility for destroying geom.

Definition at line 154 of file qgsfeature.cpp.

void QgsFeature::setValid ( bool  validity)

Set the validity of the feature.

Definition at line 176 of file qgsfeature.cpp.


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