I have multiple polygon .shp files that are denoting farm boundaries. The sources are varied and the original fields vary in name and number. These boundary files are to be imported into a Farm Management Software and require specific fields for identifying the data on import. The data in the fields will have to be entered manually. I am using QGIS 2.18

I have limited knowledge of scripting. A plugin would be awesome. I am aware of the "Add Field" in the Processing Toolbox. I could add each field separately through the batch function.

Is it possible to add more than one field to a layer in a batch configuration?


REM how about using OGRINFO to do that. -here's a simple batch file. REM for reference:http://www.gdal.org/ogrinfo.html @echo off Title add fields echo - follow prompts: set /p gisdir=your gis data directory (without quotes): set /p dataformat=what's the data type shp,gpkg,sqlite,any ogr type: echo. echo - adding new fields to shapefile or gpkg or sqlite/spatialite for /r "%gisdir%" %%G in (.%dataformat%) do ogrinfo %%G -dialect SQLite -sql "ALTER TABLE %%G ADD COLUMN columnname, column datatype;" PAUSE echo. REM you can do the same but add data echo - inserting fields into shapefiles for /r "%gisdir%" %%G in (.shp) do ogrinfo %%G -dialect SQLite -sql "INSERT INTO %%G (field1,field2,field3) VALUES ('1','2','3');" echo complete ::script by GeospatialEngineer

  • this script requires GDAL -OGR installed. usually with QGIS you'll have it. Or could be part of OSGEO4W or OSGEOLIVE – Geospatial Engineer Feb 2 '17 at 23:56
  • Could you please explain that in a little more detail. I may have overstated my knowledge of scripting. – Cob234 Feb 3 '17 at 1:17
  • save my answer in a text file and name whatever you want like "addfieldstogisattributetable.bat" edit with notepad++ or similar and enter your specific information instead of my placeholders. Then launch the file it will prompt you for folders and format. – Geospatial Engineer Feb 3 '17 at 22:15

Instead of creating a plugin, you could instead post the following code into the Python Console which iterates through all your loaded polygon layers and adds various fields:

from PyQt4.QtCore import QVariant

for layer in QgsMapLayerRegistry.instance().mapLayers().values():   
    if layer.wkbType() == QGis.WKBPolygon:
        with edit(layer):
            layer.dataProvider().addAttributes( [ QgsField("Str_field", QVariant.String) ] )
            layer.dataProvider().addAttributes( [ QgsField("Int_field", QVariant.Int) ] )
            layer.dataProvider().addAttributes( [ QgsField("Real_field", QVariant.Double) ] )
            layer.dataProvider().addAttributes( [ QgsField("Date_field", QVariant.Date) ] )

The above code shows the different types of fields you can have and the names of the field (e.g. "Str_field"). You can edit these accordingly to your needs. You could also save this into a script by going to the Processing Toolbox:

Processing Toolbox > Scripts > Tools > Create new script

and make sure to save it in your /.qgis2/processing/scripts/ directory.


You can have a look at the scripting made here : Add many columns (to many layers)

All you need to do is adapt the script to your usage, or maybe use the code to make a better script.

Not the answer you're looking for? Browse other questions tagged or ask your own question.