QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
Loading...
Searching...
No Matches
qgssymbolconvertermapboxgl.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssymbolconvertermapboxgl.cpp
3 ----------------------
4 begin : February 2026
5 copyright : (C) 2026 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7***************************************************************************
8* *
9* This program is free software; you can redistribute it and/or modify *
10* it under the terms of the GNU General Public License as published by *
11* the Free Software Foundation; either version 2 of the License, or *
12* (at your option) any later version. *
13* *
14***************************************************************************/
15
17
18#include "qgsjsonutils.h"
20#include "qgssymbol.h"
22
23#include <QObject>
24#include <QString>
25#include <QVariant>
26
27using namespace Qt::StringLiterals;
28
33
35{
36 return u"mapboxgl"_s;
37}
38
40{
41 return QObject::tr( "MapBox GL Style" );
42}
43
45{
46 throw QgsNotSupportedException( u"This symbol converter does not support serialization of symbols"_s );
47}
48
49std::unique_ptr< QgsSymbol > QgsSymbolConverterMapBoxGl::createSymbol( const QVariant &variant, QgsSymbolConverterContext &context ) const
50{
51 if ( variant.isNull() )
52 return nullptr;
53
54 QVariantMap jsonLayer;
55 if ( variant.userType() == QMetaType::Type::QVariantMap )
56 {
57 jsonLayer = variant.toMap();
58 }
59 else if ( variant.canConvert<QString>() )
60 {
61 jsonLayer = QgsJsonUtils::parseJson( variant.toString() ).toMap();
62 }
63
64 if ( jsonLayer.isEmpty() || !jsonLayer.contains( u"type"_s ) )
65 {
66 context.pushError( QObject::tr( "Invalid MapBox GL JSON: Missing 'type' property." ) );
67 return nullptr;
68 }
69
70 const QString layerType = jsonLayer.value( u"type"_s ).toString();
71
74 bool success = false;
75
76 if ( layerType == "fill"_L1 )
77 {
78 success = QgsMapBoxGlStyleConverter::parseFillLayer( jsonLayer, style, mbContext );
79 }
80 else if ( layerType == "line"_L1 )
81 {
82 success = QgsMapBoxGlStyleConverter::parseLineLayer( jsonLayer, style, mbContext );
83 }
84 else if ( layerType == "circle"_L1 )
85 {
86 success = QgsMapBoxGlStyleConverter::parseCircleLayer( jsonLayer, style, mbContext );
87 }
88 else if ( layerType == "symbol"_L1 )
89 {
90 success = QgsMapBoxGlStyleConverter::parseSymbolLayerAsRenderer( jsonLayer, style, mbContext );
91 }
92 else if ( layerType == "background"_L1 )
93 {
94 success = QgsMapBoxGlStyleConverter::parseFillLayer( jsonLayer, style, mbContext, true );
95 }
96 else
97 {
98 context.pushError( QObject::tr( "Unsupported MapBox GL layer type: %1" ).arg( layerType ) );
99 return nullptr;
100 }
101
102 const QStringList warnings = mbContext.warnings();
103 for ( const QString &warning : warnings )
104 {
105 context.pushWarning( warning );
106 }
107
108 if ( success && style.symbol() )
109 {
110 return std::unique_ptr< QgsSymbol >( style.symbol()->clone() );
111 }
112
113 return nullptr;
114}
QFlags< SymbolConverterCapability > SymbolConverterCapabilities
Symbol converter capabilities.
Definition qgis.h:824
@ ReadSymbol
Allows reading symbols from variants.
Definition qgis.h:813
static QVariant parseJson(const std::string &jsonString)
Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned and ...
Context for a MapBox GL style conversion operation.
QStringList warnings() const
Returns a list of warning messages generated during the conversion.
static bool parseCircleLayer(const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style, QgsMapBoxGlStyleConversionContext &context)
Parses a circle layer.
static bool parseSymbolLayerAsRenderer(const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &rendererStyle, QgsMapBoxGlStyleConversionContext &context)
Parses a symbol layer as a renderer.
static bool parseFillLayer(const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style, QgsMapBoxGlStyleConversionContext &context, bool isBackgroundStyle=false)
Parses a fill layer.
static bool parseLineLayer(const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style, QgsMapBoxGlStyleConversionContext &context)
Parses a line layer.
Custom exception class which is raised when an operation is not supported.
Represents the context in which a QgsSymbolConverter conversion occurs.
void pushWarning(const QString &warning)
Pushes a warning message generated during the conversion.
void pushError(const QString &error)
Pushes a error message generated during the conversion.
QString formatName() const override
Returns a translated, user-friendly name for the converter's data format.
Qgis::SymbolConverterCapabilities capabilities() const override
Returns the capabilities of the converter.
QVariant toVariant(const QgsSymbol *symbol, QgsSymbolConverterContext &context) const override
Converts a symbol into a QVariant representation.
QString name() const override
Returns the unique name for the converter.
std::unique_ptr< QgsSymbol > createSymbol(const QVariant &variant, QgsSymbolConverterContext &context) const override
Creates a new QgsSymbol from a QVariant representation.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:227
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
Definition of map rendering of a subset of vector tile data.
QgsSymbol * symbol() const
Returns symbol for rendering.