I'm trying to create a QGIS app using Python, where some layers are only visible at certain scales.

My code for the renderer is:

    ROAD_RULES = (('Freeway', "CLASS_CODE = 0", 'dark green', 0.2, Qt.SolidLine, None),
        ('Main', "CLASS_CODE IN (1, 2, 3)", 'black', 0.2, Qt.SolidLine, None),
        ('Minor', "CLASS_CODE IN (4, 5)", 'dark gray', 0.15, Qt.SolidLine, (0, 2000000000)),
        ('Track', "CLASS_CODE IN (6, 7, 11, 12)", 'dark gray', 0.15, Qt.DashLine, (0, 2000000000)),
        ('Proposed', "CLASS_CODE = 9", 'gray', 0.15, Qt.DotLine, (0, 2000000000)))

    # roads
    # create new rule-based renderer and get root rule
    symbol = QgsSymbolV2.defaultSymbol(self.roadLayer.geometryType())
    renderer = QgsRuleBasedRendererV2(symbol)
    root_rule = renderer.rootRule()

    # based on rules, create clones and set their details
    for label, expression, color_name, width, style, scale in ROAD_RULES:
        rule = root_rule.children()[0].clone()
        symbol_layer = QgsSimpleLineSymbolLayerV2()
        if scale is not None: 

    # delete default rule and apply renderer

This works OK as long as I only zoom in and out of my map canvas using self.mapCanvas.zoomIn() and self.mapCanvas.zoomOut() methods. But if I zoom in and out using the mouse wheel, or if I pan, the scale changes and the maximum and minimum scales no longer work as intended.

When I say 'the scale changes', I mean that even when the map later looks the same on screen, mapCanvas.scale() will report a different scale figure (sometimes orders of magnitude different).

Is there a way to stop this, so that scale-dependent layers operate consistently in my app as they would in QGIS itself?

  • After applying the symbol rule, how does the scale dependent setting reflect in the layer>Properties>General tab, Scale dependent visibility option? – artwork21 Dec 5 '16 at 20:05
  • Not sure - I'm trying to run it in an app that I'm creating rather than in the main QGIS program, so I don't think I have access to those tabs and options. – Steve Dec 7 '16 at 12:22

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.