4

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?

Oto Kaláb
  • 6,895
  • 3
  • 29
  • 50
Cob234
  • 41
  • 1
  • 4

3 Answers3

2

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 02 '17 at 23:56
  • 1
    Could you please explain that in a little more detail. I may have overstated my knowledge of scripting. – Cob234 Feb 03 '17 at 01: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 03 '17 at 22:15
2

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) ] )
    else:
        pass

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.

Joseph
  • 75,746
  • 7
  • 171
  • 282
0

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.

gisnside
  • 7,818
  • 2
  • 29
  • 73
  • how do i adapt this for Bonn 3.2.2? – lowsparked Sep 06 '18 at 17:00
  • It doesn't work ? – gisnside Sep 07 '18 at 07:29
  • the script and error is her in this doc https://docs.google.com/document/d/1rQCrDKiYdroTXMmtjTWsuk7Jren0pEZBy2lmgQJmJS4/edit?usp=sharing – lowsparked Sep 10 '18 at 13:45
  • No access unless with a login :( – gisnside Sep 10 '18 at 17:32
  • try this one.https://docs.google.com/document/d/1rQCrDKiYdroTXMmtjTWsuk7Jren0pEZBy2lmgQJmJS4/edit?usp=sharing – lowsparked Sep 11 '18 at 16:46
  • Without trying the script, I tried adding from osgeo import ogr (2nd line) + if layer.wkbType() == ogr.wkbPolygon: and it seems to get past the error. Dunno if it's sufficient for you though. – gisnside Sep 11 '18 at 17:13
  • It seems to me the problem may not be there, but rather in the PyQt4 > PyQt5. Maybe you shoudl create a specific question ? Might need this : https://gis.stackexchange.com/questions/276731/compatibility-pyqt5-and-pyqt4 – gisnside Sep 11 '18 at 17:15