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.

Added in version 3.16.

QgsMapBoxGlStyleConverter() Constructor for QgsMapBoxGlStyleConverter.

Enums

PropertyType

Bases: enum.IntEnum

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.

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.

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.

parseLabelStops

Parses a list of interpolation stops containing label values.

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.

parseRasterSource

Parse a raster source from JSON.

parseSources

Parse list of sources from JSON.

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).

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.

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

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.

Attributes

NoLayerList

Success

staticMetaObject

NoLayerList = 1
class PropertyType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: enum.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

Color = 0
Numeric = 1
Opacity = 2
Point = 3
baseClass

alias of QgsMapBoxGlStyleConverter

class Result

Bases: int

Success = 0
colorAsHslaComponents(color: QColor | Qt.GlobalColor)

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

convert(self, style: Dict[str, Any], context: QgsMapBoxGlStyleConversionContext | None = 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: Optional[str], context: Optional[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

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

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

str

labeling(self) QgsVectorTileLabeling | None

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]

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

Return type:

Qt.PenCapStyle

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

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

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)

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) -> (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

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

Parses a numeric value which is interpolated by zoom range.

Parameters:
Returns:

  • QgsProperty representing interpolation settings

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

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

Parses a color value which is interpolated by zoom range.

Parameters:
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)

Interpolates a list which starts with the interpolate function.

Warning

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

Parameters:
parseInterpolateOpacityByZoom(json: Dict[str, Any], maxOpacity: int, contextPtr: QgsMapBoxGlStyleConversionContext | None = 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)

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

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:
parseJoinStyle(style: str | None) 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 (Optional[str])

Return type:

Qt.PenJoinStyle

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

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)

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)

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

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

Parses and converts a match function value list.

Warning

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

Parameters:
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

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

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)

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:
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])

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

Returns:

  • converted expression

  • defaultString: reasonable default value taken from stops

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

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) -> (QgsVectorTileBasicRendererStyle) – conversion context

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

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

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

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

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]

retrieveSprite(name: str | None, 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 | None, spriteSizeProperty: str | None) 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

sources(self) List[QgsMapBoxGlStyleAbstractSource]

Returns the list of converted sources.

Added in version 3.28.

Return type:

List[QgsMapBoxGlStyleAbstractSource]

staticMetaObject = <PyQt5.QtCore.QMetaObject object>
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]