3

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 :-)?

1 Answer 1

3

I have here the solution here from Ujaval Gandhi: http://pastebin.com/QktjvJag

I asked him for help here as I was stuck with this for a long time: http://www.qgistutorials.com/en/docs/find_neighbor_polygons.html Thank you Ujaval! You are great!

Pasting the solution here for future reference

from qgis.utils import iface
layer = iface.activeLayer()
layer.startEditing()
compare_field_index = 15
concat_field_index = 5
new_field_index = 10
feature_dict = {f.id(): f for f in layer.getFeatures()}
for f in feature_dict.values():
  if f[concat_field_index]:
    new_field_text = f[concat_field_index]
  else:
    new_field_text = ''
  # Another for loop to go over features
  for compare_f in feature_dict.values():
    if (f != compare_f
        and f[compare_field_index] == compare_f[compare_field_index]):
      print '%s matches' % f[compare_field_index]
      if compare_f[concat_field_index]:
        new_field_text += compare_f[concat_field_index]
  print new_field_text
  f[new_field_index] = new_field_text
layer.commitChanges()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.