<div dir="ltr">A very late thank you Nyall, but indeed this works. Maybe it can be added to the PYGIS Cookbook documentation?<div><br></div><div>Cheers</div><div>Wouter</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Op do 7 feb. 2019 om 08:08 schreef Nyall Dawson <<a href="mailto:nyall.dawson@gmail.com">nyall.dawson@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Wed, 6 Feb 2019 at 18:14, Wouter Impens <<a href="mailto:wouter.impens@gmail.com" target="_blank">wouter.impens@gmail.com</a>> wrote:<br>
>> > I'm trying to iterate over the fields of a vector layer (using the index) and fill in the values by an expression<br>
>> ><br>
>> >  my_features = a_layer.getFeatures()<br>
>> >     for a_feat in my_features:<br>
>> >         mijn_index = a_feat.fieldNameIndex('perc_nr')<br>
>> >         value = an_expr<br>
<br>
Change this to:<br>
<br>
my_features = a_layer.getFeatures()<br>
context = QgsExpressionContext()<br>
for a_feat in my_features:<br>
    mijn_index = a_feat.fieldNameIndex('perc_nr')<br>
    context.setFeature(a_feat)<br>
    value = an_expr.evalute(context)<br>
    ...<br>
<br>
Nyall<br>
<br>
<br>
>><br>
>> What's "an_expr" here? Is it a QgsExpression object?<br>
>><br>
>> Nyall<br>
>><br>
>> >          a_feat.setAttribute(mijn_index, value)<br>
>> ><br>
>> >     a_layer.commitChanges()<br>
>> ><br>
>> > I don't get an error but nothing is filled in. I evaluated the expression and it's true. It also works in the field calculator.<br>
>> ><br>
>> > Anyone an idea?<br>
>> ><br>
>> ><br>
>> > Kind regards<br>
>> > Wouter<br>
>> > _______________________________________________<br>
>> > Qgis-user mailing list<br>
>> > <a href="mailto:Qgis-user@lists.osgeo.org" target="_blank">Qgis-user@lists.osgeo.org</a><br>
>> > List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
>> > Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-user" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-user</a><br>
</blockquote></div>