Skip to main content
3 of 5
deleted 34 characters in body; edited tags; edited title
francois
  • 315
  • 1
  • 8

Making output shapefile taking fields of input, and adding new field populated with list using PyQGIS?

I found a couple of good answers like here for example, but I do not manage to adapt them and make them work...

What i am trying to do: take the input, create new output with same fields, add a new field with values calculated from another field. For the moment, I managed to get the new values for the new field in a list:

from collections import Counter as counter

layer = qgis.utils.iface.activeLayer()
features = layer.getFeatures()

# find the index of the 'relict_id' field.
idx = layer.fieldNameIndex('RELICT_ID')

relict_id = []
for feat in features:
    attrs = feat.attributes()
    relict_id.append(attrs[idx])

relict_id_new = [x for x in relict_id]

c = counter(relict_id)
idx = 0
while idx in range(len(relict_id)):
    rel_id = relict_id[idx]  
    if c[rel_id] != 1:
        for x in range(c[rel_id]):
            relict_id_new[idx+x] = '{}_{}'.format(relict_id[idx],x)
        idx += c[rel_id]
    else:
        idx +=1

I would like now to adapt this answer, but all I get is a bunch of errors. I tried to get rid off Processings since it does not seem to work for me (QGIS 2.18.2). I am not understanding pretty well the implication of the processes, I do not know if it is the good way to do it and what are the problems.

francois
  • 315
  • 1
  • 8