QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Public Member Functions | Private Attributes | 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>

Collaboration diagram for QgsFeature:
Collaboration graph
[legend]

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

Private Attributes

QgsFeatureId mFid
 feature id More...
 
QgsAttributes mAttributes
 attributes accessed by field index More...
 
QgsGeometrymGeometry
 pointer to geometry in binary WKB format More...
 
bool mOwnsGeometry
 Indicator if the mGeometry is owned by this QgsFeature. More...
 
bool mValid
 Flag to indicate if this feature is valid. More...
 
QgsFields mFields
 Optional field map for name-based attribute lookups. 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

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.

References QgsFields::count(), and initAttributes().

QgsFeature::QgsFeature ( const QgsFeature rhs)

copy ctor needed due to internal pointer

Definition at line 46 of file qgsfeature.cpp.

References mGeometry, and setGeometry().

QgsFeature::~QgsFeature ( )

Destructor.

Definition at line 89 of file qgsfeature.cpp.

References mGeometry, and mOwnsGeometry.

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

Definition at line 230 of file qgsfeature.cpp.

References fieldNameIndex(), and mAttributes.

Referenced by QgsGeometryAnalyzer::addEventLayerFeature(), QgsVectorLayerFeatureIterator::addJoinedAttributes(), QgsGeometryAnalyzer::bufferFeature(), QgsInterpolator::cacheBaseData(), QgsValueRelationWidgetWrapper::createCache(), QgsVectorFileWriter::createFeature(), QgsAttributeTypeLoadDialog::createPreview(), QgsPointSample::createRandomPoints(), QgsGraduatedSymbolRendererV2::createRenderer(), QgsTransectSample::createSample(), QgsAttributeTableModel::data(), QgsPalLayerSettings::dataDefinedValue(), QgsComposition::dataDefinedValue(), QgsAttributeAction::doAction(), QgsExpression::NodeColumnRef::eval(), QgsGeometryAnalyzer::eventLayer(), fcnAttribute(), QgsAttributeTableModel::featureAdded(), QgsMapTip::fetchFeature(), QgsLabel::fieldValue(), QgsTransectSample::findBaselineGeometry(), QgsSearchQueryBuilder::getFieldValues(), QgsRelation::getRelatedFeaturesRequest(), QgsTINInterpolator::insertData(), QgsAttributeTypeLoadDialog::loadDataToValueMap(), QgsLineVectorLayerDirector::makeGraph(), QgsVectorLayer::maximumValue(), QgsVectorLayer::minimumValue(), QgsRelationEditorWidget::on_mAddFeatureButton_clicked(), QgsRelationEditorWidget::on_mLinkFeatureButton_clicked(), QgsAttributeTableModel::prefetchColumnData(), QgsVectorLayerUndoCommandDeleteAttribute::QgsVectorLayerUndoCommandDeleteAttribute(), QgsPalLabeling::registerDiagramFeature(), QgsPalLayerSettings::registerFeature(), QgsVectorFieldSymbolLayer::renderPoint(), QgsEditorWidgetWrapper::setFeature(), QgsRelationReferenceWidget::setRelation(), QgsVectorLayerUndoCommandChangeAttribute::undo(), QgsVectorDataProvider::uniqueValues(), and QgsVectorLayer::uniqueValues().

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

Definition at line 222 of file qgsfeature.cpp.

References mAttributes.

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

References mAttributes.

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

Definition at line 212 of file qgsfeature.cpp.

References fieldNameIndex(), and mAttributes.

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

Definition at line 239 of file qgsfeature.cpp.

References QgsFields::at(), QgsFields::count(), mFields, and QgsField::name().

Referenced by attribute(), deleteAttribute(), and setAttribute().

const QgsFields* QgsFeature::fields ( ) const
inline

Get associated field map.

Note
added in 2.0

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

Definition at line 233 of file qgsfeature.h.

Referenced by QgsPieDiagram::diagramSize(), QgsHistogramDiagram::diagramSize(), QgsTextDiagram::diagramSize(), QgsPieDiagram::renderDiagram(), QgsHistogramDiagram::renderDiagram(), QgsTextDiagram::renderDiagram(), and setFields().

QgsGeometry * QgsFeature::geometry ( ) const

Get the geometry object associated with this feature.

Definition at line 112 of file qgsfeature.cpp.

References mGeometry.

