Class: QgsRuleBasedLabeling

Rule based labeling for a vector layer.

Class Hierarchy

Inheritance diagram of qgis.core.QgsRuleBasedLabeling

Base classes

QgsAbstractVectorLayerLabeling

Abstract base class - its implementations define different approaches to the labeling of a vector layer.

Methods

rootRule

Static Methods

create

Create the instance from a DOM element with saved configuration

class qgis.core.QgsRuleBasedLabeling[source]

Bases: QgsAbstractVectorLayerLabeling

__init__(root: QgsRuleBasedLabeling.Rule | None)

Constructs the labeling from given tree of rules (takes ownership)

Parameters:

root (Optional[QgsRuleBasedLabeling.Rule])

class Rule[source]

Bases: object

A child rule for QgsRuleBasedLabeling.

QgsRuleBasedLabeling.Rule(settings: Optional[QgsPalLayerSettings], maximumScale: float = 0, minimumScale: float = 0, filterExp: Optional[str] = ‘’, description: Optional[str] = ‘’, elseRule: bool = False) takes ownership of settings, settings may be None

Filtered = 0
Inactive = 1
class RegisterResult

Bases: int

Registered = 2
active(self) bool[source]

Returns if this rule is active

Return type:

bool

Returns:

True if the rule is active

appendChild(self, rule: QgsRuleBasedLabeling.Rule | None)[source]

add child rule, take ownership, sets this as parent

Parameters:

rule (Optional[QgsRuleBasedLabeling.Rule])

children(self) List[QgsRuleBasedLabeling.Rule]

Returns all children rules of this rule

Return type:

List[QgsRuleBasedLabeling.Rule]

Returns:

A list of rules

static create(ruleElem: QDomElement, context: QgsReadWriteContext, reuseId: bool = True) QgsRuleBasedLabeling.Rule | None[source]

Create a rule from an XML definition

Parameters:
  • ruleElem (QDomElement) – The XML rule element

  • context (QgsReadWriteContext) – reading context

  • reuseId (bool = True) – set to True to create an exact copy of the original symbol or False to create a new rule with the same parameters as the original but a new unique ruleKey(). (Since QGIS 3.30)

Return type:

Optional[QgsRuleBasedLabeling.Rule]

Returns:

A new rule

dependsOnScale(self) bool[source]

Determines if scale based labeling is active

Return type:

bool

Returns:

True if scale based labeling is active

descendants(self) List[QgsRuleBasedLabeling.Rule][source]

Returns all children, grand-children, grand-grand-children, grand-gra… you get it

Return type:

List[QgsRuleBasedLabeling.Rule]

Returns:

A list of descendant rules

description(self) str[source]

A human readable description for this rule

Return type:

str

Returns:

Description

filterExpression(self) str[source]

A filter that will check if this rule applies

Return type:

str

Returns:

An expression

findRuleByKey(self, key: str | None) QgsRuleBasedLabeling.Rule | None[source]

Try to find a rule given its unique key

Parameters:

key (Optional[str])

Return type:

Optional[QgsRuleBasedLabeling.Rule]

insertChild(self, i: int, rule: QgsRuleBasedLabeling.Rule | None)[source]

add child rule, take ownership, sets this as parent

Parameters:
isElse(self) bool[source]

Check if this rule is an ELSE rule

Return type:

bool

Returns:

True if this rule is an else rule

maximumScale(self) float[source]

Returns the maximum map scale (i.e. most “zoomed in” scale) at which the label rule will be active. The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. A scale of 0 indicates no maximum scale visibility.

See also

minimumScale()

Return type:

float

minimumScale(self) float[source]

Returns the minimum map scale (i.e. most “zoomed out” scale) at which the label rule will be active. The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. A scale of 0 indicates no minimum scale visibility.

See also

maximumScale()

Return type:

float

parent(self) QgsRuleBasedLabeling.Rule | None[source]

The parent rule

Return type:

Optional[QgsRuleBasedLabeling.Rule]

Returns:

Parent rule

removeChildAt(self, i: int)[source]

delete child rule

Parameters:

i (int)

ruleKey(self) str[source]

Unique rule identifier (for identification of rule within labeling, used as provider ID)

Return type:

str

setActive(self, state: bool)[source]

Sets if this rule is active

Parameters:

state (bool) – Determines if the rule should be activated or deactivated

setDescription(self, description: str | None)[source]

Set a human readable description for this rule

Parameters:

description (Optional[str]) – Description

setFilterExpression(self, filterExp: str | None)[source]

Set the expression used to check if a given feature shall be rendered with this rule

Parameters:

filterExp (Optional[str]) – An expression

setIsElse(self, iselse: bool)[source]

Sets if this rule is an ELSE rule

Parameters:

iselse (bool) – If True, this rule is an ELSE rule

setMaximumScale(self, scale: float)[source]

Sets the maximum map scale (i.e. most “zoomed in” scale) at which the rule will be active. The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. A scale of 0 indicates no maximum scale visibility.

See also

maximumScale()

Parameters:

scale (float)

setMinimumScale(self, scale: float)[source]

Sets the minimum map scale (i.e. most “zoomed out” scale) at which the label rule will be active. The scale value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. A scale of 0 indicates no minimum scale visibility.

See also

minimumScale()

Parameters:

scale (float)

setRuleKey(self, key: str | None)[source]

Override the assigned rule key (should be used just internally by rule-based labeling)

Parameters:

key (Optional[str])

static create(element: QDomElement, context: QgsReadWriteContext) QgsRuleBasedLabeling | None[source]

Create the instance from a DOM element with saved configuration

Parameters:
Return type:

Optional[QgsRuleBasedLabeling]

rootRule(self) QgsRuleBasedLabeling.Rule | None[source]
Return type:

Optional[QgsRuleBasedLabeling.Rule]