5

When creating a virtual layer in QGIS, we can use the layer name of any existing layer in a query, like:

SELECT layer1.*, layer2.*
FROM layer1
JOIN layer2 ON layer1.id = layer2.id

However, layer names aren't necessarily unique. The docs say The query can use the name (or id) of existing vector layers. I assumed this would be the Identifier we can set in a layer's metadata options, but using these in a query results in a Referenced table layer_identifier in query not found! message. Nor is it possible to use the identifier in the Source definition of an embedded layer:

Query execution error on DROP TABLE IF EXISTS "table1"; CREATE VIRTUAL TABLE "table1" USING gsVLayer('spatialite','layer_identifier',UTF-8): 1 - Provider error`

We could use embedded layers, but then my problem is that we cannot use relative paths, as far as I can see (the project should be usable on different computers and OSes).

Is there a more robust mechanism than using layer names for creating a virtual layer?

Taras
35.8k5 gold badges77 silver badges151 bronze badges
asked Mar 18, 2018 at 17:35

1 Answer 1

5

You can get the layer ID from the layer properties / variables / layerID

Then the virtual layer can use it like

select * from lpr_000a16a_e20180319075213250

enter image description here

answered Mar 19, 2018 at 11:56
1
  • Ah yes. Upon seeing the metadata identifier field description popup it's clear that is meant for some external id. Too bad the layer_id variable isn't editable/overridable, it would be nice to get rid of the long generated string. Commented Mar 19, 2018 at 12:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.