2

Came across this "Create layer with selected" button in the result for a Spatial Query in QGIS. And simply, this creates another layer from the same source with a query on the IDs. Does anyone know if this is available elsewhere in the GUI too? I looked around and couldn't find it, but it'd sure be handy.

enter image description here

There's been some confusion as to what I'm asking. I will try to clarify. If there is a subset of features I want to work with, I begin with selecting them; this could be a spatial query, manual selection, or query by attributes. Once these are selected, if I don't want to create a new shapefile (or whatever), but instead work with and maybe modify the original data, I duplicate the layer and do a query on it. If it's a subset based on a common attribute, this is fairly straightforward, but if the attributes aren't common, I either have to type in every ID into the query or export the selection into a table, copy the ID column, paste it into notepad, find and replace characters to get the search string I'm looking for, copy that and paste it into the definition query with the additional search verbiage. This button does all that for me in a second. I'm not looking to create a new shapefile. I just want a defined subset to work with in the original data. That what this button does. It takes the IDs of the selected features and creates a new layer from the original source and defines a query on the IDs for the layer.

  • 1
    Do you want to be able to save the selected features to a new layer? If so just right click a layer in TOC and select 'Save only selected features'? – Jakob Mar 9 '16 at 7:23
  • There is also the Save selected features tool from the Processing plugin. – Joseph Mar 9 '16 at 13:31
  • But again, that's creating a NEW source even if it is stored as a temporary file. I'm wanting to subquery the original source. – Nathan Saylor Mar 9 '16 at 13:38
  • I think a similar approach would be to select some features, copy them and then go to Edit > Paste Features as > New Memory Vector Layer. – TobsenB Mar 9 '16 at 13:54
  • That is not pointing to the original data source; that is being stored in memory. Any changes I make to it do not save to the original source. – Nathan Saylor Mar 9 '16 at 13:58
1

I've never seen this button anywhere else in QGIS, but I agree that it can be useful. Here is a Workaround (a bit of python...) to achieve what you are after.

Presuming you have selected something on a layer and this layer is selected in the layer tree (python speaking: iface.activeLayer()) you can do on the python console:

l = iface.activeLayer()
l.setSubsetString('FID in (%s)' % ','.join(['%d' % f.id() for f in l.selectedFeatures()]))

Then you can duplicate the layer (if needed) and remove the subset string from the original one via:

l.setSubsetString('')

This can easily be put in a geoprocessing script, e.g.

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.