3

The requirement is to display only selected records as a New Layer pointing to same source data on disk. If I were selecting using attributes of the current layer it would be simple - I use the filter option. But, I have selected a set of records using a Select by Location. It is these set of records I wish to display as a new layer. Duplicate a layer, copies all records, NOT JUST the selected records into the new layer. I am using QGIS 3.10.6.

OR if there could be a way to get the set of selected records and set it as a filter, this would also do.

1
  • 1
    QGIS has a number of ways to accomplish this, but it might depend on which kind of spatial selection you're performing. Can you elaborate on your selection method?
    – jcarlson
    Commented Jul 11, 2020 at 14:36

3 Answers 3

5

In the selected layer create a new field with the field calculator - let's call the new field "visible". Be sure to check the box Update only selected features. Than insert a value - in the most simple case just use a boolean field type and insert TRUE. You now have permanently saved your selection to the attribute table and can duplicate the layer and define a simple filter based on the field "visible".

You could also use a virtual field combined with the expression is_selected to have a dynamic field that changes when the selection changes.

Consider however that once you have the layer duplicated, they rely on the same data source, but selcting features will be done on a by-layer-base: you can have different selections on the two layers.

To update a selection from the original layer on the duplicated layer, consider using a virtual layer. I'm not right away sure how (and if) that works, but probably there is a (simple) way to do that. In case you want to do that, post another question.

3

You might also consider a Virtual Field. I don't know exactly how your selection is functioning, but to demonstrate a non-attribute selection, I'll use a polygons layer and a points layer. I want a duplicate of the polygons layer, but only those which contain a point.

points and polygons

  1. On the toolbar, click New Virtual Layer.

virtual layer

  1. Click Import, and add your map layers to the virtual layer.

  2. Write an SQL expression to filter the input features, using the appropriate function to fit your selection. In my case, I'm using ST_Contains. Depending on your layer(s) and your selection, your SQL will probably look very different.

virtual layer and expression

A benefit of using a virtual layer is that if I edit either the points or the polygons layer, the virtual layer will stay up-to-date.

2

You can use the following

layer = iface.activeLayer()
fids = ["{}".format(i['fid']) for i in layer.selectedFeatures()] # If number
# fids = ["'{}'".format(i['fid']) for i in layer.selectedFeatures()] # If String
layer_copy = layer.clone()
layer_copy.setSubsetString('fid IN ({})'.format(', '.join(fids)))
QgsProject.instance().addMapLayer(layer_copy)

Here fid is the column name of the primary key in the original layer. You should change it. In this case, it contains number so I did not need to quote elements in the IN statement but if string you will have to quote (see commented line of the code)

I'm not sure for large selection, the recipe is a good idea e.g putting a large "column_name in (...)".

Instead, if possible, you should better use a virtual layer to make a query to get the equivalent of your "Select by Location".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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