QGIS API Documentation  3.27.0-Master (0e23467727)
qgsmapboxglstyleconverter.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmapboxglstyleconverter.h
3  --------------------------------------
4  Date : September 2020
5  Copyright : (C) 2020 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 
16 #ifndef QGSMAPBOXGLSTYLECONVERTER_H
17 #define QGSMAPBOXGLSTYLECONVERTER_H
18 
19 #include "qgis_core.h"
20 #include "qgis_sip.h"
21 #include "qgsproperty.h"
22 #include <QVariantMap>
23 #include <memory>
24 #include <QImage>
25 
30 
38 {
39  public:
40 
44  void pushWarning( const QString &warning );
45 
49  QStringList warnings() const { return mWarnings; }
50 
54  void clearWarnings() { mWarnings.clear(); }
55 
66  QgsUnitTypes::RenderUnit targetUnit() const;
67 
83  void setTargetUnit( QgsUnitTypes::RenderUnit targetUnit );
84 
91  double pixelSizeConversionFactor() const;
92 
99  void setPixelSizeConversionFactor( double sizeConversionFactor );
100 
107  QImage spriteImage() const;
108 
115  QVariantMap spriteDefinitions() const;
116 
123  void setSprites( const QImage &image, const QVariantMap &definitions );
124 
131  void setSprites( const QImage &image, const QString &definitions );
132 
138  QString layerId() const;
139 
145  void setLayerId( const QString &value );
146 
147  private:
148 
149  QStringList mWarnings;
150 
151  QString mLayerId;
152 
154 
155  double mSizeConversionFactor = 1.0;
156 
157  QImage mSpriteImage;
158  QVariantMap mSpriteDefinitions;
159 };
160 
172 class CORE_EXPORT QgsMapBoxGlStyleConverter
173 {
174  public:
175 
180 
185 
187 
189  enum Result
190  {
191  Success = 0,
192  NoLayerList = 1,
193  };
194 
206  Result convert( const QVariantMap &style, QgsMapBoxGlStyleConversionContext *context = nullptr );
207 
219  Result convert( const QString &style, QgsMapBoxGlStyleConversionContext *context = nullptr );
220 
227  QString errorMessage() const { return mError; }
228 
235  QStringList warnings() const { return mWarnings; }
236 
241  QgsVectorTileRenderer *renderer() const SIP_FACTORY;
242 
247  QgsVectorTileLabeling *labeling() const SIP_FACTORY;
248 
249  protected:
250 
256  {
261  };
262 
267  void parseLayers( const QVariantList &layers, QgsMapBoxGlStyleConversionContext *context = nullptr );
268 
280  static bool parseFillLayer( const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style SIP_OUT, QgsMapBoxGlStyleConversionContext &context, bool isBackgroundStyle = false );
281 
292  static bool parseLineLayer( const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style SIP_OUT, QgsMapBoxGlStyleConversionContext &context );
293 
304  static bool parseCircleLayer( const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style SIP_OUT, QgsMapBoxGlStyleConversionContext &context );
305 
318  static void parseSymbolLayer( const QVariantMap &jsonLayer,
320  bool &hasRenderer SIP_OUT,
322  bool &hasLabeling SIP_OUT, QgsMapBoxGlStyleConversionContext &context );
323 
335  static bool parseSymbolLayerAsRenderer( const QVariantMap &jsonLayer,
337 
347  static QgsProperty parseInterpolateColorByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, QColor *defaultColor SIP_OUT = nullptr );
348 
359  static QgsProperty parseInterpolateByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1, double *defaultNumber SIP_OUT = nullptr );
360 
368  static QgsProperty parseInterpolateOpacityByZoom( const QVariantMap &json, int maxOpacity, QgsMapBoxGlStyleConversionContext *contextPtr = 0 );
369 
376  static QString parseOpacityStops( double base, const QVariantList &stops, int maxOpacity, QgsMapBoxGlStyleConversionContext &context );
377 
384  static QgsProperty parseInterpolatePointByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1, QPointF *defaultPoint SIP_OUT = nullptr );
385 
392  static QgsProperty parseInterpolateStringByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context,
393  const QVariantMap &conversionMap,
394  QString *defaultString SIP_OUT = nullptr );
395 
402  static QString parsePointStops( double base, const QVariantList &stops, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1 );
403 
409  static QString parseArrayStops( const QVariantList &stops, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1 );
410 
419  static QString parseStops( double base, const QVariantList &stops, double multiplier, QgsMapBoxGlStyleConversionContext &context );
420 
431  static QString parseStringStops( const QVariantList &stops, QgsMapBoxGlStyleConversionContext &context,
432  const QVariantMap &conversionMap,
433  QString *defaultString SIP_OUT = nullptr );
434 
435 
444  static QString parseLabelStops( const QVariantList &stops, QgsMapBoxGlStyleConversionContext &context );
445 
451  static QgsProperty parseValueList( const QVariantList &json, PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1,
452  int maxOpacity = 255, QColor *defaultColor SIP_OUT = nullptr, double *defaultNumber SIP_OUT = nullptr );
453 
454 
460  static QgsProperty parseMatchList( const QVariantList &json, PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1,
461  int maxOpacity = 255, QColor *defaultColor SIP_OUT = nullptr, double *defaultNumber SIP_OUT = nullptr );
462 
468  static QgsProperty parseInterpolateListByZoom( const QVariantList &json, PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier = 1,
469  int maxOpacity = 255, QColor *defaultColor SIP_OUT = nullptr, double *defaultNumber SIP_OUT = nullptr );
470 
478  static QString parseColorExpression( const QVariant &colorExpression, QgsMapBoxGlStyleConversionContext &context );
479 
491  static QColor parseColor( const QVariant &color, QgsMapBoxGlStyleConversionContext &context );
492 
503  static void colorAsHslaComponents( const QColor &color, int &hue, int &saturation, int &lightness, int &alpha );
504 
511  static QString interpolateExpression( double zoomMin, double zoomMax, QVariant valueMin, QVariant valueMax, double base, double multiplier = 1, QgsMapBoxGlStyleConversionContext *contextPtr = 0 );
512 
518  static Qt::PenCapStyle parseCapStyle( const QString &style );
519 
525  static Qt::PenJoinStyle parseJoinStyle( const QString &style );
526 
532  static QString parseExpression( const QVariantList &expression, QgsMapBoxGlStyleConversionContext &context, bool colorExpected = false );
533 
540  static QImage retrieveSprite( const QString &name, QgsMapBoxGlStyleConversionContext &context, QSize &spriteSize );
541 
548  static QString retrieveSpriteAsBase64( const QVariant &value, QgsMapBoxGlStyleConversionContext &context, QSize &spriteSize, QString &spriteProperty, QString &spriteSizeProperty );
549 
550  private:
551 
552 #ifdef SIP_RUN
554 #endif
555 
556  static QString parseValue( const QVariant &value, QgsMapBoxGlStyleConversionContext &context, bool colorExpected = false );
557 
558  static QString parseKey( const QVariant &value, QgsMapBoxGlStyleConversionContext &context );
559 
560  static QString processLabelField( const QString &string, bool &isExpression );
561 
570  static bool numericArgumentsOnly( const QVariant &bottomVariant, const QVariant &topVariant, double &bottom, double &top );
571 
572  QString mError;
573  QStringList mWarnings;
574 
575  std::unique_ptr< QgsVectorTileRenderer > mRenderer;
576  std::unique_ptr< QgsVectorTileLabeling > mLabeling;
577 
578 };
579 
580 #endif // QGSMAPBOXGLSTYLECONVERTER_H
Context for a MapBox GL style conversion operation.
QStringList warnings() const
Returns a list of warning messages generated during the conversion.
void clearWarnings()
Clears the list of warning messages.
Handles conversion of MapBox GL styles to QGIS vector tile renderers and labeling settings.
PropertyType
Property types, for interpolated value conversion.
@ Numeric
Numeric property (e.g. line width, text size)
QgsMapBoxGlStyleConverter & operator=(const QgsMapBoxGlStyleConverter &other)=delete
QgsMapBoxGlStyleConverter cannot be copied.
QString errorMessage() const
Returns a descriptive error message if an error was encountered during the style conversion,...
QStringList warnings() const
Returns a list of user-friendly warnings generated during the conversion, e.g.
QgsMapBoxGlStyleConverter(const QgsMapBoxGlStyleConverter &other)=delete
QgsMapBoxGlStyleConverter cannot be copied.
A store for object properties.
Definition: qgsproperty.h:231
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:168
@ RenderPixels
Pixels.
Definition: qgsunittypes.h:171
Configuration of a single style within QgsVectorTileBasicLabeling.
Definition of map rendering of a subset of vector tile data.
Base class for labeling configuration classes for vector tile layers.
Abstract base class for all vector tile renderer implementations.
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsExpressionNode * parseExpression(const QString &str, QString &parserErrorMsg, QList< QgsExpression::ParserError > &parserErrors)