AW: [Qgis-developer] Using QgsAttributeMap to get feature attributevalue

Hugentobler Marco marco.hugentobler at karto.baug.ethz.ch
Sat Jul 18 03:44:48 EDT 2009


Hi Matthew

You need to first find out what index the attribute with name 'Elevation' has. QgsAttributeMap uses that index number, not the field name. 
You may find out the index of a field name by using the function fieldNameIndex of QgsVectorDataProvider

int fieldNameIndex(const QString& fieldName)

But that gives you only the commited fields. If you also want to consider the not-commited fields, you could work with QgsVectorLayer::pendingFields

Then, once you have the index, query the value of QgsAttributeMap for this index. QgsAttribute Map is a map (or a dict in python) with key int (the index) and QVariant value (the attribute value).

Regards,
Marco


-----Ursprüngliche Nachricht-----
Von: qgis-developer-bounces at lists.osgeo.org im Auftrag von Matthew Denno
Gesendet: Sa 18.07.2009 05:45
An: qgis-developer
Betreff: [Qgis-developer] Using QgsAttributeMap to get feature attributevalue
 
Hi,

I am having a problem that I am hoping someone can help me with.  I have a
piece of code that iterates through a feature class and pulls out the x and
y coordinates from the geometry.  The z values are stored in the attribute
table and I would also like to pull that value for each feature.  I have
looked at Tutorial 6, but it looks like the classes have changed since it
was written, I think.  I can see that feature.attributeMap() returns a
dictionary of Key-Value pairs (I think) but I can't figure out how to get
the "Elevation" field attribute value from that.  Here is the piece of code:

 while (mypVectorLayer->nextFeature(currentFeature))

{

//get geometry of the feature

QgsGeometry* currentGeometry = currentFeature.geometry();

QgsPoint currentPoint = currentGeometry->asPoint();


 //get the attributes of this feature

const QgsAttributeMap& myAttributes = currentFeature.attributeMap();


 //get the value from the "Elevation" field

double zval = //value from field



//add point coordinates to point and add point to dt

 Point pt = Point(currentPoint.x(), currentPoint.y(), zval);

dt.insert(pt);

//delete currentFeature;

}


Thanks,


Matt



More information about the Qgis-developer mailing list