1

I noticed that the Processing algorithm, Refactor Fields has a fields mapping parameter. I don't think I've come across this and definitely not used it before from the Python Console.

It provides an example on how to use it:

processing.runalg("qgis:refactorfields",
                  input,
                  [{'name': output_field_name,
                    'type': output_field_type,
                    'length': output_field_length,
                    'precision': output_field_precision,
                    'expression': expression_based_on_input_layer
                  }],
                  output)

I tried to modify this in the console but I receive no output. Here is the typical code I used:

import processing
input = qgis.utils.iface.activeLayer()
output = "C:/Users/Me/Desktop//test.shp"

processing.runalg("qgis:refactorfields",
              input,
              [{'name': "Value",
                'type': 0,
                'length': 10,
                'precision': 10,
                'expression': "Value"
              }],
              output)

Anyone know where I am going wrong? I'm using QGIS 2.12.0-Lyon with Processing version 2.10.2.

4

Well I think I found what my problem was:

The tool does not like input = qgis.utils.iface.activeLayer(). In other words, the user must either specify the actual name of the layer loaded in the Table of Contents (especially if running in the Python Console of QGIS) or specify the path of the shapefile.

import processing

input = "C:/Users/Me/Desktop//polygon_example.shp"
output = "C:/Users/Me/Desktop//test.shp"

processing.runalg("qgis:refactorfields",
                      input,
                      [{'name': 'Value',
                        'type': 10,
                        'length': 10,
                        'precision': 0,
                        'expression': 'Value'
                       }],
                      output)

Note that when running the above code, the output will only contain the field "Value". If you have multiple fields, you will have to repeat the above field map style for all fields you want kept but you could at the same time change their properties such as type and length (eg. the Value field from the question):

import processing

input = "C:/Users/Me/Desktop//polygon_example.shp"
output = "C:/Users/Me/Desktop//test.shp"

processing.runalg("qgis:refactorfields",
                      input,
                      [{'name': 'Value',
                        'type': 10,
                        'length': 5,
                        'precision': 0,
                        'expression': 'Value'
                       }, {
                        'name': 'New_Field',
                        'type': 10,
                        'length': 2,
                        'precision': 0,
                        'expression': '10'
                      output)

Hope this helps someone!


To run this on multiple shapefiles in a directory, the following could be used:

import glob, os, processing

path = "path/to/folder/"
for layer in glob.glob(path + "*.shp"):
    vLayer = QgsVectorLayer(layer, os.path.splitext(os.path.basename(layer))[0], 'ogr')
    processing.runalg("qgis:refactorfields",
                          vLayer,
                          [{'name': 'Value',
                            'type': 10,
                            'length': 10,
                            'precision': 0,
                            'expression': '1'
                           }],
                          path + vLayer.name() + '_refactored.shp')
  • 1
    Is that possible to use this method on more than one shapefile? For example on a folder full of shapefiles? – Skye Aug 21 '17 at 12:59
  • 2
    @Skye - Yup! Edited post to include a possible method :) – Joseph Aug 21 '17 at 13:19

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.