Class: QgsMapBoxGlStyleConverter

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.

Added in version 3.16.

Enums

PropertyType

Property types, for interpolated value conversion

Methods

convert

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

createSubLayers

Returns a list of new map layers corresponding to sublayers of the style, e.g. raster layers.

errorMessage

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

labeling

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

parseLayers

Parse list of layers from JSON.

parseRasterSource

Parse a raster source from JSON.

parseSources

Parse list of sources from JSON.

rasterSubLayers

Returns a list of raster sub layers contained in the style.

renderer

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

sources

Returns the list of converted sources.

warnings

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.

Static Methods

colorAsHslaComponents

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

interpolateExpression

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

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.

parseLabelStops

Parses a list of interpolation stops containing label values.

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.

parseStepList

Parses and converts a match function value list.

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. an interpolate list).

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

retrieveSpriteAsBase64WithProperties

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

Attributes

NoLayerList

Success

class qgis.core.QgsMapBoxGlStyleConverter[source]

Bases: object

__init__()

Constructor for QgsMapBoxGlStyleConverter.

NoLayerList = 1
class PropertyType(*values)

Bases: IntEnum

Property types, for interpolated value conversion

Warning

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

  • Color: Color property

  • Numeric: Numeric property (e.g. line width, text size)

  • Opacity: Opacity property

  • Point: Point/offset property

  • NumericArray: Numeric array for dash arrays or such

Color = 0
Numeric = 1
NumericArray = 4
Opacity = 2
Point = 3
class Result

Bases: int

Success = 0
static colorAsHslaComponents(color: QColor | Qt.GlobalColor)[source]

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

Parameters:
  • color (Union[QColor, Qt.GlobalColor]) -> (int) – 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

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

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.

Parameters:
Return type:

QgsMapBoxGlStyleConverter.Result

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

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

createSubLayers(self) List[QgsMapLayer]

Returns a list of new map layers corresponding to sublayers of the style, e.g. raster layers.

The caller takes ownership of the returned layers.

Added in version 3.28.

Return type:

List[QgsMapLayer]

errorMessage(self) str[source]

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

static interpolateExpression(zoomMin: float, zoomMax: float, valueMin: Any, valueMax: Any, base: float, multiplier: float = 1, contextPtr: QgsMapBoxGlStyleConversionContext | None = None) str[source]

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:
Return type:

str

labeling(self) QgsVectorTileLabeling | None[source]

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:

Optional[QgsVectorTileLabeling]

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

Takes numerical arrays from stops.

Warning

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

Parameters:
Return type:

str

static parseCapStyle(style: str | None) Qt.PenCapStyle[source]

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 (Optional[str])

Return type:

Qt.PenCapStyle

static parseCircleLayer(jsonLayer: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext)[source]

Parses a circle layer.

Warning

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

Parameters:
Returns:

  • True if the layer was successfully parsed.

  • style: generated QGIS vector tile style

static parseColor(color: Any, context: QgsMapBoxGlStyleConversionContext) QColor[source]

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

static parseColorExpression(colorExpression: Any, context: QgsMapBoxGlStyleConversionContext) str[source]

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

Added in version 3.22.

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

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

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

Parses a fill layer.

Warning

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

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

  • context (QgsMapBoxGlStyleConversionContext) – conversion context

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

Returns:

  • True if the layer was successfully parsed.

  • style: generated QGIS vector tile style

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

Parses a numeric value which is interpolated by zoom range.

Parameters:
Returns:

  • QgsProperty representing interpolation settings

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

static parseInterpolateColorByZoom(json: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext)[source]

Parses a color value which is interpolated by zoom range.

Parameters:
Returns:

  • QgsProperty representing interpolation settings

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

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

Interpolates a list which starts with the interpolate function.

Warning

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

Added in version 3.40.

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

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

static parseInterpolatePointByZoom(json: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext, multiplier: float = 1)[source]

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:
static parseInterpolateStringByZoom(json: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext, conversionMap: Dict[str, Any])[source]

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:
static parseJoinStyle(style: str | None) Qt.PenJoinStyle[source]

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 (Optional[str])

Return type:

Qt.PenJoinStyle

static parseLabelStops(stops: Iterable[Any], context: QgsMapBoxGlStyleConversionContext) str[source]

Parses a list of interpolation stops containing label values.

Parameters:
Return type:

str

Returns:

converted expression

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

Parse list of layers from JSON.

Warning

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

Parameters:
static parseLineLayer(jsonLayer: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext)[source]

Parses a line layer.

Warning

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

Parameters:
Returns:

  • True if the layer was successfully parsed.

  • style: generated QGIS vector tile style

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

Parses and converts a match function value list.

Warning

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

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

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

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

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

parseRasterSource(self, source: Dict[str, Any], name: str | None, context: QgsMapBoxGlStyleConversionContext | None = None)[source]

Parse a raster source from JSON.

Warning

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

Added in version 3.28.

Parameters:
parseSources(self, sources: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext | None = None)[source]

Parse list of sources from JSON.

Warning

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

Added in version 3.28.

Parameters:
static parseStepList(json: Iterable[Any], type: QgsMapBoxGlStyleConverter.PropertyType, context: QgsMapBoxGlStyleConversionContext, multiplier: float = 1, maxOpacity: int = 255)[source]

Parses and converts a match function value list.

Warning

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

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

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

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

Parses a list of interpolation stops containing string values.

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

  • context (QgsMapBoxGlStyleConversionContext) – conversion context

  • conversionMap (Dict[str, Any]) -> (str) – map of input string to output expression value

Returns:

  • converted expression

  • defaultString: reasonable default value taken from stops

static parseSymbolLayer(jsonLayer: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext)[source]

Parses a symbol layer as renderer or labeling.

Warning

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

Parameters:
Returns:

  • rendererStyle: generated QGIS vector tile style

  • hasRenderer: True if symbol layer generated a renderer style

  • labelingStyle: generated QGIS vector tile labeling

  • hasLabeling: True if symbol layer generated a labeling style

static parseSymbolLayerAsRenderer(jsonLayer: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext)[source]

Parses a symbol layer as a renderer

Warning

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

Parameters:
Returns:

  • True if symbol layer was converted to renderer

  • rendererStyle: generated QGIS vector tile style

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

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:
rasterSubLayers(self) List[QgsMapBoxGlStyleRasterSubLayer]

Returns a list of raster sub layers contained in the style.

Added in version 3.28.

Return type:

List[QgsMapBoxGlStyleRasterSubLayer]

renderer(self) QgsVectorTileRenderer | None[source]

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:

Optional[QgsVectorTileRenderer]

static retrieveSprite(name: str | None, context: QgsMapBoxGlStyleConversionContext, spriteSize: QSize) QImage[source]

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

static retrieveSpriteAsBase64(value: Any, context: QgsMapBoxGlStyleConversionContext) str[source]

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

static retrieveSpriteAsBase64WithProperties(value: Any, context: QgsMapBoxGlStyleConversionContext)[source]

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.

Added in version 3.40.

Parameters:
sources(self) List[QgsMapBoxGlStyleAbstractSource]

Returns the list of converted sources.

Added in version 3.28.

Return type:

List[QgsMapBoxGlStyleAbstractSource]

warnings(self) List[str][source]

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]