I'd like to test whether a given feature is displayed, i.e. visible on the map canvas, inside a QGIS expression. Any way to do this?
I'm essentially looking for an analog to the is_selected()
function, which tests whether a feature has been selected, but querying whether it's displayed or not. For my use case, I'm more concerned about whether it meets the user's current filtering criteria in the layer tree (e.g. which categories are set to display in the categorized renderer) than whether it is within the displayed canvas extent, but I could live with both. I don't see such an analog in the list of functions in the QGIS documentation, however.
As further context, I'm building an expression that aggregates across points inside polygons. Simplified version is
aggregate(layer:='YetiSites',
aggregate:='sum',
expression:=NumberOfYetiObserved,
filter:=intersects($geometry,geometry(@parent)))
which adds up the number of yeti observed at all sites that within the zone defined by the current (polygon) feature. See below, where it's assumed this expression is used in the labels for the zone polygons.
However, the YetiSites
layer also has an attribute Observer
, and in the layer tree users can choose which observers' sightings they want to see on the map. If the display settings exclude, say, Bill's observations, I want my sum to exclude them too. So I'd like to be able to write something like
filter:=(intersects($geometry,geometry(@parent)) and is_displayed($currentfeature))
but I'm trying to find how to code the (ficticious) is_displayed(...)
part. Without this clause, I get the following, with 6 rather than the desired 5 shown for Zone A since the aggregation picks up Bill's point even though it is not being displayed.
I can of course emulate this by running a processing algorithm, or defining the display filter upstream and then using it both when displaying YetiSites
and in my aggregation expression. But I'd love to just be responsive to what the user is selecting in the layer tree to display.
is_layer_visible( )
function does not help in your case, right?is_layer_visible
answers whether the whole layer is visible; I need whether a specific feature on it is.