Class: QgsMapBoxGlStyleConverter

class qgis.core.QgsMapBoxGlStyleConverter

Bases: sip.wrapper

Handles conversion of MapBox GL styles to QGIS vector tile renderers and labeling settings.

Conversions are performed by calling convert() with either a JSON map or JSON string value, and then retrieving the results by calling renderer() or labeling() respectively.

New in version 3.16.

QgsMapBoxGlStyleConverter() Constructor for QgsMapBoxGlStyleConverter.

Methods

colorAsHslaComponents

Takes a QColor object and returns HSLA components in required format for QGIS color_hsla() expression function.

convert

Converts a JSON style map, and returns the resultant status of the conversion.

errorMessage

Returns a descriptive error message if an error was encountered during the style conversion, or an empty string if no error was encountered.

interpolateExpression

Generates an interpolation for values between valueMin and valueMax, scaled between the ranges zoomMin to zoomMax.

labeling

Returns a new instance of a vector tile labeling representing the converted style, or None if the style could not be converted successfully.

parseArrayStops

Takes numerical arrays from stops.

parseCapStyle

Converts a value to Qt.PenCapStyle enum from JSON value.

parseCircleLayer

Parses a circle layer.

parseColor

Parses a color in one of these supported formats:

parseColorExpression

Converts an expression representing a color to a string (can be color string or an expression where a color is expected)

parseExpression

Converts a MapBox GL expression to a QGIS expression.

parseFillLayer

Parses a fill layer.

parseInterpolateByZoom

Parses a numeric value which is interpolated by zoom range.

parseInterpolateColorByZoom

Parses a color value which is interpolated by zoom range.

parseInterpolateListByZoom

Interpolates a list which starts with the interpolate function.

parseInterpolateOpacityByZoom

Interpolates opacity with either scale_linear() or scale_exp() (depending on base value).

parseInterpolatePointByZoom

Interpolates a point/offset with either scale_linear() or scale_exp() (depending on base value).

parseInterpolateStringByZoom

Interpolates a string by zoom.

parseJoinStyle

Converts a value to Qt.PenJoinStyle enum from JSON value.

parseLayers

Parse list of layers from JSON.

parseLineLayer

Parses a line layer.

parseMatchList

Parses and converts a match function value list.

parseOpacityStops

Takes values from stops and uses either scale_linear() or scale_exp() functions to interpolate alpha component of color.

parsePointStops

Takes values from stops and uses either scale_linear() or scale_exp() functions to interpolate point/offset values.

parseStops

Parses a list of interpolation stops

parseStringStops

Parses a list of interpolation stops containing string values.

parseSymbolLayer

Parses a symbol layer as renderer or labeling.

parseSymbolLayerAsRenderer

Parses a symbol layer as a renderer

parseValueList

