0

I am working on a process and during that process for each project I need to add 16 new fields as text strings to my shapefile. T_1, R_1, S_1, QS_1,... these 4 fields out to "_4" .

enter image description here

My scripting knowledge is not quite up to par but learning.

nmtoken
  • 13,355
  • 5
  • 38
  • 87
lowsparked
  • 359
  • 3
  • 14

2 Answers2

1

Search for the Add Field (Data Management) tool, right click it, select batch. Input your parameters per field, press the + sign to add all those you need. Can use this within a model or script

Maksim
  • 6,916
  • 2
  • 24
  • 42
  • do you any suggestions on how to add this to a model in QGIS or save the process some how. Thanks for your help – lowsparked May 17 '18 at 15:47
  • oh sorry about that, you didn't specify QGIS, I was referring to a solution with ArcGIS. Im sure the process will be similar though – Maksim May 17 '18 at 15:48
  • Also this only allows me to add one field at a time to multiple .shp – lowsparked May 17 '18 at 15:54
  • You can use the same input table (same shapefile) multiple times in the tool, just have it as your input table 16 times – Maksim May 17 '18 at 17:19
0

After finding some script i changed it up a bit and it worked great

from PyQt4.QtCore import QVariant

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
if layer.wkbType() == QGis.WKBPoint: with edit(layer): layer.dataProvider().addAttributes( [ QgsField("T_1", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("R_1", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("S_1", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("QS_1", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("T_2", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("R_2", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("S_2", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("QS_2", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("T_3", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("R_3", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("S_3", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("QS_3", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("T_4", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("R_4", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("S_4", QVariant.String) ] ) layer.dataProvider().addAttributes( [ QgsField("QS_4", QVariant.String) ] )

else:
    pass

This post helped me down the road to figure this out: Add multiple fields to a batch of layers in QGIS?``

lowsparked
  • 359
  • 3
  • 14
  • ok i have come back to this code after a while and i now am stumped on why this in not working .. any guidance would be great! i am getting the following on each line of code :"File "", line 6 layer.dataProvider().addAttributes( [ QgsField("T_1", QVariant.String) ] ) ^" – lowsparked Jul 11 '18 at 20:19
  • how do i update this for Bonn 2.2 – lowsparked Sep 06 '18 at 17:03