QGIS – How to Select Features Intersecting Another Layer’s Selection

qgisselect-by-location

I have two large WFS layers in QGIS (2.16.3): one with polygons and the other with points. I've selected some of the features from the polygon layer and now I'd like to select all the features from the point layer that intersect only with the selected features from the polygon layer. How would I go about doing this?

In the past I had solved the problem by saving the polygon layer's selection as a new shapefile layer. Then I would select the features from the point layer that intersect with the new layer. The problem with this approach is that the size of the shapefiles can end up being very large. I also face the first world problem of having to delete the shapefile once I'm done using it for selecting the features from the point layer.

I had also tried to copy the selected polygons and then pasting them as a temporary scratch layer, but maybe QGIS doesn't like the WFS layer I'm using, or something, because depending on which polygons I've selected, it creates only some of the features for the temporary layer, refuses to create the layer or even crashes QGIS altogether.

I had also tried to use the intersects(), geomintersects() (refFunctions 1.0 plugin) and isselected() (Expressions+ plugin) functions when selecting from the point layer with "Select by expression", but couldn't figure it out.

In another similar question posted on this site it was recommended to use Vector->Research tools->Select by Location with the "Use selected features only" checkbox ticked, but as JaakL had commented, there is no such checkbox for me either. (I would have actually just commented on that question, instead of creating a new question, had I had enough reputation to allow commenting.)

Best Answer

VirtualLayers possibly to the first world problem rescue; this is a nice solution if you can express your polygon selection with some condition (I use an id <some_id> as an example here):

  • Layer -> Add Layer -> Add/Edit Virtual Layer...
  • Import -> both your WFS layers when loaded
    (you can try adding the service directly via Add -> select WFS as provider and paste in the URL, but it keeps on throwing errors for me on QGIS 2.18)

and then type

SELECT pts.*
FROM <point_layer> AS pts,
     <polygon_layer> AS plg
WHERE plg.<id_column> = <some_id>
  AND ST_Intersects(pts.geometry, plg.geometry);

into the Query field.

If you want to manually select features:

  • Edit -> Copy Features
  • Edit -> Past Features As -> Temporary Scratch Layer...

and continue with your usual workflow from there
(you could of course use that layer in the above query to get the intersecting points into a virtual layer; just omit plg.<id_column> = <some_id> AND in above query and replace <polygon_layer> with the name of the scratch layer).