Can anyone tell me if it is possible (and how) to convert or copy a selection on a layer (that has not been created through "select by expression", but instead by irregular selections from a canvas, or a combination of methods) in to the filter/query builder.

OR: If i have made a selection of features not based on a "selection query" or "select by expression" process, but simply by selecting them from the canvas in an irregular fashion ... I want to know if i can take that irregular selection and turn it into a "filter"? (I know i can "save as" a new layer - but I am trying to avoid having to do that) by simply clicking a button/command or something similar and "pasting" it in the query builder to create a filter?

The benefit of this feature would make progressive/iterative selections of selections quicker and without having to "save as" lots of new files ... or potentially lose them if they are just memory layers.

... beyond progressive/iterative, the important thing is to be able to attempt various selections on an initial selection before been satisfied and moving to the next stage.


You can enter the following python command which will create an expression based on the selected feature ids on the active layer.

'$id in ({})'.format( ','.join( str(id) for id in iface.activeLayer().selectedFeaturesIds() ) )

$id in (497483783,497483784,497483785)

or something that works also with persistent values but needs to be finetuned for your particular needs (provider/dataset)

'"pk_field" in ({})'.format( ','.join( str(f[pk_field]) for f in iface.activeLayer().selectedFeatures() ) )

or for strings

'"pk_field" in ({})'.format( ','.join( '\'{}\''.format(f[pk_field]) for f in iface.activeLayer().selectedFeatures() ) )
  • thanks @MatthiasKuhn ... wow, it does indeed ... & I actually managed to work out where to run it (first time w. python)! ... it works for copying & pasting into "select by expression" dialog, i.e. will select the features with those id's. ... however, when i got my result "'$id in (155,20,4,21,5,6,93,94,95,96,148)'" & pasted this into the filter/query builder I get the results shown here: !image here. Are you able to see what i'm doing wrong here and advise further? (seems like its so close now) – guestagain Nov 12 '15 at 12:51
  • The filter/query builder does not take a QgsExpression, but an SQL string that is sent to the provider. It's very similar, but not completely equal. And since $id (feature ids) are internal to QGIS, your database cannot handle it. Replace $id with a unique field (PK) and fill it with appropriate values. – Matthias Kuhn Nov 12 '15 at 16:23
  • Thanks again @MatthiasKuhn. Prior to your helpful explanation above I was trying (a million times and to no avail) to replace in an amateur fashion some of the parts of the original python command you initially sent! ... but wasn't quite sure how! ... Now I am almost there ... by using your second command posted above I can get the field values to copy/paste into the query builder ... it seems the only thing I need to do now is work out how to put an extra ' (apostrophe) before and after each field (to turn it in to a field that the query recognises) that is returned by the command! ... ? – guestagain Nov 12 '15 at 23:37
  • I suppose the intended output would be this: ---------- '"PUBLANDGRP" in ('OTHER PUBLIC LAND','COASTAL WATERS','STATE FOREST')' – guestagain Nov 13 '15 at 1:24
  • 1
    Updated for string fields like yours – Matthias Kuhn Nov 13 '15 at 5:44

You can copy the features once they've been selected and then paste them into the map as a Memory Vector Layer

enter image description here

  • thanks Morgan Ellingham - was trying to do it without having to create a new memory/scratch layer etc. ... i.e. when i've just gone and selected a bunch of features (say from canvas/attribute table) I wish I could just push ~CTRL+C [equivalent], open up the query builder, click "paste" to filter the layer using those features that were selected, then start doing something else to the layer. – guestagain Nov 12 '15 at 5:21
  • Ah ok, so you want to do all your processing on one layer? You could always select your features, copy the unique identifiers from the attribute table to your clip board then paste them into a filter with some fanangling. – Morgan Ellingham Nov 12 '15 at 5:26
  • - yes to your first question. - the point would be that the "filter/query" would stay active (on the one layer) as I save the project and return to it later. And the third suggestion is what I do a lot of now!!! hehehe .... which gets frustrating after a while (ok, very quickly when doing it lots). – guestagain Nov 12 '15 at 5:28
  • You could look at the processing toolbox and create a model with multiple "Select by attribute" steps for each phase of your analysis. It would eventually spit out the resultant layer but at least its only two, rather than a layer for each step. – Morgan Ellingham Nov 12 '15 at 5:35
  • thanks again Morgan Ellingham, but model creation might be a little beyond my capabilities at this stage. _______________________________________________________________________________________________ Surely someone has found/made a way to copy the unique identifiers of an irregular selection (or cumulative regular expression) to paste them somewhere else in this way ... it would also be helpful for styling/labeling etc. I reakon – guestagain Nov 12 '15 at 5:49

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.