3

i'd like to know how can i set the fields of shapefiles in a batch process of QGIS (one setting for all shp) using the field calculator: enter image description here

I'm using an expression like this, taken from an another post:

[{'name': 'Value', 'type': 10, 'length': 5, 'precision': 0, 'expression': 'Value' }, { 'name': 'New_Field', 'type': 10, 'length': 2, 'precision': 0, 'expression': '10'}]

but it doesn't work..any suggestions?

2
  • 2
    What should this expression do exactly?
    – Erik
    Commented Jul 14, 2021 at 15:05
  • it should refactor the fields of all shapes, creating an attribute table composed by 3 fields: 1) name = id, type = integer64, length=5, precision = 0 2) name= city, type = string, length=50, precision = 0 3) name = area, type = real, length= 7, precision=2
    – Mark
    Commented Jul 15, 2021 at 7:54

1 Answer 1

1

For Refactor Fields, the @FIELDS_MAPPING array variable is used to describe the attribute table for each input layer. It is an array of "maps", like you typed out, but you would need to use the expression functions to construct it as brackets are not understood as syntax for arrays and maps in the QGIS expression editor (despite the fact that they are displayed like this in the documentation and preview). Here is an example of how you could populate @FIELDS_MAPPING with "Calculate by Expression":

array(
  map(
    'name', 'Value',
    'type', 10,
    'length', 5,
    'precision', 0,
    'expression', 'Value'
  ),
  map(
    'name', 'New_Field',
    'type', 10,
    'length', 2,
    'precision', 0,
    'expression', '10'
  )
)

This is assuming you are adding an entirely new set of fields, or replacing an attribute table entirely, for each of the input layers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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