3

I have a memory layer loaded in the QGIS virtual layer in the data source manager. I am trying to run a very simple query:

select * from layer

Here is a snapshot: enter image description here

While this works for the physical layer, it does not work for the memory layer. They output virtual layer is blank, and I know that my memory layer has attribute data.

Is this simply a bug? I want to keep this a memory layer to reduce the number of steps I need to do to arrive at my final result. Should I resort to another tool or is there a workaround?

EDIT

Also found this quote in this link:

Creating a virtual layer with the layer_ref key allows to directly access already loaded QGIS layers (including memory layers).

  • 1
    Just tested right now : i had no problem of this kind (Q3.44) ... Are you sure of your data structure ? – snaileater Apr 19 at 9:28
  • 1
    what do you mean by data structure? I am just calling all columns in the table I loaded.... Using QGIS3.2.2 by the way – user32882 Apr 19 at 9:29
  • 1
    When u open its attribute table in the layer manager you see all the features (those u would like to get when querying it) ? Do u just get an empty answer or any error message ? Did u try a select count(*) from memory_layer ? – snaileater Apr 19 at 9:33
  • 1
    The resulting virtual layer is empty (no attributes, no fields). The original memory layer has plenty of attributes... I'm updating QGIS now to see if it has been fixed – user32882 Apr 19 at 9:36
  • 1
    Nope... doesn't work with QGIS 3.4.6... Are you sure you tested a memory layer? – user32882 Apr 19 at 9:43
2

You can find all your layers in the DB-manager. There (in my opinion) it is easier to access and query them. And you can load results as a new layer.

  • Go to DB-manager > Virtual Layers > QGIS layers

  • Select one of the layers and make your query. Note that the geometry is written as geometry and not geom.

enter image description here

  • The DB Manager is much better and could be a good segway into PostGIS, spatialite or other db-type gis systems. Do you know if the geometry column can be manipulated in here to do spatial queries as well? Perhaps I don't need that NNJoin plugin after all.... that would be nice – user32882 Apr 19 at 13:10
  • I never used the NNjoin before. So I do not know which functionality it has. But probably it would perform the same with spatialite or postgis – PieterB Apr 23 at 7:31
  • To answer your question @user32882 you can easily perform spatial joins and nearest neighbour queries in DB Manager using SQL. It may not be that fast if you are using shapefiles in DB manager, but it is dynamic and much more flexible (pick which columns to join, distance threshold for nearest neighbour, etc). If you use spatialite you can access the VirtualKNN feature directly within DB Manager. SQL is well worth exploring – she_weeds Apr 26 at 11:03
3

The upper section, embeded layers, allows listing (and using) layers that are not loaded in QGIS table of content.

Using an in-memory layer that is not loaded in QGIS is to be tested. I believe the layer is cleared from memory as soon as it is removed from the table of content.

If you do have the in-memory layer loaded in the table of content, you can simply remove it from the embeded layers section, and the query will work. I just reproduced your use case and workaround in 3.4.5

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.