Is it possible to access the QGIS Map Canvas as a variable or expression value to be used in the field calculator or labelling engine?

(i.e. so that you can create an expression that selects/interrogates features that are visible/present in a particular view of the canvas in a similar way that the print composer 'filters the legend by map content')

(QGIS 2.18.1 [osgeo4w], win7)

Potential (fictional) use case 1 - There are 50 polygon's in a layer, currently there are 20 polygon's visible in the current view of the map canvas, I would like to style the polygon's blue if there are <= 25 polygon's visible or style them green if there are >= 25 polygon's visible.

Potential (fictional) use case 2 - There are 50 polygon's in a layer, currently there are 10 polygon's visible, 5 that have an attribute value of 'House' in the "Building Type" field/column, and 5 that have the value of 'Garage' in the "Building Type" field/column, visible in the current view of the map canvas. Return: the value equalling the number of polygon's that are garages ... answer = 5.

  • 3
    You have some variables (map_extent_center/height/width, map_id, map_scale, map_rotation) Otherwise, in master, there is a is_selected function to know if a feature is selected. Might be worth to have more details on what you would like to do. Commented Dec 19, 2016 at 13:49
  • Thanks @Denis Rouzaud. I have made use of some of those variables in various contexts. This question was asked as a simplified attempt to solve the problem (or one part thereof) within another question I asked here: gis.stackexchange.com/questions/218508/…
    – guestagain
    Commented Dec 21, 2016 at 3:58
  • @DenisRouzaud Would you like to post that as an answer? Otherwise I'm tempted to close this as duplicate of gis.stackexchange.com/questions/218508/…
    – underdark
    Commented Jan 19, 2017 at 20:52
  • Let's close it. The question is a bit vague. Commented Jan 19, 2017 at 21:08
  • 1
    I don't think the question is vague nor simply a duplicate of the other linked but more detailed question. Indeed, it specifically seeks a solution that isn't solvable by the expression variables listed in the first comment, and a resolution to the query is not limited to the use case in the linked question. ... And there seem to be a few others interested in an answer to the question who must have their own applications for such functionality or a currently possible work-around.
    – guestagain
    Commented Jan 19, 2017 at 23:30

1 Answer 1


One tool to go beyond the variables currently available would be a python custom expression.

To my knowledge, this is the only currently-available tool to access the map canvas in the way you describe and use that info in expressions.

To solve your fictional use case 1 you could do something like this:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def visibleFeaturesCount(layerName, feature, parent):
    lyr = QgsMapLayerRegistry.instance().mapLayersByName(layerName)[0]
    extentRect = iface.mapCanvas().extent()
    request = QgsFeatureRequest().setFilterRect(extentRect)
    feats = lyr.getFeatures(request)
    count = 0
    for feat in feats:
        count += 1
    return count

Then, you could set up rule-based styles using filters like:

visibleFeaturesCount('mylayer') > 20

The full canvas API can be found here.

  • Thanks very much @Jesse McMillan. It seems like a very helpful answer which I will test in more detail as soon as I get a brake from another project. A quick test with pasting the above into a "Select by Expression" dialog as per the linked tutorial didn't work as the dialog had no options/buttons to save and or run the script in the way the tutorial described and as displayed in its screen shots. New to using python scripts (or whatever they're properly called) but can't wait to start to get my head around it. Where do you expect that I would define/set up the variable using the code above?
    – guestagain
    Commented Feb 1, 2017 at 1:51
  • It's a little confusing, but what you do is select the 'load' button on the custom expressions tab. Once it's loaded, the additional command will show in your available qgis expressions.
    – user52245
    Commented Feb 1, 2017 at 1:52

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.