|

PyQGIS: Adding and deleting vector layer fields

Objectives:

  • Add fields to a shapefile
  • Delete fields from a shapefile

Open the QGIS Python Console

From the menu, select Plugins -> Python Console, or Ctrl + Alt + P (Windows)

You can type directly into the console, or select the pad and paper icon to write code in the editor. I prefer to write code in the editor because it allows me to save my work and run a compilation of commands.

Load a vector layer

Since we’ll be adding and deleting fields, you may want to create a copy of your shapefile so you don’t lose any important data. We’ll load a shapefile in the QGIS interface and assign it to the variable layer with the following code.

fn = 'c:/path/to/shapefile.shp'
layer = iface.addVectorLayer(fn, '', 'ogr')

Get layer capabilities

Now we’ll get the layer capabilities. This will allow us to make sure we can add and delete fields of the layer. This is done by calling capabilities() on the layer’s data provider.

caps = layer.dataProvider().capabilities()

Add fields

To add a field we’ll first make sure the layer has the proper capabilities. If it does we’ll add the field. The code below demonstrates confirming the layer capabilities and adding two fields (‘New1’ and ‘New2’) to the layer. Once the fields are added we update the layer with updateFields().

if caps & QgsVectorDataProvider.AddAttributes:
    res = layer.dataProvider().addAttributes([QgsField('New1', QVariant.String),
    QgsField('New2', QVariant.String)])
    layer.updateFields()

Delete fields

Loop through fields

Before deleting fields you may want to loop through the existing fields and print field names to the console. This can be done as follows.

for field in layer.fields():
    print(field.name())

The output will be field names.

Delete fields

We delete fields in a very similar way to adding fields. First check the layer capabilities, delete fields, and update fields. This example shows deletion of two fields by the field index.

if caps & QgsVectorDataProvider.DeleteAttributes:
    res = layer.dataProvider().deleteAttributes([7,8])
    layer.updateFields()

Video tutorials

Adding and deleting fields

Add and remove fields from a vector layer.

Looping through fields

Loop through fields in the attribute table of a vector layer.

Similar Posts