3

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

3
  • see if now it s clear to you
    – Mattia
    Commented Aug 24, 2015 at 9:04
  • I tried to answer, but the more often I read the question, the more confused I am.
    – Bernd V.
    Commented Aug 24, 2015 at 11:11
  • edited again, hope that now my qestion is understandable
    – Mattia
    Commented Aug 24, 2015 at 12:28

2 Answers 2

4

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 =)

11
  • Thank You! It's kind of what I was looking for, the only thing that I have to improve to your script is the fact that I need to update the values of only 7 columns (ALT_0, ALT_1, ALT_2, ALT_3, ALT_4, ALT_5, ALT_Ideale and ALT_propos) and not all the attributes of the layer. Can you help me to modify the script in order to do this?
    – Mattia
    Commented Aug 24, 2015 at 14:32
  • And I have to put this script inside the graphical modeler, is there anything else I have to modify?
    – Mattia
    Commented Aug 24, 2015 at 14:38
  • @Mattia - I edited my post, hopefully it will work for you :)
    – Joseph
    Commented Aug 24, 2015 at 14:59
  • 1
    Works like a charm! thank you so much Joseph! I'll give a try inside the modeler in the next days.
    – Mattia
    Commented Aug 24, 2015 at 15:16
  • 1
    @Mattia - I will see what I can do as the code I added does need some cleaning up :)
    – Joseph
    Commented Aug 25, 2015 at 9:21
2

Joseph's answer worked perfectly for me as well ! However you have to adapt it a bit if you want to use it with QGIS 3.

In Python 3 layer.getFeatures(request).next() is deprecated and next(layer.getFeatures(request)) shall be used instead ! :)

layer.startEditing()
for field in layer.fields():
    name = field.name()
    request = QgsFeatureRequest().setFilterFid(0)
    feat = next(layer.getFeatures(request))
    result = feat[name]

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.