I have a question concerning pyQGIS. I'm a QGIS long-time (self-learned) user, but just a pyQGIS newbie. I want to implement 1:n spatial join with attribute concatenation in the table. I have a vector points layer with multiple points on the corresponding polygons, I already made a spatial join of the points with the polygons, so I have now a point layer with the key field of the polygon (column 10 in the table).
Now I just want to search and compare the table attributes in column 10 and if they are the same, concatenate the text in column 5. Should be easy, but for me it isn't. at first I just wanted to compare the column 10 and if it is the same - print how many time it repeats, so my first approach:
#!/usr/bin/env python
# -*- coding: cp1250 -*-
layer = qgis.utils.iface.activeLayer()
features = layer.getFeatures()
count=1
for feature in features:
# fetch attributes
attrs = feature.attributes()
attrs1 = features.next()
# attrs is a list. It contains all the attribute values of this feature
print attrs[10]
print attrs1[10]
print "FID:"+str(feature.id())
if attrs[10] == attrs1[10]:
count+=1
print count
else:
count=1
print count
the program works, but it skips each 1 row.
the next approach (with help of Krzysztof Maślanka):
#!/usr/bin/env python
# -*- coding: cp1250 -*-
layer = qgis.utils.iface.activeLayer()
features2 = layer.getFeatures()
features = layer.getFeatures()
powt=1
for feature in features:
attrs = feature.attributes()
for feature2 in features2:
# fetch attributes
attrs1 = feature2.attributes()
# attrs is a list. It contains all the attribute values of this feature
if attrs[10] == attrs1[10]:
powt+=1
print "FID:"+str(feature.id())
print attrs[10]
print attrs1[10]
print powt
else:
print "FID:"+str(feature.id())
print attrs[10]
print attrs1[10]
powt=1
print powt
The problem with the above program is that it should compare every row to each other, but the primary loop exits after the sub-loop is finished, so it just compares the 1st row to the table, then exits.
Can anybody help with this? It would cover the lack of this in QGIS. OR maybe somebody knows how to make a multiple 1:n spatial join with attribute concatenation in GRASS GIS or some other OS4GEO software (I didn't find it, but why should one ram against open door if it exists :-)?