7

I have several layers which are stagged on each other. I need to create a label (or text-window / description, doesn't matter) which should contain values from different layers. Creating a regular label I can only choose attributes from this layer. Because the features have different nodes, it is not possible to just merge the layers.

  • Yes,but I can only select fields and values from the layer you are working on. I need to create a label / textwindow containing values from multiple layers. – KReisser Feb 25 at 13:28
  • I think it is not possible, because QGIS can not know where the "imported" feature is, therefore not choose the right value from the table. That's why I asked another question: how to split lines and merge the attributes – KReisser Feb 25 at 13:34
10

You can create a virtual layer that will compute the intersection between the layers and retrieve the desired values to be labeled. Then you would put a label on this virtual layer.

Here is an example between two line layers, but this approach can be generalized to other types.

Go to the menu Layer / Add layer / Add - Edit virtual layer and put the following definition:

SELECT a.myField nameA, b.myField nameB, st_intersection(a.geometry, b.geometry)
FROM myLayer1 a, myLayer2 b
WHERE st_intersects(a.geometry, b.geometry)

This will create a new layer. Label it using the formula nameA || nameB or any other one that would concatenate the information to be shown.

enter image description here

  • 1
    That is magnificent! – Taras Feb 25 at 13:41
  • @Taras :-) Virtual layers are truly powerful! I believe they should be much more advertised – JGH Feb 25 at 13:43
  • Exactly! Once @J.R gave me a hint and afterwards I started to use them by myself. It helps a lot! – Taras Feb 25 at 13:50
  • Wow that looks promising. Thank you very much! I tried it but it says "Referenced table a in query not found" . what are the variables to be changed? nameA/nameB are my layer-names, what else? – KReisser Feb 25 at 13:58
  • 1
    @KReisser no, you need to change FROM a,b to your layer names (FROM myLayer1, myLayer2) . SELECT a.name -> change name to your field name (SELECT a.myField nameA ...) (I have updated the sample code) – JGH Feb 25 at 14:09
2

Have a look at the refFunctions Plugin. It allows you to create a reference to another layer and you can specify the field you want to reference:

Setup:

  • Point layer named points with id and field1 fields
  • Polygon layer named poly with id field

to Create the label for the poly layer I used this:

concat("id", ' with other ID: ', geomcontains('points', 'id') ,' and name :' ,  geomcontains('points', 'field1'))

Where:

  • "id" is the ID field of poly
  • geomcontains is the function from refFunctions
  • 'points' references my points layer and
  • 'id' is the ID field of points

Be aware this creates problems when you have many features inside one feature of the other layer (see gif for reference) enter image description here

  • Thank you, that seems helpful. But I need the label to be placed somewhere else, not on/near the feature, because it contains a lot of information. Usually I create a point-layer and link it to the layer with the values, where I create a point, the labels are displayed and are movable. But in this case I have two (or more) layers containing the requested values. Would that be possible with refFunctions? – KReisser Feb 25 at 15:12

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.