already read many answers for similar questions, but i didnt find a solution for my problem. Dont know how to start.
Problem: shp with about 50 fields. Each field contain NULL, 1 or 2. Now i need two new fields. One containing the number of fields with "1", the other with "2".
I am using QGis 2.16.2 and 2.14; OS Win10
I'm trying @Joseph's solution, add the hint @kyle reed and make some modifications to start as an expression:
@qgsfunction(args='auto', group='Custom')
def R1func(feature, parent):
lists = []
layer = qgis.utils.iface.activeLayer()
for feat in layer.getFeatures():
f = feat.fields()
num = f.count()
for i in range(num):
if feat[i] == 2:
lists.append(i)
return len(set(lists))
and also try a short version
@qgsfunction(args='auto', group='Custom')
def R2func(feature, parent):
lists = []
layer = qgis.utils.iface.activeLayer()
for feat in layer.getFeatures():
lists.append(feat.fields())
return lists.count(2)
Version 1 returns for every row 60
(for number 1) or 37
(for number 2)
Version 2 returns 0
Maybe i make a mistake.