Parses and converts a value list (e.g.

renderer

Returns a new instance of a vector tile renderer representing the converted style, or None if the style could not be converted successfully.

retrieveSprite

Retrieves the sprite image with the specified name, taken from the specified context.

retrieveSpriteAsBase64

Retrieves the sprite image with the specified name, taken from the specified context as a base64 encoded value

warnings

Returns a list of user-friendly warnings generated during the conversion, e.g.

Attributes

Color

NoLayerList

Numeric

Opacity

Point

Success

Color = 0
NoLayerList = 1
Numeric = 1
Opacity = 2
Point = 3
class PropertyType

Bases: int

class Result

Bases: int

Success = 0
colorAsHslaComponents(color: QColor | Qt.GlobalColor) Tuple[int, int, int, int]

Takes a QColor object and returns HSLA components in required format for QGIS color_hsla() expression function.

Parameters:
  • color (Union[QColor) – input color

  • hue – an integer value from 0 to 360

  • saturation – an integer value from 0 to 100

  • lightness – an integer value from 0 to 100

  • alpha – an integer value from 0 (completely transparent) to 255 (opaque).

Warning

This is private API only, and may change in future QGIS versions

Return type:

Tuple[int, int, int, int]

convert(self, style: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext = None) QgsMapBoxGlStyleConverter.Result

Converts a JSON style map, and returns the resultant status of the conversion.

If an error occurs during conversion then a descriptive error message can be retrieved by calling errorMessage().

After conversion, the resultant labeling and style rules can be retrieved by calling renderer() or labeling() respectively.

The optional context argument can be set to use a specific context during the conversion.

convert(self, style: str, context: QgsMapBoxGlStyleConversionContext = None) -> QgsMapBoxGlStyleConverter.Result Converts a JSON style string, and returns the resultant status of the conversion.

If an error occurs during conversion then a descriptive error message can be retrieved by calling errorMessage().

After conversion, the resultant labeling and style rules can be retrieved by calling renderer() or labeling() respectively.

The optional context argument can be set to use a specific context during the conversion.

Parameters:
Return type:

QgsMapBoxGlStyleConverter.Result

errorMessage(self) str

Returns a descriptive error message if an error was encountered during the style conversion, or an empty string if no error was encountered.

See also

warnings()

Return type:

str

interpolateExpression(zoomMin: float, zoomMax: float, valueMin: Any, valueMax: Any, base: float, multiplier: float = 1, contextPtr: QgsMapBoxGlStyleConversionContext = None) str

Generates an interpolation for values between valueMin and valueMax, scaled between the ranges zoomMin to zoomMax.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
  • zoomMin (float) –

  • zoomMax (float) –

  • valueMin (Any) –

  • valueMax (Any) –

  • base (float) –

  • multiplier (float = 1) –

  • contextPtr (QgsMapBoxGlStyleConversionContext = None) –

Return type:

str

labeling(self) QgsVectorTileLabeling

Returns a new instance of a vector tile labeling representing the converted style, or None if the style could not be converted successfully.

Return type:

QgsVectorTileLabeling

parseArrayStops(stops: Iterable[Any], context: QgsMapBoxGlStyleConversionContext, multiplier: float = 1) str

Takes numerical arrays from stops.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

str

parseCapStyle(style: str) Qt.PenCapStyle

Converts a value to Qt.PenCapStyle enum from JSON value.

Warning

This is private API only, and may change in future QGIS versions

Parameters:

style (str) –

Return type:

Qt.PenCapStyle

parseCircleLayer(jsonLayer: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext) Tuple[bool, QgsVectorTileBasicRendererStyle]

Parses a circle layer.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

Tuple[bool, QgsVectorTileBasicRendererStyle]

Returns:

  • True if the layer was successfully parsed.

  • style: generated QGIS vector tile style

parseColor(color: Any, context: QgsMapBoxGlStyleConversionContext) QColor

Parses a color in one of these supported formats:

  • c #fff or c #ffffff

  • hsl(30, 19%, 90%) or hsla(30, 19%, 90%, 0.4)

  • rgb(10, 20, 30) or rgba(10, 20, 30, 0.5)

Returns an invalid color if the color could not be parsed.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

QColor

parseColorExpression(colorExpression: Any, context: QgsMapBoxGlStyleConversionContext) str

Converts an expression representing a color to a string (can be color string or an expression where a color is expected)

Parameters:
Return type:

str

Returns:

the QGIS expression string since QGIS 3.22

parseExpression(expression: Iterable[Any], context: QgsMapBoxGlStyleConversionContext, colorExpected: bool = False) str

Converts a MapBox GL expression to a QGIS expression.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

str

parseFillLayer(jsonLayer: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext, isBackgroundStyle: bool = False) Tuple[bool, QgsVectorTileBasicRendererStyle]

Parses a fill layer.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
  • jsonLayer (Dict[str) – fill layer to parse

  • context (QgsMapBoxGlStyleConversionContext) – conversion context

  • isBackgroundStyle (bool = False) – set to True if the layer should be parsed as background layer

Return type:

Tuple[bool, QgsVectorTileBasicRendererStyle]

Returns:

  • True if the layer was successfully parsed.

  • style: generated QGIS vector tile style

parseInterpolateByZoom(json: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext, multiplier: float = 1) Tuple[QgsProperty, float]

Parses a numeric value which is interpolated by zoom range.

Parameters:
  • json (Dict[str) – definition of interpolation

  • context (QgsMapBoxGlStyleConversionContext) – conversion context

  • multiplier (float = 1) – optional multiplication factor

Return type:

Tuple[QgsProperty, float]

Returns:

  • QgsProperty representing interpolation settings

  • defaultNumber: storage for a reasonable “default” number representing the overall property.

parseInterpolateColorByZoom(json: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext) Tuple[QgsProperty, QColor]

Parses a color value which is interpolated by zoom range.

Parameters:
Return type:

Tuple[QgsProperty, QColor]

Returns:

  • QgsProperty representing interpolation settings

  • defaultColor: storage for a reasonable “default” color representing the overall property.

parseInterpolateListByZoom(json: Iterable[Any], type: QgsMapBoxGlStyleConverter.PropertyType, context: QgsMapBoxGlStyleConversionContext, multiplier: float = 1, maxOpacity: int = 255) Tuple[QgsProperty, QColor, float]

Interpolates a list which starts with the interpolate function.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

Tuple[QgsProperty, QColor, float]

parseInterpolateOpacityByZoom(json: Dict[str, Any], maxOpacity: int, contextPtr: QgsMapBoxGlStyleConversionContext = None) QgsProperty

Interpolates opacity with either scale_linear() or scale_exp() (depending on base value). For json with intermediate stops it uses parseOpacityStops() function. It uses QGIS set_color_part() function to set alpha component of color.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

QgsProperty

parseInterpolatePointByZoom(json: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext, multiplier: float = 1) Tuple[QgsProperty, QPointF]

Interpolates a point/offset with either scale_linear() or scale_exp() (depending on base value). For json with intermediate stops it uses parsePointStops() function.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

Tuple[QgsProperty, QPointF]

parseInterpolateStringByZoom(json: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext, conversionMap: Dict[str, Any]) Tuple[QgsProperty, str]

Interpolates a string by zoom. For json with intermediate stops it uses parseStringStops() function.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

Tuple[QgsProperty, str]

parseJoinStyle(style: str) Qt.PenJoinStyle

Converts a value to Qt.PenJoinStyle enum from JSON value.

Warning

This is private API only, and may change in future QGIS versions

Parameters:

style (str) –

Return type:

Qt.PenJoinStyle

parseLayers(self, layers: Iterable[Any], context: QgsMapBoxGlStyleConversionContext = None)

Parse list of layers from JSON.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
parseLineLayer(jsonLayer: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext) Tuple[bool, QgsVectorTileBasicRendererStyle]

Parses a line layer.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

Tuple[bool, QgsVectorTileBasicRendererStyle]

Returns:

  • True if the layer was successfully parsed.

  • style: generated QGIS vector tile style

parseMatchList(json: Iterable[Any], type: QgsMapBoxGlStyleConverter.PropertyType, context: QgsMapBoxGlStyleConversionContext, multiplier: float = 1, maxOpacity: int = 255) Tuple[QgsProperty, QColor, float]

Parses and converts a match function value list.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

Tuple[QgsProperty, QColor, float]

parseOpacityStops(base: float, stops: Iterable[Any], maxOpacity: int, context: QgsMapBoxGlStyleConversionContext) str

Takes values from stops and uses either scale_linear() or scale_exp() functions to interpolate alpha component of color.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

str

parsePointStops(base: float, stops: Iterable[Any], context: QgsMapBoxGlStyleConversionContext, multiplier: float = 1) str

Takes values from stops and uses either scale_linear() or scale_exp() functions to interpolate point/offset values.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

str

parseStops(base: float, stops: Iterable[Any], multiplier: float, context: QgsMapBoxGlStyleConversionContext) str

Parses a list of interpolation stops

Parameters:
  • base (float) – interpolation exponent base

  • stops (Iterable[Any]) – definition of interpolation stops

  • multiplier (float) – optional multiplication factor

  • context (QgsMapBoxGlStyleConversionContext) – conversion context

Return type:

str

parseStringStops(stops: Iterable[Any], context: QgsMapBoxGlStyleConversionContext, conversionMap: Dict[str, Any]) Tuple[str, str]

Parses a list of interpolation stops containing string values.

Parameters:
  • stops (Iterable[Any]) – definition of interpolation stops

  • context (QgsMapBoxGlStyleConversionContext) – conversion context

  • conversionMap (Dict[str) – map of input string to output expression value

Return type:

Tuple[str, str]

Returns:

  • converted expression

  • defaultString: reasonable default value taken from stops

parseSymbolLayer(jsonLayer: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext) Tuple[QgsVectorTileBasicRendererStyle, bool, QgsVectorTileBasicLabelingStyle, bool]

Parses a symbol layer as renderer or labeling.

Warning

This is private API only, and may change in future QGIS versions

Parameters:
  • jsonLayer (Dict[str) – symbol layer to parse

  • rendererStyle – generated QGIS vector tile style

  • hasRenderer – will be set to True if symbol layer generated a renderer style

  • labelingStyle – generated QGIS vector tile labeling

  • context (QgsMapBoxGlStyleConversionContext) – conversion context

Return type:

Tuple[QgsVectorTileBasicRendererStyle, bool, QgsVectorTileBasicLabelingStyle, bool]

parseSymbolLayerAsRenderer(jsonLayer: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext) Tuple[bool, QgsVectorTileBasicRendererStyle]

Parses a symbol layer as a renderer

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

Tuple[bool, QgsVectorTileBasicRendererStyle]

Returns:

  • True if symbol layer was converted to renderer

  • rendererStyle: generated QGIS vector tile style

parseValueList(json: Iterable[Any], type: QgsMapBoxGlStyleConverter.PropertyType, context: QgsMapBoxGlStyleConversionContext, multiplier: float = 1, maxOpacity: int = 255) Tuple[QgsProperty, QColor, float]

Parses and converts a value list (e.g. an interpolate list).

Warning

This is private API only, and may change in future QGIS versions

Parameters:
Return type:

Tuple[QgsProperty, QColor, float]

renderer(self) QgsVectorTileRenderer

Returns a new instance of a vector tile renderer representing the converted style, or None if the style could not be converted successfully.

Return type:

QgsVectorTileRenderer

retrieveSprite(name: str, context: QgsMapBoxGlStyleConversionContext, spriteSize: QSize) QImage

Retrieves the sprite image with the specified name, taken from the specified context.

The context must have valid sprite definitions and images set via QgsMapBoxGlStyleConversionContext.setSprites() prior to conversion.

Parameters:
Return type:

QImage

retrieveSpriteAsBase64(value: Any, context: QgsMapBoxGlStyleConversionContext, spriteSize: QSize, spriteProperty: str, spriteSizeProperty: str) str

Retrieves the sprite image with the specified name, taken from the specified context as a base64 encoded value

The context must have valid sprite definitions and images set via QgsMapBoxGlStyleConversionContext.setSprites() prior to conversion.

Parameters:
Return type:

str

warnings(self) List[str]

Returns a list of user-friendly warnings generated during the conversion, e.g. as a result of MapBox GL style settings which cannot be translated to QGIS styles.

See also

errorMessage()

Return type:

List[str]