Class: QgsRuleBasedRenderer¶
- class qgis.core.QgsRuleBasedRenderer¶
Bases:
QgsFeatureRenderer
Rule based renderer.
When drawing a vector layer with rule-based renderer, it goes through the rules and draws features with symbols from rules that match.
QgsRuleBasedRenderer(root: QgsRuleBasedRenderer.Rule) Constructs the renderer from given tree of rules (takes ownership)
QgsRuleBasedRenderer(defaultSymbol:
QgsSymbol
) Constructor for convenience. Creates a root rule and adds a default rule with symbol (takes ownership)Methods
- param visitor:
- rtype:
bool
- rtype:
QgsFeatureRenderer.Capabilities
- param key:
- rtype:
QgsRuleBasedRenderer
Creates a new
QgsRuleBasedRenderer
from an existingrenderer
.helper function to convert the size scale and rotation fields present in some other renderers to data defined symbology
Creates a new rule-based renderer instance from XML
Creates a new rule based renderer from an SLD XML element.
- rtype:
str
- param fields:
- rtype:
bool
- param key:
- param feature:
- param key:
- rtype:
List[QgsLegendSymbolItem]
- rtype:
bool
- param feature:
take a rule and create a list of new rules based on the categories from categorized symbol renderer
take a rule and create a list of new rules based on the ranges from graduated symbol renderer
take a rule and create a list of new rules with intervals of scales given by the passed scale denominators
- param feature:
Render the
feature
with thesymbol
usingcontext
.render editing vertex marker at specified point
render editing vertex marker for a polygon
render editing vertex marker for a polyline
- rtype:
QgsRuleBasedRenderer.Rule
- param doc:
Saves generic renderer data into the specified
element
.- param key:
- param context:
- param context:
Returns symbol for current feature.
- param context:
- param feature:
- param doc:
- param context:
- param feature:
Attributes
- FeatDrawMarkers = 2¶
- FeatIsSelected = 1¶
- class FeatureFlags¶
Bases:
int
- class FeatureToRender(_f: QgsFeature, _flags: int)¶
- class FeatureToRender(QgsRuleBasedRenderer.FeatureToRender)
Bases:
sip.wrapper
- feat¶
- flags¶
- class RenderJob(_ftr: QgsRuleBasedRenderer.FeatureToRender, _s: QgsSymbol)¶
Bases:
sip.wrapper
Constructor for a render job, with the specified feature to render and symbol.
Note
The symbol ownership is not transferred.
QgsRuleBasedRenderer.RenderJob(QgsRuleBasedRenderer.RenderJob)
- ftr¶
- symbol¶
- class RenderLevel(z: int)¶
- class RenderLevel(other: QgsRuleBasedRenderer.RenderLevel)
Bases:
sip.wrapper
- jobs¶
- zIndex¶
- class Rule¶
Bases:
sip.wrapper
This class keeps data about a rules for rule-based renderer.
A rule consists of a symbol, filter expression and range of scales. If filter is empty, it matches all features. If scale range has both values zero, it matches all scales. If one of the min/max scale denominators is zero, there is no lower/upper bound for scales. A rule matches if both filter and scale range match.
QgsRuleBasedRenderer.Rule(symbol:
QgsSymbol
, maximumScale: int = 0, minimumScale: int = 0, filterExp: str = ‘’, label: str = ‘’, description: str = ‘’, elseRule: bool = False) Constructor takes ownership of the symbol- Filtered = 0¶
- Inactive = 1¶
- class RenderResult¶
Bases:
int
- Rendered = 2¶
- accept(self, visitor: QgsStyleEntityVisitorInterface) bool ¶
Accepts the specified symbology
visitor
, causing it to visit all child rules associated with the rule.Returns
True
if the visitor should continue visiting other objects, orFalse
if visiting should be canceled.New in version 3.10.
- Parameters:
visitor (QgsStyleEntityVisitorInterface) –
- Return type:
bool
- active(self) bool ¶
Returns if this rule is active
- Return type:
bool
- Returns:
True
if the rule is active
- appendChild(self, rule: QgsRuleBasedRenderer.Rule)¶
add child rule, take ownership, sets this as parent
- Parameters:
rule (QgsRuleBasedRenderer.Rule) –
- children(self) List[QgsRuleBasedRenderer.Rule] ¶
Returns all children rules of this rule
- Return type:
- Returns:
A list of rules
- clone(self) QgsRuleBasedRenderer.Rule ¶
clone this rule, return new instance
- Return type:
- collectZLevels(self) object ¶
Gets all used z-levels from this rule and children
- Return type:
object
- create(ruleElem: QDomElement, symbolMap: object, reuseId: bool = True) QgsRuleBasedRenderer.Rule ¶
Create a rule from an XML definition
- Parameters:
ruleElem (QDomElement) – The XML rule element
symbolMap (object) – Symbol map
reuseId (bool = True) – set to
True
to create an exact copy of the original symbol orFalse
to create a new rule with the same parameters as the original but a new uniqueruleKey()
. (Since QGIS 3.30)
- Return type:
- Returns:
A new rule
- createFromSld(element: QDomElement, geomType: Qgis.GeometryType) QgsRuleBasedRenderer.Rule ¶
Create a rule from the SLD provided in element and for the specified geometry type.
- Parameters:
element (QDomElement) –
geomType (Qgis.GeometryType) –
- Return type:
- dependsOnScale(self) bool ¶
- Return type:
bool
- descendants(self) List[QgsRuleBasedRenderer.Rule] ¶
Returns all children, grand-children, grand-grand-children, grand-gra… you get it
- Return type:
- Returns:
A list of descendant rules
- description(self) str ¶
A human readable description for this rule
- Return type:
str
- Returns:
Description
- dump(self, indent: int = 0) str ¶
Dump for debug purpose
- Parameters:
indent (int = 0) – How many characters to indent. Will increase by two with every of the recursive calls
- Return type:
str
- Returns:
A string representing this rule
- filter(self) QgsExpression ¶
A filter that will check if this rule applies
- Return type:
- Returns:
An expression
- filterExpression(self) str ¶
A filter that will check if this rule applies
- Return type:
str
- Returns:
An expression
- findRuleByKey(self, key: str) QgsRuleBasedRenderer.Rule ¶
Try to find a rule given its unique key
- Parameters:
key (str) –
- Return type:
- hasActiveChildren(self) bool ¶
Returns
True
if the rule has any active children.Note
Must be called after
startRender()
New in version 3.30.
- Return type:
bool
- initFilter(self)¶
- insertChild(self, i: int, rule: QgsRuleBasedRenderer.Rule)¶
add child rule, take ownership, sets this as parent
- Parameters:
i (int) –
rule (QgsRuleBasedRenderer.Rule) –
- isElse(self) bool ¶
Check if this rule is an ELSE rule
- Return type:
bool
- Returns:
True
if this rule is an else rule
- isFilterOK(self, f: QgsFeature, context: QgsRenderContext = None) bool ¶
Check if a given feature shall be rendered by this rule
- Parameters:
f (QgsFeature) – The feature to test
context (
QgsRenderContext
= None) – The context in which the rendering happens
- Return type:
bool
- Returns:
True
if the feature shall be rendered
- isScaleOK(self, scale: float) bool ¶
Check if this rule applies for a given
scale
. Thescale
value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. If set to 0, it will always returnTrue
.- Return type:
bool
- Returns:
If the rule will be evaluated at this scale
- Parameters:
scale (float) –
- label(self) str ¶
- Return type:
str
- legendKeysForFeature(self, feature: QgsFeature, context: QgsRenderContext = None) Set[str] ¶
Returns which legend keys match the feature
- Parameters:
feature (QgsFeature) –
context (
QgsRenderContext
= None) –
- Return type:
Set[str]
- legendSymbolItems(self, currentLevel: int = -1) List[QgsLegendSymbolItem] ¶
- Parameters:
currentLevel (int = -1) –
- Return type:
List[QgsLegendSymbolItem]
- maximumScale(self) float ¶
Returns 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
See also
- Return type:
float
- minimumScale(self) float ¶
Returns the minimum map scale (i.e. most “zoomed out” 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 minimum scale visibility.
See also
See also
- Return type:
float
- needsGeometry(self) bool ¶
Returns
True
if this rule or one of its children needs the geometry to be applied.- Return type:
bool
- parent(self) QgsRuleBasedRenderer.Rule ¶
The parent rule
- Return type:
- Returns:
Parent rule
- removeChild(self, rule: QgsRuleBasedRenderer.Rule)¶
delete child rule
- Parameters:
rule (QgsRuleBasedRenderer.Rule) –
- removeChildAt(self, i: int)¶
delete child rule
- Parameters:
i (int) –
- renderFeature(self, featToRender: QgsRuleBasedRenderer.FeatureToRender, context: QgsRenderContext, renderQueue: Iterable[QgsRuleBasedRenderer.RenderLevel]) QgsRuleBasedRenderer.Rule.RenderResult ¶
Render a given feature, will recursively call subclasses and only render if the constraints apply.
- Parameters:
featToRender (QgsRuleBasedRenderer.FeatureToRender) – The feature to render
context (QgsRenderContext) – The rendering context
renderQueue (Iterable[QgsRuleBasedRenderer.RenderLevel]) – The rendering queue to which the feature should be added
- Return type:
- Returns:
The result of the rendering. In explicit if the feature is added to the queue or the reason for not rendering the feature.
- ruleKey(self) str ¶
Unique rule identifier (for identification of rule within renderer)
- Return type:
str
- rulesForFeature(self, feature: QgsFeature, context: QgsRenderContext = None, onlyActive: bool = True) List[QgsRuleBasedRenderer.Rule] ¶
Returns the list of rules used to render the feature in a specific context.
- Parameters:
feature (QgsFeature) – The feature for which rules have to be find
context (
QgsRenderContext
= None) – The rendering contextonlyActive (bool = True) –
True
to search for active rules only,False
otherwise
- Return type:
- save(self, doc: QDomDocument, symbolMap: object) QDomElement ¶
- Parameters:
doc (QDomDocument) –
symbolMap (object) –
- Return type:
QDomElement
- setActive(self, state: bool)¶
Sets if this rule is active
- Parameters:
state (bool) – Determines if the rule should be activated or deactivated
- setDescription(self, description: str)¶
Set a human readable description for this rule
- Parameters:
description (str) – Description
- setFilterExpression(self, filterExp: str)¶
Set the expression used to check if a given feature shall be rendered with this rule
- Parameters:
filterExp (str) – An expression
- setIsElse(self, iselse: bool)¶
Sets if this rule is an ELSE rule
- Parameters:
iselse (bool) – If
True
, this rule is an ELSE rule
- setLabel(self, label: str)¶
- Parameters:
label (str) –
- setMaximumScale(self, scale: float)¶
Sets the maximum map
scale
(i.e. most “zoomed in” scale) at which the rule will be active. Thescale
value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. Ascale
of 0 indicates no maximum scale visibility.See also
See also
- Parameters:
scale (float) –
- setMinimumScale(self, scale: float)¶
Sets the minimum map
scale
(i.e. most “zoomed out” scale) at which the rule will be active. Thescale
value indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. Ascale
of 0 indicates no minimum scale visibility.See also
See also
- Parameters:
scale (float) –
- setRuleKey(self, key: str)¶
Override the assigned rule key (should be used just internally by rule-based renderer)
- Parameters:
key (str) –
- setSymbol(self, sym: QgsSymbol)¶
Sets a new symbol (or
None
). Deletes old symbol.- Parameters:
sym (QgsSymbol) –
- startRender(self, context: QgsRenderContext, fields: QgsFields, filter: str) bool ¶
prepare the rule for rendering and its children (build active children array)
- Parameters:
context (QgsRenderContext) –
fields (QgsFields) –
filter (str) –
- Return type:
bool
- stopRender(self, context: QgsRenderContext)¶
Stop a rendering process. Used to clean up the internal state of this rule
- Parameters:
context (QgsRenderContext) – The rendering context
- symbols(self, context: QgsRenderContext = QgsRenderContext()) List[QgsSymbol] ¶
Note
available in Python bindings as symbol2
- Parameters:
context (
QgsRenderContext
= QgsRenderContext()) –- Return type:
List[QgsSymbol]
- symbolsForFeature(self, feature: QgsFeature, context: QgsRenderContext = None) List[QgsSymbol] ¶
tell which symbols will be used to render the feature
- Parameters:
feature (QgsFeature) –
context (
QgsRenderContext
= None) –
- Return type:
List[QgsSymbol]
- takeChild(self, rule: QgsRuleBasedRenderer.Rule) QgsRuleBasedRenderer.Rule ¶
take child rule out, set parent as
None
- Parameters:
rule (QgsRuleBasedRenderer.Rule) –
- Return type:
- takeChildAt(self, i: int) QgsRuleBasedRenderer.Rule ¶
take child rule out, set parent as
None
- Parameters:
i (int) –
- Return type:
- toSld(self, doc: QDomDocument, element: QDomElement, props: Dict[str, Any])¶
Saves the symbol layer as SLD
- Parameters:
doc (QDomDocument) –
element (QDomElement) –
props (Dict[str) –
- usedAttributes(self, context: QgsRenderContext) Set[str] ¶
Returns the attributes used to evaluate the expression of this rule
- Return type:
Set[str]
- Returns:
A set of attribute names
- Parameters:
context (QgsRenderContext) –
- willRenderFeature(self, feature: QgsFeature, context: QgsRenderContext = None) bool ¶
only tell whether a feature will be rendered without actually rendering it
- Parameters:
feature (QgsFeature) –
context (
QgsRenderContext
= None) –
- Return type:
bool
- accept(self, visitor: QgsStyleEntityVisitorInterface) bool ¶
- Parameters:
visitor (QgsStyleEntityVisitorInterface) –
- Return type:
bool
- canSkipRender(self) bool ¶
- Return type:
bool
- capabilities(self) QgsFeatureRenderer.Capabilities ¶
- Return type:
- checkLegendSymbolItem(self, key: str, state: bool = True)¶
- Parameters:
key (str) –
state (bool = True) –
- clone(self) QgsRuleBasedRenderer ¶
- Return type:
- convertFromRenderer(renderer: QgsFeatureRenderer, layer: QgsVectorLayer = None) QgsRuleBasedRenderer ¶
Creates a new
QgsRuleBasedRenderer
from an existingrenderer
.Since QGIS 3.20, the optional
layer
parameter is required for conversions of some renderer types.- Return type:
- Returns:
a new renderer if the conversion was possible, otherwise
None
.- Parameters:
renderer (QgsFeatureRenderer) –
layer (
QgsVectorLayer
= None) –
- convertSymbolRotation(symbol: QgsSymbol, field: str)¶
Note
this function is used to convert old rotations expressions to symbol level DataDefined angle
- convertSymbolSizeScale(symbol: QgsSymbol, method: Qgis.ScaleMethod, field: str)¶
Note
this function is used to convert old sizeScale expressions to symbol level DataDefined size
- convertToDataDefinedSymbology(symbol: QgsSymbol, sizeScaleField: str, rotationField: str = '')¶
helper function to convert the size scale and rotation fields present in some other renderers to data defined symbology
- Parameters:
symbol (QgsSymbol) –
sizeScaleField (str) –
rotationField (str = '') –
- create(element: QDomElement, context: QgsReadWriteContext) QgsFeatureRenderer ¶
Creates a new rule-based renderer instance from XML
- Parameters:
element (QDomElement) –
context (QgsReadWriteContext) –
- Return type:
- createFromSld(element: QDomElement, geomType: Qgis.GeometryType) QgsFeatureRenderer ¶
Creates a new rule based renderer from an SLD XML element.
- Parameters:
element (QDomElement) –
geomType (Qgis.GeometryType) –
- Return type:
- dump(self) str ¶
- Return type:
str
- filter(self, fields: QgsFields = QgsFields()) str ¶
- Parameters:
fields (
QgsFields
= QgsFields()) –- Return type:
str
- filterNeedsGeometry(self) bool ¶
- Return type:
bool
- legendKeyToExpression(self, key: str, layer: QgsVectorLayer) Tuple[str, bool] ¶
- Parameters:
key (str) –
layer (QgsVectorLayer) –
- Return type:
Tuple[str, bool]
- legendKeysForFeature(self, feature: QgsFeature, context: QgsRenderContext) Set[str] ¶
- Parameters:
feature (QgsFeature) –
context (QgsRenderContext) –
- Return type:
Set[str]
- legendSymbolItemChecked(self, key: str) bool ¶
- Parameters:
key (str) –
- Return type:
bool
- legendSymbolItems(self) List[QgsLegendSymbolItem] ¶
- Return type:
List[QgsLegendSymbolItem]
- legendSymbolItemsCheckable(self) bool ¶
- Return type:
bool
- originalSymbolsForFeature(self, feature: QgsFeature, context: QgsRenderContext) List[QgsSymbol] ¶
- Parameters:
feature (QgsFeature) –
context (QgsRenderContext) –
- Return type:
List[QgsSymbol]
- refineRuleCategories(initialRule: QgsRuleBasedRenderer.Rule, r: QgsCategorizedSymbolRenderer)¶
take a rule and create a list of new rules based on the categories from categorized symbol renderer
- Parameters:
initialRule (QgsRuleBasedRenderer.Rule) –
- refineRuleRanges(initialRule: QgsRuleBasedRenderer.Rule, r: QgsGraduatedSymbolRenderer)¶
take a rule and create a list of new rules based on the ranges from graduated symbol renderer
- Parameters:
initialRule (QgsRuleBasedRenderer.Rule) –
- refineRuleScales(initialRule: QgsRuleBasedRenderer.Rule, scales: Iterable[int])¶
take a rule and create a list of new rules with intervals of scales given by the passed scale denominators
- Parameters:
initialRule (QgsRuleBasedRenderer.Rule) –
scales (Iterable[int]) –
- renderFeature(self, feature: QgsFeature, context: QgsRenderContext, layer: int = -1, selected: bool = False, drawVertexMarker: bool = False) bool ¶
- Parameters:
feature (QgsFeature) –
context (QgsRenderContext) –
layer (int = -1) –
selected (bool = False) –
drawVertexMarker (bool = False) –
- Return type:
bool
- renderFeatureWithSymbol(self, feature: QgsFeature, symbol: QgsSymbol, context: QgsRenderContext, layer: int, selected: bool, drawVertexMarker: bool)¶
Render the
feature
with thesymbol
usingcontext
. Uselayer
to specify the symbol layer,selected
to specify if it should be rendered as selected anddrawVertexMarker
to specify if vertex markers should be rendered.
- renderVertexMarker(self, pt: QPointF | QPoint, context: QgsRenderContext)¶
render editing vertex marker at specified point
- renderVertexMarkerPolygon(self, pts: QPolygonF, rings: Iterable[QPolygonF], context: QgsRenderContext)¶
render editing vertex marker for a polygon
- renderVertexMarkerPolyline(self, pts: QPolygonF, context: QgsRenderContext)¶
render editing vertex marker for a polyline
- rootRule(self) QgsRuleBasedRenderer.Rule ¶
- Return type:
- save(self, doc: QDomDocument, context: QgsReadWriteContext) QDomElement ¶
- Parameters:
doc (QDomDocument) –
context (QgsReadWriteContext) –
- Return type:
QDomElement
- saveRendererData(self, doc: QDomDocument, element: QDomElement, context: QgsReadWriteContext)¶
Saves generic renderer data into the specified
element
.This method should be called in a subclass’
save()
implementation in order to store all common base class properties in the DOMelement
.New in version 3.22.
- setLegendSymbolItem(self, key: str, symbol: QgsSymbol)¶
- Parameters:
key (str) –
symbol (QgsSymbol) –
- startRender(self, context: QgsRenderContext, fields: QgsFields)¶
- Parameters:
context (QgsRenderContext) –
fields (QgsFields) –
- stopRender(self, context: QgsRenderContext)¶
- Parameters:
context (QgsRenderContext) –
- symbolForFeature(self, feature: QgsFeature, context: QgsRenderContext) QgsSymbol ¶
Returns symbol for current feature. Should not be used individually: there could be more symbols for a feature
- Parameters:
feature (QgsFeature) –
context (QgsRenderContext) –
- Return type:
- symbols(self, context: QgsRenderContext) List[QgsSymbol] ¶
- Parameters:
context (QgsRenderContext) –
- Return type:
List[QgsSymbol]
- symbolsForFeature(self, feature: QgsFeature, context: QgsRenderContext) List[QgsSymbol] ¶
- Parameters:
feature (QgsFeature) –
context (QgsRenderContext) –
- Return type:
List[QgsSymbol]
- toSld(self, doc: QDomDocument, element: QDomElement, props: Dict[str, Any] = {})¶
- Parameters:
doc (QDomDocument) –
element (QDomElement) –
props (Dict[str) –
- usedAttributes(self, context: QgsRenderContext) Set[str] ¶
- Parameters:
context (QgsRenderContext) –
- Return type:
Set[str]
- willRenderFeature(self, feature: QgsFeature, context: QgsRenderContext) bool ¶
- Parameters:
feature (QgsFeature) –
context (QgsRenderContext) –
- Return type:
bool