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 https://geospatialpython.googlecode.com/svn/NYC_MUSEUMS_GEO.zip.
Extract this shapefile to the /qgis_data/nyc
directory.
How to do it...
A feature must have geometry, but it does not require attributes. So, we will create a new feature, add some attributes, and then add everything to the layer, as follows:
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 so that we can get the list of fields:
vpr = vectorLyr.dataProvider()
Now, create a point geometry, which in this case is a new museum:
pnt = QgsGeometry...