Adding a set of attributes to a vector layer
Each QGIS feature has two parts: the geometry and the attributes. In this recipe, we'll add an attribute for a layer from an existing dataset.
Getting ready...
We will use a point shapefile with museum data for New York City, 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 the following directory: /qgis_data/nyc
How to do it...
While you can add a geometry to a feature without attributes, you must have at least a geometry in order to add attributes. So, we will create a new feature, add some attributes, and then add everything to the layer:
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' , "ogr") vectorLyr.isValid()
Next, access the layer's data provider...