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

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
added in 2.0
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
added in 2.0
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
added in 2.0
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.

Note
added in 2.0
const QgsFields* QgsFeature::fields ( ) const
inline

Get associated field map.

Note
added in 2.0

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
added in 2.0
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
Note
added in 2.0

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: