3

I'm trying to copy multiple attribute values between two features in a geojson with the same value of another field (like an id).

For example:

  • id= 1 field = A

if in another record I repeat id =1 automatically set field = A

I think with autofields plugin I could make something like that but I don´t know how to build the expression I have to use.

6
  • 1
    Which QGIS version?
    – Mapperz
    May 15, 2017 at 18:20
  • Hi, i´m in QGIS 2.14 ltr
    – pacuga
    May 15, 2017 at 20:09
  • @pacuga - How many id values are there?
    – Joseph
    May 16, 2017 at 10:59
  • Not too many, the idea is to copy the attributes of the rest of the fields. Each "unit" would be a small portion of a large dataset assigned to a technician (such as 200 polygons) but not many with the repeated ID
    – pacuga
    May 16, 2017 at 11:22
  • @pacuga - If the values for "field" is fixed for each particular id (e.g. id = 1; field = A, id = 2; field = B etc), then you could create a case statement so instead of copying the values, just let the expression enter them. I.e. CASE WHEN "id" = 1 THEN A WHEN "id" = 2 THEN B WHEN "id" = 3 THEN C END.
    – Joseph
    May 16, 2017 at 11:40

1 Answer 1

3

I think one method could be to create a dictionary holding the id keys and their associated values in your field. Then when a user adds a feature and enters the id, the value would be read from the dictionary and added to the field.

So you could use something like the following in the Python Console (remember to change the field names accordingly):

layer = iface.activeLayer()
value_dict = {}
id_idx = layer.fieldNameIndex('id')
value_idx = layer.fieldNameIndex('field_name')
for feat in layer.getFeatures():
    value_dict[feat[id_idx]] = feat[value_idx]

def update(featureAdded):
    for feat in layer.getFeatures():
        attr = feat.attributes()
        if feat.id() == featureAdded:
            layer.changeAttributeValue(featureAdded, value_idx, value_dict[attr[id_idx]])

layer.featureAdded.connect(update)

Then test by adding a feature and entering a known id in your "id" field.

2
  • 1
    Thank you Joseph !! Just that solution I thought with a small form and another dictionary for when the feature already exists. Thanks
    – pacuga
    May 17, 2017 at 10:59
  • @pacuga - Most welcome! Sorry, are you asking another question?
    – Joseph
    May 17, 2017 at 11:19

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.