QGIS – Copy First Row of Attributes and Paste for Each Feature

combo-boxfield-calculatorfields-attributesqgisunique value

In a newly digitised polyline vector layer, there are 7 columns attributes value that are choosen inside a value map combo-box that MUST be exactly the same for every newly digitised line (I choose the values for the first digitised line then all the other newly digitised lines must have the same value of the first one).

I'd like to fill them (in a kind of automatic, or auto completed way) with the exact same values of the first digitised row.

I was thinking about some unique values properties, but since the values are kept inside a combobox I can't use both field properties together.
OR to complete the missing values with some field calculator expression at the end of the digiting.

EDIT:
I'm using QGIS 2.8 and 2.10 running in both Windows and Ubuntu
I post a screenshot of the value map used for 7 different columns, all real(4,3)

enter image description here

The attribute table is the following one and all the "empty" () columns should have the same value appearing in the first row

enter image description here

Like this
enter image description here

Best Answer

Although you're asking for an answer related to value map combo-boxes, I thought to provide an alternative where you could use the following code to read the values in the first row for each column and copy it for each of the attributes. Select the layer in QGIS and copy/paste the code into the Python Console (Plugins -> Python Console or Ctrl + Alt + P):

layer = qgis.utils.iface.activeLayer()
layer.startEditing()
for field in layer.pendingFields():
    name = field.name()
    request = QgsFeatureRequest().setFilterFid(0)
    feat = layer.getFeatures(request).next()
    result = feat[name]
    for feature in layer.getFeatures():
        feature[name] = result
        layer.updateFeature(feature)

This is not automated, which is what you seem to be asking for. But I hope it helps a little anyway! Tested this on QGIS 2.8.2-Wien.


Update:

If you want to run the above script in the graphical modeler, first select the Create new script tool from Processing Toolbox -> Scripts. Note you said 7 columns but gave 8 :). Then insert the following:

##Copy attributes from first row=name
##Layer=vector

import qgis
from qgis.core import *

layer = processing.getObject(input)
layer.startEditing()
for field in layer.pendingFields():
        name = field.name()
        request = QgsFeatureRequest().setFilterFid(0)
        feat = layer.getFeatures(request).next()
        result = feat[name]
        for feature in layer.getFeatures():
                if name == 'ALT_0':
                        feature[name] = result
                        layer.updateFeature(feature)
                if name == 'ALT_1':
                        feature[name] = result
                        layer.updateFeature(feature)
                if name == 'ALT_2':
                        feature[name] = result
                        layer.updateFeature(feature)
                if name == 'ALT_3':
                        feature[name] = result
                        layer.updateFeature(feature)
                if name == 'ALT_4':
                        feature[name] = result
                        layer.updateFeature(feature)
                if name == 'ALT_5':
                        feature[name] = result
                        layer.updateFeature(feature)
                if name == 'ALT_Ideale':
                        feature[name] = result
                        layer.updateFeature(feature)
                if name == 'ALT_propos':
                        feature[name] = result
                        layer.updateFeature(feature)

Also note that I purposely did not save the changes just incase there's a mistake. If you want the script to save the changes, use the layer.commitChanges() code at the end of the script outside all the for loops.

I tested this and it seems to work so hopefully it will for you too =)

Related Question