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.
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.
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:
- 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'))
- "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
attribute( get_feature( 'layer_wth_label_field','column_name_with_cross_value_within_that_layer', attribute( $currentfeature, 'corresponding_column_within_labeled_layer_containing_cross_value' )),'column_containing_labeling_text')
which essentially does:
- within a second layer (one containing text you want to appear on labels: 'layer_wth_label_field')
- find the right record by:
2a) from current feature (layer 1) get the cross value (common between both tables/layers, here: attribute( $currentfeature, 'corresponding_column_within_labeled_layer_containing_cross_value'
2b) find the corresponding record in the 2nd table/layer
for that record (in 2nd table) get the value from specific column
put it in the label of current feature