3

I want to filter my atlas pages based on a list returned by a custom function but it won't work.

This is my function:

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

@qgsfunction(args="auto", group='Custom')
def validZones(layerName, f1, feature, parent):

    lyr = QgsMapLayerRegistry.instance().mapLayersByName(layerName)[0]
    if lyr is None:
        raise Exception("Layer not found: " + layerName)

    expr = QgsExpression("\"f_%s\" IS NOT NULL" % (f1))
    uniqueVal = set([feat['Zone'] for feat in lyr.getFeatures(QgsFeatureRequest(expr))])
    okZone = list(uniqueVal)
    return okZone

And this is the filter expression:

"id_zone" IN validZones('myLayer', '32')

Trying to find the problem I've even tested the simplest function. Something like this:

@qgsfunction(args="auto", group='Custom')
def validZones(feature, parent):
    okZone = ['50','51']
    return okZone 

With this filter expression:

"id_zone" IN validZones()

But it doesn't work either.

And as this works:

"id_zone" IN ('50','51')

I also tried using a tuple but to no avail:

@qgsfunction(args="auto", group='Custom')
def validZones(feature, parent):
    okokZone = ('50','51')
    return okZone

What am I doing wrong?

  • The IN operator won't a accept a function no matter what I do. You must always pass a list like this (value1[, value2,...]). I decided to include the "id_zone" field as an argument to the function and evaluate it inside a for loop: for zone in okZone : if int(id_zone) == int(zone): return int(zone) Anyway, if anyone has any good idea please share it! – DMS02 Jan 3 '17 at 13:22
  • You should consider posting your comment as an answer :) – Joseph Jan 4 '17 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.