TransWikia.com

Creating a virtual layer based on a virtual field for dynamic selection in QGIS 3.4?

Geographic Information Systems Asked by Rexogis on May 3, 2021

Working on a PostGIS database, I would like to send to the print composer the selected features only. I’v tried several methods including working with is_selected() expression but the only way that actually works is constructing a filter on the table based on a list of values from attribute data which is quite tiresome.

I’v been trying to work with a virtual layer which could be dynamically updated with selected features from my source layer and that I could send to the print composer. I first set a virtual field on my source layer with the expression is_selected which returns 1 when the feature is selected. Then, to build the virtual layer, I first embed my source layer and then use a query on it :

SELECT * from my_layer where "selected" = '1'

“Selected” being my virtual field which is updated each time I select a new set of features.

However, even if the virtual layer toolbox is telling me there are no errors, the created virtual layer stays empty even if I have some selected features and my virtual field is returning ‘1’ value. There are no values shown in the attribute table and no geometry shown on map.

Maybe working with a virtual layer is not the way to do it and there is a simpler way, but I’v not found it yet.

3 Answers

Under 3.4.5, it seems there is a bug between virtual layers and virtual fields of type Boolean. (Tested against a Postgres table and a Shapefile)

If one create a boolean virtual field and set the value to true, then create a virtual layer, this field will be empty but not null.

The workaround is to create a virtual field of type integer, with the same expression is_selected(), and to use this field in the virtual layer

select * from mylayer where virtual_int_selected = 1;

Make sure to refresh the display (ex: pan a bit) after selecting a different feature

PS: reported as QGIS issue #31798

Answered by JGH on May 3, 2021

You can use another aproach, without using virtual layers and virtual fields.

  1. Go to the tab 'Atlas' in the print composer.

    • select 'layername' as coverage layer
    • Check 'Filter with' and use the expression is_selected()
  2. Go to 'item properties' and check 'controled by atlas'

  3. activate 'preview atlas'

  4. select some features and click 'export atlas'

-- edit --

This worked in 2.18.x, but not in 3.8.2. This bug is fixed (https://github.com/qgis/QGIS/issues/31807) and will be available in the next release.

Answered by PieterB on May 3, 2021

Try using the following expression for the virtual field (with name selected e.g.), type whole number (integer):

case when is_selected() then 1 else 0 end

When creating the virtual layer with this query:

select * from my_layer where selected = 1

it should work as expected. It did so in my case using QGIS 3.16.

Answered by bbsea on May 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP