[Qgis-developer] Change feature via Python

bernhard.stroebl at jena.de bernhard.stroebl at jena.de
Mon Sep 5 06:24:15 EDT 2011


Dear Martin,

thank you for your quick response. I knew I must be missing something...
will try layer.changeAttributeValue()

Bernhard

Am 05.09.2011 11:31, schrieb Martin Dobias:
> On Mon, Sep 5, 2011 at 10:45 AM,<bernhard.stroebl at jena.de>  wrote:
>> Dear devs,
>>
>> my situation is as follows:
>> I am working on some Python code. I have a non-spatial layer in the project
>> created from a PostgreSQL table.
>> For this layer I can (via Python):
>> 1) create a new feature, manipulate its attribute values and add it to the
>> layer
>> 2) access an existing feature and its attribute values by this code:
>> feat = QgsFeature()
>> ok = layer.featureAtId(fid, feat, False, True)
>> 3) open the feature form and have the user manipulate the attribute values
>> of either 1) or 2)
>>
>> However I could not find a way to access an existing feature and manipulate
>> its attribute values so that the changed values are written back to the
>> layer just like the feature form does.
>> I understand that the feature is not part of the layer as soon as it is
>> accessed by the code given above. Is there a way to replace a feature in a
>> layer or change its attribute values other than via feature form?
>
> Sure. There are two options.
>
> You can use layer's changeAttributeValue() method to tell the vector
> layer about the changes. For this the layer has to be in editing mode
> - either user can start it or you can start it from plugin with
> startEditing() and stop with commitChanges() or rollBack(). In the
> editing mode the changes are stored in memory and stored to a
> file/database when editing mode is left with commitChanges() method.
> This is equivalent to the use of feature form.
>
> The other option is to use vector data provider's method
> changeAttributeValues(). The difference is that the changes are
> immediately written to the backend and the editing mode is not
> considered. That method is actually called from vector layer when
> committing changes from the editing mode.
>
> Martin
>
>
> ________ Information from NOD32 ________
> This message was checked by NOD32 Antivirus System for Linux Mail Server.
> http://www.nod32.com



________ Information from NOD32 ________
This message was checked by NOD32 Antivirus System for Linux Mail Server.
http://www.nod32.com


More information about the Qgis-developer mailing list