Class: QgsCategorizedSymbolRenderer¶
- class qgis.core.QgsCategorizedSymbolRenderer(attrName: str = '', categories: Iterable[QgsRendererCategory] = [])¶
Bases:
QgsFeatureRenderer
Constructor for QgsCategorizedSymbolRenderer.
The
attrName
argument specifies the layer’s field name, or expression, which the categories will be matched against.A list of renderer
categories
can optionally be specified. If no categories are specified in the constructor, they can be added later by callingaddCategory()
.Methods
- param visitor:
Adds a new
category
to the renderer.- rtype:
QgsFeatureRenderer.Capabilities
Returns a list of all categories recognized by the renderer.
Returns the index of the category with the specified label (or -1 if the label was not found, or is not unique).
Returns the index for the category with the specified value (or -1 if not found).
- param key:
Returns the class attribute for the renderer, which is the field name or expression string from the layer which will be matched against the renderer categories.
- rtype:
QgsCategorizedSymbolRenderer
Creates a new
QgsCategorizedSymbolRenderer
from an existingrenderer
.Creates a categorized renderer from an XML
element
.Create categories for a list of
values
.Returns configuration of appearance of legend when using data-defined size for marker symbols.
Deletes all existing categories from the renderer.
Deletes the category with the specified index from the renderer.
- rtype:
str
- param fields:
- rtype:
bool
- rtype:
str
- param feature:
- param key:
- rtype:
List[QgsLegendSymbolItem]
- rtype:
bool
Replaces category symbols with the symbols from a
style
that have a matching name and symboltype
.Moves an existing category at index position from to index position to.
- param feature:
hashtable for faster access to symbols
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
- param doc:
Saves generic renderer data into the specified
element
.Sets the class attribute for the renderer, which is the field name or expression string from the layer which will be matched against the renderer categories.
Configures appearance of legend when renderer is configured to use data-defined size for marker symbols.
- param key:
Sets the source color ramp.
Sets the source symbol for the renderer, which is the base symbol used for the each categories' symbol before applying the categories' color.
Sorts the existing categories by their label.
Sorts the existing categories by their value.
Returns the source color ramp, from which each categories' color is derived.
Returns the renderer's source symbol, which is the base symbol used for the each categories' symbol before applying the categories' color.
- param context:
- param context:
- param feature:
Returns the matching symbol corresponding to an attribute
value
.Returns the matching symbol corresponding to an attribute
value
.- param context:
- param doc:
Changes the
label
for the category with the specified index.Changes the render state for the category with the specified index.
Changes the
symbol
for the category with the specified index.Changes the value for the category with the specified index.
Update the color ramp used and all symbols colors.
Update all the symbols but leave categories and colors.
- param context:
- accept(self, visitor: QgsStyleEntityVisitorInterface) bool ¶
- Parameters:
visitor (QgsStyleEntityVisitorInterface) –
- Return type:
bool
- addCategory(self, category: QgsRendererCategory)¶
Adds a new
category
to the renderer.See also
- Parameters:
category (QgsRendererCategory) –
- capabilities(self) QgsFeatureRenderer.Capabilities ¶
- Return type:
- categories(self) List[QgsRendererCategory] ¶
Returns a list of all categories recognized by the renderer.
- Return type:
List[QgsRendererCategory]
- categoryIndexForLabel(self, val: str) int ¶
Returns the index of the category with the specified label (or -1 if the label was not found, or is not unique).
New in version 2.5.
- Parameters:
val (str) –
- Return type:
int
- categoryIndexForValue(self, val: Any) int ¶
Returns the index for the category with the specified value (or -1 if not found).
- Parameters:
val (Any) –
- Return type:
int
- checkLegendSymbolItem(self, key: str, state: bool = True)¶
- Parameters:
key (str) –
state (bool = True) –
- classAttribute(self) str ¶
Returns the class attribute for the renderer, which is the field name or expression string from the layer which will be matched against the renderer categories.
See also
- Return type:
str
- clone(self) QgsCategorizedSymbolRenderer ¶
- Return type:
- convertFromRenderer(renderer: QgsFeatureRenderer, layer: QgsVectorLayer = None) QgsCategorizedSymbolRenderer ¶
Creates a new
QgsCategorizedSymbolRenderer
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
.
New in version 2.5.
- 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
- create(element: QDomElement, context: QgsReadWriteContext) QgsFeatureRenderer ¶
Creates a categorized renderer from an XML
element
.- Parameters:
element (QDomElement) –
context (QgsReadWriteContext) –
- Return type:
- createCategories(values: Iterable[Any], symbol: QgsSymbol, layer: QgsVectorLayer = None, fieldName: str = '') List[QgsRendererCategory] ¶
Create categories for a list of
values
. The returned symbols in the category list will be a modification ofsymbol
.If
layer
andfieldName
are specified it will try to find nicer values to represent the description for the categories based on the respective field configuration.New in version 3.6.
- Parameters:
values (Iterable[Any]) –
symbol (QgsSymbol) –
layer (
QgsVectorLayer
= None) –fieldName (str = '') –
- Return type:
List[QgsRendererCategory]
- dataDefinedSizeLegend(self) QgsDataDefinedSizeLegend ¶
Returns configuration of appearance of legend when using data-defined size for marker symbols. Will return
None
if the functionality is disabled.New in version 3.0.
- Return type:
- deleteAllCategories(self)¶
Deletes all existing categories from the renderer.
See also
- deleteCategory(self, catIndex: int) bool ¶
Deletes the category with the specified index from the renderer.
See also
- Parameters:
catIndex (int) –
- Return type:
bool
- 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
- legendClassificationAttribute(self) str ¶
- Return type:
str
- 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
- matchToSymbols(self, style: QgsStyle, type: Qgis.SymbolType, caseSensitive: bool = True, useTolerantMatch: bool = False) Tuple[int, List[Any], List[str]] ¶
Replaces category symbols with the symbols from a
style
that have a matching name and symboltype
.The
unmatchedCategories
list will be filled with all existing categories which could not be matched to a symbol instyle
.The
unmatchedSymbols
list will be filled with all symbol names fromstyle
which were not matched to an existing category.If
caseSensitive
isFalse
, then a case-insensitive match will be performed. IfuseTolerantMatch
isTrue
, then non-alphanumeric characters in style and category names will be ignored during the match.Returns the count of symbols matched.
New in version 3.4.
- Parameters:
style (QgsStyle) –
type (Qgis.SymbolType) –
caseSensitive (bool = True) –
useTolerantMatch (bool = False) –
- Return type:
Tuple[int, List[Any], List[str]]
- moveCategory(self, from_: int, to: int)¶
Moves an existing category at index position from to index position to.
- Parameters:
from (int) –
to (int) –
- originalSymbolForFeature(self, feature: QgsFeature, context: QgsRenderContext) QgsSymbol ¶
- Parameters:
feature (QgsFeature) –
context (QgsRenderContext) –
- Return type:
- rebuildHash(self)¶
hashtable for faster access to symbols
- 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
- 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.
- setClassAttribute(self, attr: str)¶
Sets the class attribute for the renderer, which is the field name or expression string from the layer which will be matched against the renderer categories.
See also
- Parameters:
attr (str) –
- setDataDefinedSizeLegend(self, settings: QgsDataDefinedSizeLegend)¶
Configures appearance of legend when renderer is configured to use data-defined size for marker symbols. This allows configuring for which values (symbol sizes) should be shown in the legend, whether to display different symbol sizes collapsed in one legend node or separated across multiple legend nodes etc.
When renderer does not use data-defined size or does not use marker symbols, these settings will be ignored. Takes ownership of the passed settings objects.
None
is a valid input that disables data-defined size legend.New in version 3.0.
- Parameters:
settings (QgsDataDefinedSizeLegend) –
- setLegendSymbolItem(self, key: str, symbol: QgsSymbol)¶
- Parameters:
key (str) –
symbol (QgsSymbol) –
- setSourceColorRamp(self, ramp: QgsColorRamp)¶
Sets the source color ramp.
- Parameters:
ramp (QgsColorRamp) – color ramp. Ownership is transferred to the renderer
See also
See also
- setSourceSymbol(self, sym: QgsSymbol)¶
Sets the source symbol for the renderer, which is the base symbol used for the each categories’ symbol before applying the categories’ color.
- Parameters:
sym (QgsSymbol) – source symbol, ownership is transferred to the renderer
See also
See also
- skipRender(self) QgsSymbol ¶
Deprecated since version No: longer used, will be removed in QGIS 4.0
- Return type:
- sortByLabel(self, order: Qt.SortOrder = Qt.AscendingOrder)¶
Sorts the existing categories by their label.
See also
- Parameters:
order (Qt.SortOrder = Qt.AscendingOrder) –
- sortByValue(self, order: Qt.SortOrder = Qt.AscendingOrder)¶
Sorts the existing categories by their value.
See also
- Parameters:
order (Qt.SortOrder = Qt.AscendingOrder) –
- sourceColorRamp(self) QgsColorRamp ¶
Returns the source color ramp, from which each categories’ color is derived.
See also
See also
- Return type:
- sourceSymbol(self) QgsSymbol ¶
Returns the renderer’s source symbol, which is the base symbol used for the each categories’ symbol before applying the categories’ color.
See also
See also
- Return type:
- 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 ¶
- Parameters:
feature (QgsFeature) –
context (QgsRenderContext) –
- Return type:
- symbolForValue(self, value: Any) QgsSymbol ¶
Returns the matching symbol corresponding to an attribute
value
.Deprecated since version use: variant which takes a second bool argument instead.
- Parameters:
value (Any) –
- Return type:
- symbolForValue2(self, value: Any) Tuple[QgsSymbol, bool] ¶
Returns the matching symbol corresponding to an attribute
value
.Will return
None
if no matching symbol was found forvalue
, or if the category corresponding tovalue
is currently disabled (seeQgsRendererCategory.renderState()
).If
foundMatchingSymbol
is specified then it will be set toTrue
if a matching category was found. This can be used to differentiate betweenNone
returned as a result of no matching category vsNone
as a result of disabled categories.Note
available in Python bindings as symbolForValue2
- Parameters:
value (Any) –
- Return type:
Tuple[
QgsSymbol
, bool]
- symbols(self, context: QgsRenderContext) List[QgsSymbol] ¶
- Parameters:
context (QgsRenderContext) –
- Return type:
List[QgsSymbol]
- toSld(self, doc: QDomDocument, element: QDomElement, props: Dict[str, Any] = {})¶
- Parameters:
doc (QDomDocument) –
element (QDomElement) –
props (Dict[str) –
- updateCategoryLabel(self, catIndex: int, label: str) bool ¶
Changes the
label
for the category with the specified index.A category’s label is used to represent the category within legends and the layer tree.
See also
See also
See also
- Parameters:
catIndex (int) –
label (str) –
- Return type:
bool
- updateCategoryRenderState(self, catIndex: int, render: bool) bool ¶
Changes the render state for the category with the specified index.
The render state indicates whether or not the category will be rendered, and is reflected in whether the category is checked with the project’s layer tree.
See also
See also
See also
New in version 2.5.
- Parameters:
catIndex (int) –
render (bool) –
- Return type:
bool
- updateCategorySymbol(self, catIndex: int, symbol: QgsSymbol) bool ¶
Changes the
symbol
for the category with the specified index.Ownership of
symbol
is transferred to the renderer.See also
See also
See also
- Parameters:
catIndex (int) –
symbol (QgsSymbol) –
- Return type:
bool
- updateCategoryValue(self, catIndex: int, value: Any) bool ¶
Changes the value for the category with the specified index.
If
value
is a list, then the category will match any of the values from this list.See also
See also
See also
- Parameters:
catIndex (int) –
value (Any) –
- Return type:
bool
- updateColorRamp(self, ramp: QgsColorRamp)¶
Update the color ramp used and all symbols colors.
- Parameters:
ramp (QgsColorRamp) – color ramp. Ownership is transferred to the renderer
New in version 2.5.
- updateSymbols(self, sym: QgsSymbol)¶
Update all the symbols but leave categories and colors. This method also sets the source symbol for the renderer.
- Parameters:
sym (QgsSymbol) – source symbol to use for categories. Ownership is not transferred.
See also
- usedAttributes(self, context: QgsRenderContext) Set[str] ¶
- Parameters:
context (QgsRenderContext) –
- Return type:
Set[str]