In this recipe, we'll completely remove a feature, including the geometry and attributes, from a layer.
You will need the New York City museums' shapefile used in other recipes, which you can download as a ZIP file from https://geospatialpython.googlecode.com/svn/NYC_MUSEUMS_GEO.zip.
Extract this shapefile to /qgis_data/nyc
.
All we need to do is load the layer and then delete the desired features by ID, using the layer's data provider:
vectorLyr = QgsVectorLayer('/qgis_data/nyc/NYC_MUSEUMS_GEO.shp', 'Museums' , "ogr") vectorLyr.isValid()
vectorLyr.dataProvider().deleteFeatures([ 22, 95 ])
This operation cannot be simpler or better designed. There are a number of ways in which we can programmatically fill a Python list with feature IDs. For example, we can use the Chapter 2, Filtering a Layer by Attributes in this recipe. Then, we just pass this list to the layer's data provider and we are done.