6

I've added a new column to my table called "STRNAME" using:

path = "/Users/shapefiles/roads/"
metro_roads = QgsMapLayerRegistry.instance().mapLayersByName('Metro_roads')[0]
processing.runalg('qgis:addfieldtoattributestable', metro_roads, 'STRNAME', 2, 20, 0, path+"Metro_roads_new.shp")

Now I want to populate the new column with values. But I've looked for a processing algorithm (similar to "qgis.addfieldtoattributestable') and can't find one.

How can I add values to a table column from the Python Console?

0

1 Answer 1

3

You can actually do both (add a field and write values in it) using a single QGIS Processing algorithm:

processing.runalg('qgis:fieldcalculator', metro_roads, 'STRNAME', 2, 20, 0, True, "concat('This is row # ', $rownum)", path+"Metro_roads_new.shp")

This is what each parameter means:

processing.runalg('qgis:fieldcalculator', input_layer, field_name, field_type, field_length, field_precision, new_field, formula, output_layer)

If you want to add values for a field that was already added, just change the new_field parameter to False:

processing.runalg('qgis:fieldcalculator', metro_roads_new, 'STRNAME', 2, 20, 0, False, "concat('This is row # ', $rownum)", path+"Metro_roads_new_2.shp")

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.