0

My code is iterating a shapefile and for each iteration a new point feature is added in another shapefile. Inside the same iteration I want to change the attribute value of the point feature which has just been added. I found this code PyQGIS: using changeAttributeValues on new feature and tried to adapt it to my needs but I does not work. I get no error but also not the result needed. Where am I going wrong?

lawis_Pts = QgsProject.instance().mapLayersByName('REL_LAWIS_profiles')[0]
ftsPoly = newlayer.getFeatures()

addedFeatures = []
def storeFeaturesIds(featId):
    addedFeatures.append(featId)
        

def updateAttributesAfterAdding():
    while addedFeatures:
        featureId = addedFeatures.pop()
        layer.beginEditCommand(layer.startEditing())
        layer.changeAttributeValue(featureId, field, new_val) #REL_LAWIS_ID is variable for new value
        layer.endEditCommand()

for feat in ftsPoly:
    #doing a lot of things with ftsPoly to get coordinates for my points..    
    lawis_Pts.startEditing()
    lawisfeat = QgsFeature()
    lawisfeat.setGeometry( QgsGeometry.fromPointXY(QgsPointXY(lawisprofile_long,lawisprofile_lat)))
    lawisprovider.addFeatures([lawisfeat])
    lawis_Pts.commitChanges()

    #get field_id and new value
    field = lawis_Pts.fields().lookupField('REL_ID')
    new_val = REL_LAWIS_ID

    #run function
    lawis_Pts.featureAdded.connect(storeFeaturesIds)
    lawis_Pts.editCommandEnded.connect(updateAttributesAfterAdding)
2
  • Try with minimal code, is it working then? No for loop, functions. ..
    – Bera
    Commented Feb 19, 2023 at 9:57
  • I put the functions out of the loop now, also the list with featID. Still doesn't change values. I think the problem is with the first function. It does not append the ID of the feat..
    – Simon
    Commented Feb 19, 2023 at 12:10

1 Answer 1

0

I found a solution that does the job for me.

for feat in ftsPoly:
    #doing a lot of things with ftsPoly to get coordinates for my points..    
    lawis_Pts.startEditing()
    lawisfeat = QgsFeature()
    lawisfeat.setGeometry( QgsGeometry.fromPointXY(QgsPointXY(lawisprofile_long,lawisprofile_lat)))
    lawisprovider.addFeatures([lawisfeat])
    lawis_Pts.commitChanges()

    #does the job:
    featnr = lawis_Pts.featureCount() #count feat of points layer
    featlist = list(range(featnr))    #write list with number of points
    wanted_feat = [featlist[-1]]      #get index of last idem in list
    lawis_Pts.select(wanted_feat)     #select last pointfeat of layer
    selection = lawis_Pts.selectedFeatures() #get feature
    
    #change attribute
    lawis_Pts.startEditing() #start edit session
    for lfeat in selection:  
        lawis_Pts.changeAttributeValue(lfeat.id(), rel_id_idx, REL_LAWIS_ID)
        lawis_Pts.changeAttributeValue(lfeat.id(), name_idx, LAWIS_NAME)
        lawis_Pts.changeAttributeValue(lfeat.id(), id_idx, LAWIS_id)
        lawis_Pts.changeAttributeValue(lfeat.id(), pdf_idx, LAWIS_PDFlink)
    lawis_Pts.commitChanges()
        
    lawis_Pts.removeSelection() #don't forget to remove, otherwise it adds to selection
'''

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.