I would like to duplicate a layer and apply a filter on the new layer that contains only selected features.
Requirements are:
- using a memory layer is not an option, I want to keep the original datasource
- I want to use a filter, so later changes should not be an issue
How can I use setSubsetString()
to execute a query that refers to the internal fid obtained from feature.id()
?
Here is my attempt (which doesn't work, because obviously there is no column named "fid"):
lyr = self.cb_layer.currentLayer()
# copy layer and apply style
iface.setActiveLayer(lyr)
iface.actionCopyLayerStyle().trigger()
lyr_copy = iface.addVectorLayer(lyr.source(), lyr.name() + '_' + str('temp'), lyr.providerType())
iface.actionPasteLayerStyle().trigger()
# create list with ids from selected features
sel_f = lyr.selectedFeatures()
l = []
for f in sel_f:
l.append(f.id())
query = f"fid in ({tuple(l)})"
# Filter
lyr_copy.setSubsetString(query)
Is there an alternative method to setSubsetString()
to filter features by the internal id set by QGIS?