Referenced by QgsFeatureRequest::acceptFeature(), QgsVectorLayerImport::addFeature(), QgsDxfExport::addFeature(), QgsVectorLayerEditUtils::addPart(), QgsVectorLayerEditUtils::addRing(), QgsPointSample::addSamplePoints(), QgsGeometry::avoidIntersections(), QgsVectorLayer::boundingBoxOfSelected(), QgsGeometryAnalyzer::bufferFeature(), QgsInterpolator::cacheBaseData(), QgsGml::calculateExtentFromFeatures(), QgsZonalStatistics::calculateStatistics(), QgsGeometryAnalyzer::centroidFeature(), QgsAtlasComposition::computeExtent(), QgsGeometryAnalyzer::convexFeature(), QgsVectorFileWriter::createFeature(), QgsTransectSample::createSample(), QgsVectorLayerEditUtils::deleteVertex(), QgsGeometryAnalyzer::dissolveFeature(), QgsPointDisplacementRenderer::drawGroup(), QgsVectorLayerRenderer::drawRendererV2(), QgsVectorLayerRenderer::drawRendererV2Levels(), QgsVectorFileWriter::exportFeaturesSymbolLevels(), QgsVectorLayer::extent(), fcnGeomArea(), fcnGeometry(), fcnGeomLength(), fcnGeomPerimeter(), QgsMapToolIdentify::featureDerivedAttributes(), QgsSpatialIndex::featureInfo(), QgsRelationReferenceWidget::highlightFeature(), QgsVectorLayerImport::importLayer(), QgsHighlight::init(), QgsTINInterpolator::insertData(), QgsVectorLayerEditUtils::insertVertex(), QgsOverlayAnalyzer::intersectFeature(), QgsLabel::labelPoint(), QgsLineVectorLayerDirector::makeGraph(), QgsVectorLayerEditUtils::moveVertex(), QgsAbstractFeatureIterator::nextFeature(), QgsHighlight::paint(), QgsAtlasComposition::prepareForFeature(), QgsVectorLayerUndoCommandAddFeature::redo(), QgsPalLabeling::registerDiagramFeature(), QgsPalLayerSettings::registerFeature(), QgsVectorLayer::removePolygonIntersections(), QgsPointDisplacementRenderer::renderFeature(), QgsInvertedPolygonRenderer::renderFeature(), QgsFeatureRendererV2::renderFeatureWithSymbol(), QgsSimpleLineSymbolLayerV2::renderPolyline(), QgsMarkerLineSymbolLayerV2::renderPolyline(), QgsAtlasComposition::setCoverageLayer(), QgsAbstractFeatureIterator::simplify(), QgsGeometryAnalyzer::simplifyFeature(), QgsVectorLayer::snapWithContext(), QgsVectorLayerEditUtils::splitParts(), QgsInvertedPolygonRenderer::stopRender(), QgsVectorLayerEditUtils::translateFeature(), QgsVectorLayerUndoCommandAddFeature::undo(), QgsVectorLayerUndoCommandChangeGeometry::undo(), QgsVectorLayer::updateFeature(), QgsHighlight::updateRect(), QgsVectorLayerFeatureIterator::useAddedFeature(), QgsVectorLayerFeatureIterator::useChangedAttributeFeature(), and QgsVectorFileWriter::writeAsVectorFormat().

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.

References mGeometry, and mOwnsGeometry.

Referenced by QgsTransectSample::createSample(), and QgsTransectSample::findBaselineGeometry().

QgsFeatureId QgsFeature::id ( ) const

Get the feature id for this feature.

Returns
Feature id

Definition at line 100 of file qgsfeature.cpp.

References mFid.

