I'm trying to isolate one or more features in a layer, and add them to a different one. I've set up an empty layer (tempLayer) with the same fields as the one I'm selecting features from (layer1).

I then use:

query = '"AreaIDlvl0" = 25007'
selection = layer1.getFeatures(QgsFeatureRequest().setFilterExpression(query))
layer1.selectByIds([k.id() for k in selection])
for feature in layer1.getSelectedFeatures():

But it returns False. I've also tried adding all features:

for feature in layer1.getFeatures():

it returns False for all of them.


You need to make your tempLayer editable before being able to insert features.

layer.startEditing() or with edit(layer).

There is also no need to go through the selection, you already have the features from the first request.

query = '"AreaIDlvl0" = 25007'
features = layer1.getFeatures(QgsFeatureRequest().setFilterExpression(query))
with edit(tempLayer):
    for feature in features:

Your Answer

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

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