Changing a vector layer feature's attribute
The process to change an attribute in a feature is 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 as desired at once.
Getting ready
You will need the New York City museums' shapefile used in other recipes, which you can download as a ZIP file from https://geospatialpython.googlecode.com/svn/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 IDs of the fields we want to change, get the index of the field names that we will change, define the new attribute value as an attribute index and value, and change the feature in the layer. To do this, we need to perform the following steps:
Start QGIS.
From the Plugins menu, select Python Console.
First, load the layer and validate it:
vectorLyr = QgsVectorLayer('/qgis_data/nyc/NYC_MUSEUMS_GEO.shp', 'Museums...