Changing a vector layer feature's attribute

Changing a feature's attribute is very straightforward, and well-supported by the PyQGIS API. In this recipe, we'll change a single attribute, but you can change as many attributes of a feature at once as desired.

Getting ready

You will need the New York City museums shapefile used in other recipes, which you can download as a ZIP file from the following URL:

https://github.com/GeospatialPython/Learn/raw/master/NYC_MUSEUMS_GEO.zip

Extract this shapefile to /qgis_data/nyc.

How to do it...

We will load the shapefile as a vector layer, validate it, define the feature ID whose fields we want to change, define the new attribute value as an attribute index and value, and change the feature in the layer:

  1. Start QGIS. ...

Get QGIS Python Programming Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.