[Qgis-developer] The first column in the attribute table

Tom Chadwin tom.chadwin at nnpa.org.uk
Fri Jun 17 03:15:18 PDT 2016


Matthias Kuhn-2 wrote
> Are you working with an attribute table (or an attribute table model)
> there?

Yes. This code takes categorized 2.5d render roof colours, wall colours, and
heights, and adds them as fields into cleanedLayer, which is GeoJSON from
writeAsVectorFormat():
provider =
cleanedLayer.dataProvider()provider.addAttributes([QgsField("height",
QVariant.Double),    QgsField("wallColor", QVariant.String),   
QgsField("roofColor", QVariant.String)])cleanedLayer.updateFields()fields =
cleanedLayer.pendingFields()renderer = dirtyLayer.rendererV2()renderContext
= QgsRenderContext.fromMapSettings(        canvas.mapSettings())heightField
= fields.indexFromName("height")wallField =
fields.indexFromName("wallColor")roofField =
fields.indexFromName("roofColor")feats =
dirtyLayer.getFeatures()cleanedLayer.startEditing()for feat in feats:   
classAttribute = renderer.classAttribute()    attrValue =
feat.attribute(classAttribute)    catIndex =
renderer.categoryIndexForValue(attrValue)    categories =
renderer.categories()    symbol = categories[catIndex].symbol()    wallColor
= symbol.symbolLayer(1).subSymbol().color().name()    roofColor =
symbol.symbolLayer(2).subSymbol().color().name()   
cleanedLayer.changeAttributeValue(feat.id() + 1,                                     
heightField, height)    cleanedLayer.changeAttributeValue(feat.id() + 1,                                     
wallField, wallColor)    cleanedLayer.changeAttributeValue(feat.id() + 1,                                     
roofField, roofColor)cleanedLayer.commitChanges()
Before adding those three "+ 1"s from that commit, the colours and height
were applying to the wrong feature - the id (I think) off by one. Hence the
hacky fix, and my comment that this works, but I don't understand why I have
to do it.




--
View this message in context: http://osgeo-org.1560.x6.nabble.com/The-first-column-in-the-attribute-table-tp5271947p5272186.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20160617/535b6a3c/attachment.html>


More information about the Qgis-developer mailing list