2

I have expressions for a table field that has a map layer named as a data source file (building_ex.shp). I plan to save this setting as a style and use it in other projects, but in other projects the layer with this data source file can be named differently (BUILDINGS; real estate; facilities).

Is it possible to make this expression more universal, so as not to change it in each project?

enter image description here

1
  • 1
    Did you consider "project variables"? You can access them by using @project_variable - perhaps there is a hidden solution?
    – MartinMap
    Mar 9 at 8:59

1 Answer 1

3

You could do something like this. Admittedly, the expression can probably be cleaned up.

with_variable('existing_layer_name',
    with_variable('options', array('buildings', 'facilities', 'real estate'),  -- pre-determined list of layer names 
        with_variable('layer_names', array_foreach(@layers, layer_property(@element, 'name')),

            array_get(@options,
                array_filter(
                    array_foreach(
                        @layer_names,
                        array_find(@options, @element)
                    ),                      
                    @element > 0
                )[0]
            )
        )
    ),

    -- the original expression
    aggregate(
        layer:=@existing_layer_name,    -- the result of the matching procedure above
        aggregate:='sum',
        expression:="value"
    )
)

It basically matches the layer names in the project against a pre-determined list. If more than one match is found then it uses the first one.

array_find returns the index of the found element, and array_get uses that index to pull the layer name from the array of 'options'. array_filter is used to remove any -1 returned by array_find (non-matching).

The following two screenshots omit the original expression to demonstrate what is returned by the match.

enter image description here

enter image description here

Your Answer

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

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