1

I have two layers, pipes and intersect layers. Both layers have an id, score column but intersect layer does not have all the ids present in pipes. So I am trying to check if the id present in pipes exists in intersect, if yes then copy intersect's score to pipe's score else leave it blank and continue. But the code neither gives an error nor does it make the changes in the attribute table of the pipe layer. Am I missing something? The code is as follows:

pipe_feat = selectedLayer.getFeatures() #pipes layer
field4 = selectedLayer.fields().indexFromName(fid1) #id column is referenced
selectedLayer.startEditing()
featLayer = layer.getFeatures() #intersect layer
field3 = layer.fields().indexFromName('PIPEID')
for feat in pipe_feat:
    for f in featLayer:
        if feat[field4]==f[field3]:
             feat['SewerScore'] = f['Score'] 
             selectedLayer.updateFeature(feat)
        else:
             continue
selectedLayer.commitChanges()

I have also tried this method: But even this does not work.

  id_score={}
  for f in featLayer:
      id_score[f[field3]] = f['Score']

  for feat in pipe_feat:    
      for key,value in id_score.items():
          if key == feat[field4] :
              feat['SewerScore'] = id_score.get[key]
              layer.updateFeature(f)
              selectedLayer.updateFeature(feat)
          else:
              continue

1 Answer 1

1

Call the getFeatures() function inside your loops to ensure it iterates through all features (at the moment it is only iterating through features in your second for loop which may explain why no features are being matched):

field4 = selectedLayer.fields().indexFromName(fid1)
selectedLayer.startEditing()
field3 = layer.fields().indexFromName('PIPEID')
# Get your pipe features
for feat in selectedLayer.getFeatures():
    # Get your intersect features
    for f in layer.getFeatures():
        if feat[field4]==f[field3]:
            feat['SewerScore'] = f['Score'] 
            selectedLayer.updateFeature(feat)
        else:
            continue
selectedLayer.commitChanges()
2
  • 1
    Thanks a ton ..it worked.... didnt realize this could be the mistake.
    – rao
    Commented Mar 7, 2019 at 12:34
  • @rao - Most welcome, glad it helped :)
    – Joseph
    Commented Mar 7, 2019 at 12:36

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.