Friday, 9 November 2018

shapefile - What's the batched Refactor Fields tool "field mapping" syntax in QGIS?


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



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': ,
'name': u'type the field name here',
'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)



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...