1

The goal seems simple, the explanation not, but I try to:

On the one hand, I have a table with several objects ("objects_layer"). On the other hand is a table with a list-field ("list_layer").

I wan't to get the attributes ("prop1", "prop2", "prop3") from all objects in the objects_layer which are written in the "tellmeabout-field" in "list_layer". I need it as expression or virtual field/layer.

Example: enter image description here

The result I expect returns for example as new virtual field in list_layer like this:

enter image description here

Assume, someone is creating a new entry in "list_layer", and he want's to know the props of A and B as text, so he choose A and B from a list and the expression or virt. field is doing the rest:

enter image description here

4
  • 1
    QGIS 3.10 has an attributes function (not present in QGIS LTR 3.4) which may reduce expressions when all fields for a row are queried. Do you need the expression working in QGIS 3.4? Dec 5, 2019 at 10:55
  • 1
    :-D isn't this the 2rd incarnation of your initial question? Well, this time its a perfect TL;DR...
    – geozelot
    Dec 5, 2019 at 10:57
  • 1
    A 3.10 solution would be fine!
    – MartinMap
    Dec 5, 2019 at 11:13
  • @ThingumaBob, in this question a one-to-many relation is involved, and that open the possibility of get_feature function use. I think the development of the expression to this question is simpler than the other. Dec 5, 2019 at 15:32

1 Answer 1

1

You can create a virtual field with the following expression:

array_to_string(
  array_foreach(
    --Replace '{"A","B"}' to 'A,B' and create the array: [ A , B ].
    string_to_array(
      replace(
        "tellmeabout",
        array( '{', '"', '}'),
        array( '', '' , ''))),
    --Then, for each element of the previous array return a string containing:
    array_to_string(
      array_remove_at(
        --All the values of a map created with the attributes of the feature...
        map_avals(
          attributes(
            --from the 'objects_layer' layer, which 'object' value is equal to the...
            get_feature(
              'objects_layer',
              'object',
              --previous element,
              @element))),
        --remove the first value of the map ('fid' value),
        0))),
  --and concatenate the array elements whit a ' / ' character to a string.
  ' / ')

Replace '{"A","B"}' to 'A,B' and create the array: [ A , B ].
Then, for each element of the previous array return a string containing:
All the values of a map created with the attributes of the feature...
from the 'objects_layer' layer, which 'object' value is equal to the...
previous element,
remove the first value of the map ('fid' value),
and concatenate the array elements whit a ' / ' character to a string.


Notes and references:

  • The previous expression works fine with your sample data in QGIS 3.10. QGIS LTR 3.4 doesn't include the attributes function.

  • If you plan to arrange NULL values, some modifications must be done to handle them.

  • Thanks to J. Monticolo for the following answer: https://gis.stackexchange.com/a/343608/133276, which shows the array_foreach function.

  • Thanks to PieterB for the following answer: https://gis.stackexchange.com/a/343621/133276, which shows the attributes function.

Your Answer

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

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