2

I have ~5000 features in a layer in QGIS. Each feature has a unique ID. I have a list of ~800 features (with corresponding IDs) I would like to select and remove from the layer. Which expression could I use to do this?

The expressions I have used in the expression field of the attribute table have not worked.

For example: My IDs look like ['AB123', 'AC12-30', 'TH018'....] I've tried expressions like: 'ID' = 'AB123' OR 'AC12-30' OR 'TH018'

But the expression is invalid because the above IDs cannot be converted to boolean. I generated my list of IDs I want to remove in another development environment. I'd like to select those IDs in the Attribute Table then delete selected features.

2
  • Welcome to GIS SE. As a new user, please take the Tour. Without the expressions you have used and the nature of the "not worked", it's likely that this Question will be closed. Please Edit the Question to provide a specific workflow and outcome description.
    – Vince
    Mar 30 at 20:33
  • Another way is to modify your expression like this : 'ID' = 'AB123' OR 'ID' = 'AC12-30' OR 'ID' = 'TH018' (etc.). But the array solution is cleaner and allows you use an array built with the aggregate function.
    – ThiPa
    Mar 31 at 6:08

1 Answer 1

1

Using Python, you can adapt the following code to your situation (do this with a unique id field, not the actual feature id):

# get your layer
layer = iface.mapCanvas().layers()[0] #this gets the first visible layer from interface

# list of unique id's to delete
uid_list = [3 , 5, 105] #this is an example, you can create a list from many sources, like csv file

# name of the unique id field
uid_field = 'field name'
with edit(layer):
    for feature in layer.getFeatures():
        if (feature[uid_field] in uid_list):
            layer.deleteFeature(feature.id())

Using an expression, first select the features with following expression (you'll need to build the array somehow):

array_contains( array(3, 5, 105), "uid_field")

Then open layer for editing and delete the features, close and save.

If you struggle to build the unique id's list, provide more details about the source format of the list in order to get help.

1
  • 1
    Thank you. I used: array_contains( array('AB123', 'AC12-30', 'TH018'), "uid_field") and wad able to select the IDs I wanted to delete.
    – PeanutMap
    Mar 30 at 21:59

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