Referenced by QgsFeatureRequest::acceptFeature(), QgsVectorLayerEditUtils::addRing(), QgsPointSample::addSamplePoints(), QgsVectorLayer::allFeatureIds(), QgsGeometry::avoidIntersections(), QgsVectorLayer::boundingBoxOfSelected(), QgsVectorLayerCache::cacheFeature(), QgsZonalStatistics::calculateStatistics(), QgsVectorLayerEditBuffer::commitChanges(), QgsOfflineEditing::copyVectorLayer(), QgsVectorFileWriter::createFeature(), QgsFeatureListModel::data(), QgsAttributeTableModel::data(), QgsPointDisplacementRenderer::drawGroup(), QgsVectorLayerRenderer::drawRendererV2(), QgsVectorLayerRenderer::drawRendererV2Levels(), QgsGml::endElement(), QgsGeometryAnalyzer::eventLayer(), fcnFeatureId(), QgsAttributeTableModel::featureAdded(), QgsSpatialIndex::featureInfo(), QgsVectorLayerFeatureIterator::fetchFeature(), QgsCachedFeatureWriterIterator::fetchFeature(), QgsVectorLayerFeatureIterator::fetchNextChangedAttributeFeature(), QgsAttributeTableFilterModel::generateListOfVisibleFeatures(), QgsVectorLayerImport::importLayer(), QgsVectorLayer::invertSelectionInRectangle(), QgsAttributeTableModel::loadLayer(), QgsAbstractFeatureIterator::nextFeatureFilterFids(), QgsExpressionSelectionDialog::on_mActionAddToSelection_triggered(), QgsExpressionSelectionDialog::on_mActionRemoveFromSelection_triggered(), QgsExpressionSelectionDialog::on_mActionSelect_triggered(), QgsExpressionSelectionDialog::on_mActionSelectInstersect_triggered(), QgsDualView::on_mFeatureList_currentEditSelectionChanged(), QgsAttributeTableModel::prefetchColumnData(), QgsAtlasComposition::prepareForFeature(), QgsVectorLayerUndoCommandDeleteAttribute::QgsVectorLayerUndoCommandDeleteAttribute(), QgsVectorLayerUndoCommandAddFeature::redo(), QgsPalLabeling::registerDiagramFeature(), QgsPalLayerSettings::registerFeature(), QgsVectorLayer::removePolygonIntersections(), QgsPointDisplacementRenderer::renderFeature(), QgsFeatureRendererV2::renderFeatureWithSymbol(), QgsAttributeForm::save(), QgsVectorLayer::select(), QgsAtlasComposition::setCoverageLayer(), QgsHtmlAnnotationItem::setFeatureForMapPosition(), QgsFormAnnotationItem::setFeatureForMapPosition(), setFeatureId(), QgsRelationReferenceWidget::setRelation(), QgsVectorLayer::snapWithContext(), QgsVectorLayerEditUtils::splitParts(), QgsVectorLayerUndoCommandAddFeature::undo(), QgsVectorLayerUndoCommandDeleteFeature::undo(), QgsVectorLayerUndoCommandDeleteAttribute::undo(), QgsVectorLayerFeatureIterator::updateChangedAttributes(), QgsVectorLayerEditBuffer::updateChangedAttributes(), QgsVectorLayer::updateFeature(), QgsVectorLayerFeatureIterator::updateFeatureGeometry(), QgsVectorLayerEditBuffer::updateFeatureGeometry(), QgsAtlasComposition::updateFeatures(), QgsOfflineEditing::updateFidLookup(), QgsVectorLayerFeatureIterator::useAddedFeature(), and QgsVectorFileWriter::writeAsVectorFormat().

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.

References mAttributes.

Referenced by QgsVectorLayerImport::addFeature(), QgsAttributeTableModel::feature(), QgsVectorLayerImport::importLayer(), QgsFeature(), setFields(), and QgsVectorFileWriter::writeAsVectorFormat().

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

assignment operator needed due to internal pointer

Definition at line 63 of file qgsfeature.cpp.

References mAttributes, mFid, mFields, mGeometry, mOwnsGeometry, mValid, and setGeometry().

bool QgsFeature::setAttribute ( int  field,
const QVariant &  attr 
)
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

Definition at line 202 of file qgsfeature.cpp.

References fieldNameIndex(), and mAttributes.

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

Definition at line 161 of file qgsfeature.cpp.

References QgsFields::count(), fields(), initAttributes(), and mFields.

Referenced by QgsFeatureStore::addFeature(), QgsPalLabeling::drawLabeling(), QgsVectorLayerFeatureIterator::fetchFeature(), QgsAttributeForm::onAttributeAdded(), QgsAttributeForm::onAttributeDeleted(), QgsFeatureStore::setFields(), QgsVectorLayerFeatureIterator::useAddedFeature(), and QgsVectorLayerFeatureIterator::useChangedAttributeFeature().

void QgsFeature::setGeometry ( const QgsGeometry geom)
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.

References mGeometry, and mOwnsGeometry.

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.

References QgsGeometry::fromWkb(), and setGeometry().

Referenced by QgsGml::endElement().

void QgsFeature::setValid ( bool  validity)

Member Data Documentation

QgsAttributes QgsFeature::mAttributes
private

attributes accessed by field index

Definition at line 298 of file qgsfeature.h.

Referenced by attribute(), deleteAttribute(), initAttributes(), operator=(), and setAttribute().

QgsFeatureId QgsFeature::mFid
private

feature id

Definition at line 295 of file qgsfeature.h.

Referenced by id(), operator=(), and setFeatureId().

QgsFields QgsFeature::mFields
private

Optional field map for name-based attribute lookups.

Definition at line 315 of file qgsfeature.h.

Referenced by fieldNameIndex(), operator=(), and setFields().

QgsGeometry* QgsFeature::mGeometry
private

pointer to geometry in binary WKB format

This is usually set by a call to OGRGeometry::exportToWkb()

Definition at line 304 of file qgsfeature.h.

Referenced by geometry(), geometryAndOwnership(), operator=(), QgsFeature(), setGeometry(), and ~QgsFeature().

bool QgsFeature::mOwnsGeometry
private

Indicator if the mGeometry is owned by this QgsFeature.

If so, this QgsFeature takes responsibility for the mGeometry's destruction.

Definition at line 309 of file qgsfeature.h.

Referenced by geometryAndOwnership(), operator=(), setGeometry(), and ~QgsFeature().

bool QgsFeature::mValid
private

Flag to indicate if this feature is valid.

Definition at line 312 of file qgsfeature.h.

Referenced by isValid(), operator=(), and setValid().


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