4

Inside a GeoPackage, I have a table without geometry that stores information about trees inventory. I also have a point layer representing trees. A single record in the inventory table can be related to one or more trees - this relationship is defined in the project.

The inventory table has fields that summarize data from the tree layer. I want these fields to be updated automatically based on the values in the tree layer. Specifically, I want to count the number of trees and list unique species by tree status.

For example, I have an inventory case with three trees: two to be cut down and one to be replanted. I want to display these counts separately and also show species names as a summary.

However, I cannot get the expression to evaluate correctly when applying a condition based on the "status" field. Instead, the total count is always 3, rather than being split into 2 and 1 depending on the tree status. Similarly, all species are listed together, whereas it should show "Pine" for trees to be cut down and "Spruce" for the tree to be replanted.

inventory - trees form

trees table

I am using the following expressions, but the filtering condition doesn't seem to work, and I can't figure out the correct syntax.

Expression for counting trees:

relation_aggregate(
    'inventory - trees',
    'count',
    $id,
    "status = 'cut down'"
)

Expression for displaying unique trees species:

array_to_string(
    array_sort(
        array_distinct(
            relation_aggregate(
                'inventory - trees',
                'array_agg',
                "species",
                "status = 'cut down'"
            )
        )
    ),
    ', '
)

1 Answer 1

4

In your expression, there are several problems:

  1. You have a wront syntax concering the use of quotes. It concerns the following part of the expression:

    "status = 'cut down'"
    

    Double quotes " are used to define fields (attributes), single quotes ' are used to define text strings. So the correct expression to define: field status equals text cut down is:

     "status" = 'cut down'
    
  2. The function relation_aggregate() has three mandatory arguments and two optional ones:

    relation_aggregate(relation, aggregate, expression, [concatenator=’’], [order_by])
    

    In your expression, the forth argument is not named, so it is not clear if it is used as concatenator or for odering. You seem to attempt to filter it, but relation_aggregate has not filter option. You should use another expression array_filter() to do so.

1
  • The expression needs to be created for inventory table, so I have to use relation_aggregate to obtain filed from related points layer, right? How to tell expression that it needs to retrive "status" filed of the element? This syntax is wrong array_filter( relation_aggregate( 'inventory - trees', 'count', $id ), element "status" = 'cut down' ) This returns wrong values array_to_string( array_filter( array_sort( array_distinct( relation_aggregate( 'inventory - trees', 'array_agg', "species" ) ) ), element ), ', ' ) Commented yesterday

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.