6

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.

6
  • Try to specify the field in your query (replace ''SELECT *'' by ""SELECT field1, field2, ... , TABLENAME.geometry'')
    – J.R
    Sep 16, 2019 at 9:29
  • I've just tried with "select id, feature_name, my_layer.geometry from my_layer where "selected" = '1'" but virtual layer is still empty
    – Rexogis
    Sep 16, 2019 at 9:37
  • Maybe it didn't work with virtual field, try to select on another field to see if that work
    – J.R
    Sep 16, 2019 at 10:00
  • Works partially with another field : attribute table shows the item but no geometries appears on map... Seems to be two problems in one. 1. Impossibility to create a virtual layer based on a virtual field. 2. Maybe a problem with the formulation of the query to make the geometries appear.
    – Rexogis
    Sep 16, 2019 at 10:13
  • I tested it and geometry does appear (I use the ''Autodetect'' setting for the geometry option), it work with the * or by declaring all field.
    – J.R
    Sep 16, 2019 at 11:33

4 Answers 4

2

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.

3
  • Doesn't work either in 3.4. Tried the different steps you provided but all the features from the layer still appears on my map. Tried to export anyway to check if it wasn't a preview bug but got an error "Error while exporting atlas".
    – Rexogis
    Sep 17, 2019 at 8:57
  • if you download the latest nightly building then it wil work. Or just wait until the next release. I think it will be 3.8.4
    – PieterB
    Sep 17, 2019 at 10:09
  • Yes, the only problem is that I cannot manage myself all the upgrades. Every version of QGIS is to be controlled and customized by the IT service at my work so the chances I get 3.8.4 will certainly be in a few months at the best...
    – Rexogis
    Sep 17, 2019 at 11:25
2

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

1
  • Boolean virtual field updates to "true" when the feature is selected (non selected items stays empty). Integer-typ updates to "1" if selected, other are set to "0". But none of them seems to be usable in the virtual layer query.
    – Rexogis
    Sep 17, 2019 at 8:52
2

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.

0

I don´t know why, but try not to incorporate a layer, just fill your SQL query. I hope it could help and someone could explain the reason.

Your Answer

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

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