10

In QGIS, there is this nice tool "Refactor Fields" in the Processing Toolbox :

Refactor Fields tool

Using it on a single layer is fairly straightforward.

However, when you plan on using it in a batch mode, one can see the "Fields mapping" textbox. I'm looking for an example of the syntax to be used there. I haven't found any specific documentation yet.

Refactor batched

  • After run a refactoring from the GUI, you can watch the command in the Processing -> History menu (Ctrl+Alt+H) – kadarivan Jul 17 '18 at 6:02
7

It is not very comfortable to do, but the following is the required syntax for that textbox (I write it in several lines only for the sake of clearness):

{'expression': u'type the expression here',
 'length': <type the length>,
 'type': <code of the type>,
 'name': u'type the field name here',
 'precision': <type the precision>}

For example (remeber to use curly brackets):

{'expression': u'"FLAG1"+"FLAG2"',
 'length': 20, 'type': 6,
 'name': u'FLAG1',
 'precision': 5}

You need to write as many lines like the one above as the number of input fields, separated by commas. For example, if your layer contains three fields:

{'expression': u'"FLAG1"+"FLAG2"', 'length': 20, 'type': 6, 'name': u'FLAG1', 'precision': 5}, {'expression': u'100*"FLAG2"', 'length': 20, 'type': 6, 'name': u'FLAG2', 'precision': 5}, {'expression': u'"100-"FLAG2"', 'length': 20, 'type': 6, 'name': u'FLAG3', 'precision': 5}

and it should work (it worked for me).

Please note that you may avoid to use this dictionary-oriented syntax and directly use the required values. For example, with reference to the first example, you may simply use:

{u'"FLAG1"+"FLAG2"', 20, 6, u'FLAG1', 5}

If you don't use this syntax, the following error will be shown:

Missing parameter value: Fields mapping (row 1)

  • 2
    Can you please explain what <code of the type> means? I tried using 3 assuming it is for string (as it is the 3rd in the dropdown options) but I got an error: 'int' object has no attribute 'getItem' See log for more details. The code I used is:{u'"FLAG"', 0, 3, u'FLAG', 0} for one of my layers where {'expression': u'"FLAG"', 'length': 0, 'type': 3, 'name': u'FLAG', 'precision': 0}. – JAT86 Sep 11 '17 at 8:27
  • @JAT86 It is the number that identifies the data type (i.e. int, string and so on). Unfortunately, I didn't find a reference for them, but I should remember the values for the most used types: 2 for int, 6 for double and 10 for string. In addition to this, maybe you should not specify 0 for the field length (at least 1!). Please, let me know if you solved the issue. – mgri Sep 11 '17 at 10:08
  • Thanks for the prompt response. Perhaps the batch processing is much more complex than I had thought. I tried using at least 1 for the length and 10 for type of code: {u'"FLAG"', 1, 10, u'FLAG', 0} Still, the error shows up: 'int' object has no attribute 'getItem' See log for more details. My layer has the following attributes: name: flag,Type: string, Length: 0, Precision: 0, Expression: "flag" – JAT86 Sep 11 '17 at 12:30
  • @JAT86 Does running the tool from the GUI return any problem? – mgri Sep 11 '17 at 14:23
  • If by GUI you mean a single layer, no it did not have any problems. The FLAG column was successfully deleted upon "clicking Delete selected field" and was saved into a new layer "Refactored". – JAT86 Sep 11 '17 at 19:00
2

There is another algorithm similar to this whose interface simplifies the batch process better.

enter image description here

  • 1
    English english – BERA Nov 10 '17 at 13:44
  • The english translation for the menu item in question is "Add field to attribute table" – J. Taylor Mar 7 '18 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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