4

How can I access a feature and change its attribute value, I just added to a processing-output before via sink.addFeature(feature, QgsFeatureSink.FastInsert)? Things like feature.id(specificfeatid)[fieldindex] = newattributevalue do not work.


To provide some background information:

I am working on a processing tool, where I need to change a features attribute, after it was added. Reason is that I want to add the total distance of a route (=iterinaries), I dont know when a part of a route (=leg) is created. So I am doing it like this in a PlugIn:

    # Creating the features here....
    route_leg_totaldistcounter += 1 # counting the number of legs of a route
    # END OF LOOP legs
                        
# Update total distance here since it is the sum of all legs and not available in response jsons
attr_totaldistance = { fieldindex_position_of_routetotaldistance : route_total_distance } # only change totaldistance field, we get its position while building the dict
last_featureid_totaldistance = feature.id() # the last featureid of a route
first_featureid_totaldistance = last_featureid_totaldistance - route_leg_totaldistcounter + 1 # the first featureid of a route
for features_before in range(first_featureid_totaldistance,last_featureid_totaldistance): # loop through all legs of the current route
    routes_memorylayer_pr.changeAttributeValues({ features_before : attr_totaldistance }) # add the leg-sum of all legs of a route as totaldistance
# END OF LOOP iterinaries

Which is working just fine. But I can not use this method in my processing tool, since there is no layer and therefore no dataprovider available. So my processing tool attempt currently looks like:

    sink.addFeature(feature, QgsFeatureSink.FastInsert) # add feature to the output
    route_leg_totaldistcounter += 1 # counting the number of legs of a route
    # END OF LOOP legs
                        
# Update total distance here since it is the sum of all legs and not available in response jsons
last_featureid_totaldistance = feature.id() # the last featureid of a route = current feature
first_featureid_totaldistance = last_featureid_totaldistance - route_leg_totaldistcounter + 1 # the first featureid of a route
for features_before in range(first_featureid_totaldistance,last_featureid_totaldistance): # loop through all legs of the current route
    #dings = feature.id(features_before)
    #dings.setAttribute(fieldindex_position_of_routetotaldistance,route_total_distance)
    #feature.id(features_before)[fieldindex_position_of_routetotaldistance] = route_total_distance
# END OF LOOP iterinaries

Any ideas how this can be done?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.