QGIS 3.28 LTR
I am trying to write an expression for a calculated field to return an attribute from at non-spatial layer.
The calculated field is on the layer 'tree-pt'.
The target attribute is on the layer 'photos', which amongst others has attributes "path", "tag", "fk_layer" and "fk_uuid". The latter two attributes are used in the polymprhic relation between the layers 'tree-photo_tree-pt'
There may be several photos related to a tree-pt. I want the value of the attribute "path", where "tag"='T'. I can't seem to find an expression that allows me to do this.
relation_aggregate() doesn't work because it doesn't have a filter to narrow the results to only those with "tag"='T'.
When I use aggregate(), which has a filter expression, I can't get the filter to work when trying to match "uuid" from 'tree-pt" to "fk_uuid" from 'photos'.
The following expressions find no match when I know there is one.
aggregate(
'photos',
'array_agg',
"path",
"fk_uuid"=uuid)[0]
aggregate(
'photos',
'array_agg',
"path",
"fk_uuid"='uuid')[0]
aggregate(
'photos',
'array_agg',
"path",
"fk_uuid"="uuid")[0]
aggregate(
'photos',
'array_agg',
"path",
"fk_uuid"=attribute(@feature,'uuid')[0]
If I enter a known uuid match, in the following format, it returns the desired result:
aggregate(
'photos',
'array_agg',
"path",
"fk_uuid"='{12345678-2x22-3y3y-z123-xxx12y3za45b}')[0]
How can I get the filter to work on uuid? Or, is there another function I can use for this?
1 Answer 1
I've found a solution:
with_variable(
'myArray',
relation_aggregate('tree-photo_tree-pt','array_agg',array("select", "path")),
@myArray[array_find(array_foreach(@myArray,array_find(@element,'A')),0)][1]
)
I find this hard to descibe in plain english. The relation_aggregate expression returns an array, each element of which is an array of the relevant feature's attributes "select" and "path". This is named 'myArray' by virtue of the with_variable function.
I know that neither "path" cannot = 'T', which is the value I want to find in "select". So I can use array_foreach to test each feature's attribute array for a match to the value 'T'. This the index position of the first match or -1 for no match. I use this value in the square brackets to return the relvant record from myArray[], which is itself an array in the form ["select", "path"]. Adding [1] to the end of this returns "path" (zero based index).