QGIS API Documentation  2.6.0-Brighton
 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
QgsFeatureoperator= (QgsFeature const &rhs)
 assignment operator needed due to internal pointer
 ~QgsFeature ()
 Destructor.
QgsFeatureId id () const
 Get the feature id for this feature.
void setFeatureId (QgsFeatureId id)
 Set the feature id for this feature.
const QgsAttributesattributes () const
QgsAttributesattributes ()
void setAttributes (const QgsAttributes &attrs)
bool setAttribute (int field, const QVariant &attr)
 Set an attribute by id.
void initAttributes (int fieldCount)
 Initialize this feature with the given number of fields.
void deleteAttribute (int field)
 Deletes an attribute and its value.
bool isValid () const
 Return the validity of this feature.
void setValid (bool validity)
 Set the validity of the feature.
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.
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 setFields (const QgsFields *fields, bool initAttributes=false)
 Assign a field map with the feature to allow attribute access by attribute name.
const QgsFieldsfields () const
 Get associated field map.
bool setAttribute (const QString &name, QVariant value)
 Insert a value into attribute.
bool deleteAttribute (const QString &name)
 Remove an attribute value.
QVariant attribute (const QString &name) const
 Lookup attribute value from attribute name.
QVariant attribute (int fieldIdx) const
 Lookup attribute value from its index.
int fieldNameIndex (const QString &fieldName) const
 Utility method to get attribute index from name.

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

Constructor & Destructor Documentation

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

Constructor.

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

copy ctor needed due to internal pointer

QgsFeature::~QgsFeature ( )

Destructor.

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
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
const QgsAttributes& QgsFeature::attributes ( ) const
inline
QgsAttributes& QgsFeature::attributes ( )
inline
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
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
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.

const QgsFields* QgsFeature::fields ( ) const
inline

Get associated field map.

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

QgsGeometry * QgsFeature::geometry ( ) const

Get the geometry object associated with this feature.

QgsGeometry * QgsFeature::geometryAndOwnership ( )

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

QgsFeatureId QgsFeature::id ( ) const

Get the feature id for this feature.

Returns
Feature id
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
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.

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

assignment operator needed due to internal pointer

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
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
void QgsFeature::setAttributes ( const QgsAttributes attrs)
inline
void QgsFeature::setFeatureId ( QgsFeatureId  id)

Set the feature id for this feature.

Set the feature id.

Parameters
idFeature id
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

void QgsFeature::setGeometry ( const QgsGeometry geom)

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

void QgsFeature::setGeometry ( QgsGeometry geom)

Set this feature's geometry (takes geometry ownership)

Note
not available in python bindings
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.

void QgsFeature::setValid ( bool  validity)

Set the validity of the feature.


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