QGIS Processing – What’s the Batched Refactor Fields Tool ‘Field Mapping’ Syntax in QGIS?

batchqgisqgis-processingshapefilevector

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

Best Answer

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)

Related Question