QGIS API Documentation 4.5.0-Master (3b5d115045b)
Loading...
Searching...
No Matches
qgsmapboxglstyleconverter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmapboxglstyleconverter.cpp
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
17/*
18 * Ported from original work by Martin Dobias, and extended by the MapTiler team!
19 */
20
22
23#include "qgis.h"
24#include "qgsapplication.h"
25#include "qgsblureffect.h"
26#include "qgseffectstack.h"
27#include "qgsfillsymbol.h"
28#include "qgsfillsymbollayer.h"
29#include "qgsfontmanager.h"
30#include "qgsfontutils.h"
31#include "qgsjsonutils.h"
32#include "qgslinesymbol.h"
33#include "qgslinesymbollayer.h"
34#include "qgslogger.h"
35#include "qgsmarkersymbol.h"
37#include "qgspainteffect.h"
38#include "qgsproviderregistry.h"
39#include "qgsrasterlayer.h"
40#include "qgsrasterpipe.h"
41#include "qgssymbollayer.h"
42#include "qgssymbollayerutils.h"
46
47#include <QBuffer>
48#include <QRegularExpression>
49#include <QString>
50
51#include "moc_qgsmapboxglstyleconverter.cpp"
52
53using namespace Qt::StringLiterals;
54
57
59{
60 mError.clear();
61 mWarnings.clear();
62
63 if ( style.contains( u"sources"_s ) )
64 {
65 parseSources( style.value( u"sources"_s ).toMap(), context );
66 }
67
68 if ( style.contains( u"layers"_s ) )
69 {
70 parseLayers( style.value( u"layers"_s ).toList(), context );
71 }
72 else
73 {
74 mError = QObject::tr( "Could not find layers list in JSON" );
75 return NoLayerList;
76 }
77 return Success;
78}
79
84
86{
87 qDeleteAll( mSources );
88}
89
91{
92 std::unique_ptr< QgsMapBoxGlStyleConversionContext > tmpContext;
93 if ( !context )
94 {
95 tmpContext = std::make_unique< QgsMapBoxGlStyleConversionContext >();
96 context = tmpContext.get();
97 }
98
99 QList<QgsVectorTileBasicRendererStyle> rendererStyles;
100 QList<QgsVectorTileBasicLabelingStyle> labelingStyles;
101
102 QgsVectorTileBasicRendererStyle rendererBackgroundStyle;
103 bool hasRendererBackgroundStyle = false;
104
105 for ( const QVariant &layer : layers )
106 {
107 const QVariantMap jsonLayer = layer.toMap();
108
109 const QString layerType = jsonLayer.value( u"type"_s ).toString();
110 if ( layerType == "background"_L1 )
111 {
112 hasRendererBackgroundStyle = parseFillLayer( jsonLayer, rendererBackgroundStyle, *context, true );
113 if ( hasRendererBackgroundStyle )
114 {
115 rendererBackgroundStyle.setStyleName( layerType );
116 rendererBackgroundStyle.setLayerName( layerType );
117 rendererBackgroundStyle.setFilterExpression( QString() );
118 rendererBackgroundStyle.setEnabled( true );
119 }
120 continue;
121 }
122
123 const QString styleId = jsonLayer.value( u"id"_s ).toString();
124 context->setLayerId( styleId );
125
126 if ( layerType.compare( "raster"_L1, Qt::CaseInsensitive ) == 0 )
127 {
128 QgsMapBoxGlStyleRasterSubLayer raster( styleId, jsonLayer.value( u"source"_s ).toString() );
129 const QVariantMap jsonPaint = jsonLayer.value( u"paint"_s ).toMap();
130 if ( jsonPaint.contains( u"raster-opacity"_s ) )
131 {
132 const QVariant jsonRasterOpacity = jsonPaint.value( u"raster-opacity"_s );
133 double defaultOpacity = 1;
134 raster.dataDefinedProperties().setProperty( QgsRasterPipe::Property::RendererOpacity, parseInterpolateByZoom( jsonRasterOpacity.toMap(), *context, 100, &defaultOpacity ) );
135 }
136
137 mRasterSubLayers.append( raster );
138 continue;
139 }
140
141 const QString layerName = jsonLayer.value( u"source-layer"_s ).toString();
142
143 const int minZoom = jsonLayer.value( u"minzoom"_s, u"-1"_s ).toInt();
144
145 // WARNING -- the QGIS renderers for vector tiles treat maxzoom different to the MapBox Style Specifications.
146 // from the MapBox Specifications:
147 //
148 // "The maximum zoom level for the layer. At zoom levels equal to or greater than the maxzoom, the layer will be hidden."
149 //
150 // However the QGIS styles will be hidden if the zoom level is GREATER THAN (not equal to) maxzoom.
151 // Accordingly we need to subtract 1 from the maxzoom value in the JSON:
152 int maxZoom = jsonLayer.value( u"maxzoom"_s, u"-1"_s ).toInt();
153 if ( maxZoom != -1 )
154 maxZoom--;
155
156 QString visibilyStr;
157 if ( jsonLayer.contains( u"visibility"_s ) )
158 {
159 visibilyStr = jsonLayer.value( u"visibility"_s ).toString();
160 }
161 else if ( jsonLayer.contains( u"layout"_s ) && jsonLayer.value( u"layout"_s ).userType() == QMetaType::Type::QVariantMap )
162 {
163 const QVariantMap jsonLayout = jsonLayer.value( u"layout"_s ).toMap();
164 visibilyStr = jsonLayout.value( u"visibility"_s ).toString();
165 }
166
167 const bool enabled = visibilyStr != "none"_L1;
168
169 QString filterExpression;
170 if ( jsonLayer.contains( u"filter"_s ) )
171 {
172 filterExpression = parseExpression( jsonLayer.value( u"filter"_s ).toList(), *context );
173 }
174
177
178 bool hasRendererStyle = false;
179 bool hasLabelingStyle = false;
180 if ( layerType == "fill"_L1 )
181 {
182 hasRendererStyle = parseFillLayer( jsonLayer, rendererStyle, *context );
183 }
184 else if ( layerType == "line"_L1 )
185 {
186 hasRendererStyle = parseLineLayer( jsonLayer, rendererStyle, *context );
187 }
188 else if ( layerType == "circle"_L1 )
189 {
190 hasRendererStyle = parseCircleLayer( jsonLayer, rendererStyle, *context );
191 }
192 else if ( layerType == "symbol"_L1 )
193 {
194 parseSymbolLayer( jsonLayer, rendererStyle, hasRendererStyle, labelingStyle, hasLabelingStyle, *context );
195 }
196 else
197 {
198 mWarnings << QObject::tr( "%1: Skipping unknown layer type %2" ).arg( context->layerId(), layerType );
199 QgsDebugError( mWarnings.constLast() );
200 continue;
201 }
202
203 if ( hasRendererStyle )
204 {
205 rendererStyle.setStyleName( styleId );
206 rendererStyle.setLayerName( layerName );
207 rendererStyle.setFilterExpression( filterExpression );
208 rendererStyle.setMinZoomLevel( minZoom );
209 rendererStyle.setMaxZoomLevel( maxZoom );
210 rendererStyle.setEnabled( enabled );
211 rendererStyles.append( rendererStyle );
212 }
213
214 if ( hasLabelingStyle )
215 {
216 labelingStyle.setStyleName( styleId );
217 labelingStyle.setLayerName( layerName );
218 labelingStyle.setFilterExpression( filterExpression );
219 labelingStyle.setMinZoomLevel( minZoom );
220 labelingStyle.setMaxZoomLevel( maxZoom );
221 labelingStyle.setEnabled( enabled );
222 labelingStyles.append( labelingStyle );
223 }
224
225 mWarnings.append( context->warnings() );
226 context->clearWarnings();
227 }
228
229 if ( hasRendererBackgroundStyle )
230 rendererStyles.prepend( rendererBackgroundStyle );
231
232 auto renderer = std::make_unique< QgsVectorTileBasicRenderer >();
233 renderer->setStyles( rendererStyles );
234 mRenderer = std::move( renderer );
235
236 auto labeling = std::make_unique< QgsVectorTileBasicLabeling >();
237 labeling->setStyles( labelingStyles );
238 mLabeling = std::move( labeling );
239}
240
241bool QgsMapBoxGlStyleConverter::parseFillLayer( const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style, QgsMapBoxGlStyleConversionContext &context, bool isBackgroundStyle )
242{
243 const QVariantMap jsonPaint = jsonLayer.value( u"paint"_s ).toMap();
244
245 QgsPropertyCollection ddProperties;
246 QgsPropertyCollection ddRasterProperties;
247
248 bool colorIsDataDefined = false;
249
250 std::unique_ptr< QgsSymbol > symbol( std::make_unique< QgsFillSymbol >() );
251
252 // fill color
253 QColor fillColor;
254 if ( jsonPaint.contains( isBackgroundStyle ? u"background-color"_s : u"fill-color"_s ) )
255 {
256 const QVariant jsonFillColor = jsonPaint.value( isBackgroundStyle ? u"background-color"_s : u"fill-color"_s );
257 switch ( jsonFillColor.userType() )
258 {
259 case QMetaType::Type::QVariantMap:
260 ddProperties.setProperty( QgsSymbolLayer::Property::FillColor, parseInterpolateColorByZoom( jsonFillColor.toMap(), context, &fillColor ) );
261 break;
262
263 case QMetaType::Type::QVariantList:
264 case QMetaType::Type::QStringList:
265 colorIsDataDefined = true;
266 ddProperties.setProperty( QgsSymbolLayer::Property::FillColor, parseValueList( jsonFillColor.toList(), PropertyType::Color, context, 1, 255, &fillColor ) );
267 break;
268
269 case QMetaType::Type::QString:
270 fillColor = parseColor( jsonFillColor.toString(), context );
271 break;
272
273 default:
274 {
275 context.pushWarning( QObject::tr( "%1: Skipping unsupported fill-color type (%2)" ).arg( context.layerId(), jsonFillColor.metaType().name() ) );
276 break;
277 }
278 }
279 }
280 else
281 {
282 // defaults to #000000
283 fillColor = QColor( 0, 0, 0 );
284 }
285
286 QColor fillOutlineColor;
287 if ( !isBackgroundStyle )
288 {
289 if ( !jsonPaint.contains( u"fill-outline-color"_s ) )
290 {
291 if ( fillColor.isValid() )
292 fillOutlineColor = fillColor;
293
294 // match fill color data defined property when active
295 if ( ddProperties.isActive( QgsSymbolLayer::Property::FillColor ) )
297 }
298 else
299 {
300 const QVariant jsonFillOutlineColor = jsonPaint.value( u"fill-outline-color"_s );
301 switch ( jsonFillOutlineColor.userType() )
302 {
303 case QMetaType::Type::QVariantMap:
304 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeColor, parseInterpolateColorByZoom( jsonFillOutlineColor.toMap(), context, &fillOutlineColor ) );
305 break;
306
307 case QMetaType::Type::QVariantList:
308 case QMetaType::Type::QStringList:
309 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeColor, parseValueList( jsonFillOutlineColor.toList(), PropertyType::Color, context, 1, 255, &fillOutlineColor ) );
310 break;
311
312 case QMetaType::Type::QString:
313 fillOutlineColor = parseColor( jsonFillOutlineColor.toString(), context );
314 break;
315
316 default:
317 context.pushWarning( QObject::tr( "%1: Skipping unsupported fill-outline-color type (%2)" ).arg( context.layerId(), jsonFillOutlineColor.metaType().name() ) );
318 break;
319 }
320 }
321 }
322
323 double fillOpacity = -1.0;
324 double rasterOpacity = -1.0;
325 if ( jsonPaint.contains( isBackgroundStyle ? u"background-opacity"_s : u"fill-opacity"_s ) )
326 {
327 const QVariant jsonFillOpacity = jsonPaint.value( isBackgroundStyle ? u"background-opacity"_s : u"fill-opacity"_s );
328 switch ( jsonFillOpacity.userType() )
329 {
330 case QMetaType::Type::Int:
331 case QMetaType::Type::LongLong:
332 case QMetaType::Type::Double:
333 fillOpacity = jsonFillOpacity.toDouble();
334 rasterOpacity = fillOpacity;
335 break;
336
337 case QMetaType::Type::QVariantMap:
338 if ( ddProperties.isActive( QgsSymbolLayer::Property::FillColor ) )
339 {
340 symbol->setDataDefinedProperty( QgsSymbol::Property::Opacity, parseInterpolateByZoom( jsonFillOpacity.toMap(), context, 100 ) );
341 }
342 else
343 {
344 ddProperties.setProperty( QgsSymbolLayer::Property::FillColor, parseInterpolateOpacityByZoom( jsonFillOpacity.toMap(), fillColor.isValid() ? fillColor.alpha() : 255, &context ) );
345 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeColor, parseInterpolateOpacityByZoom( jsonFillOpacity.toMap(), fillOutlineColor.isValid() ? fillOutlineColor.alpha() : 255, &context ) );
346 ddRasterProperties.setProperty( QgsSymbolLayer::Property::Opacity, parseInterpolateByZoom( jsonFillOpacity.toMap(), context, 100, &rasterOpacity ) );
347 }
348 break;
349
350 case QMetaType::Type::QVariantList:
351 case QMetaType::Type::QStringList:
352 if ( ddProperties.isActive( QgsSymbolLayer::Property::FillColor ) )
353 {
354 symbol->setDataDefinedProperty( QgsSymbol::Property::Opacity, parseValueList( jsonFillOpacity.toList(), PropertyType::Numeric, context, 100, 100 ) );
355 }
356 else
357 {
358 ddProperties.setProperty( QgsSymbolLayer::Property::FillColor, parseValueList( jsonFillOpacity.toList(), PropertyType::Opacity, context, 1, fillColor.isValid() ? fillColor.alpha() : 255 ) );
359 ddProperties
360 .setProperty( QgsSymbolLayer::Property::StrokeColor, parseValueList( jsonFillOpacity.toList(), PropertyType::Opacity, context, 1, fillOutlineColor.isValid() ? fillOutlineColor.alpha() : 255 ) );
361 ddRasterProperties.setProperty( QgsSymbolLayer::Property::Opacity, parseValueList( jsonFillOpacity.toList(), PropertyType::Numeric, context, 100, 255, nullptr, &rasterOpacity ) );
362 }
363 break;
364
365 default:
366 context.pushWarning( QObject::tr( "%1: Skipping unsupported fill-opacity type (%2)" ).arg( context.layerId(), jsonFillOpacity.metaType().name() ) );
367 break;
368 }
369 }
370
371 // fill-translate
372 QPointF fillTranslate;
373 if ( jsonPaint.contains( u"fill-translate"_s ) )
374 {
375 const QVariant jsonFillTranslate = jsonPaint.value( u"fill-translate"_s );
376 switch ( jsonFillTranslate.userType() )
377 {
378 case QMetaType::Type::QVariantMap:
379 ddProperties.setProperty( QgsSymbolLayer::Property::Offset, parseInterpolatePointByZoom( jsonFillTranslate.toMap(), context, context.pixelSizeConversionFactor(), &fillTranslate ) );
380 break;
381
382 case QMetaType::Type::QVariantList:
383 case QMetaType::Type::QStringList:
384 {
385 bool isPointValues = false;
386 const double pointX = jsonFillTranslate.toList().value( 0 ).toDouble( &isPointValues );
387 if ( isPointValues )
388 {
389 fillTranslate = QPointF( pointX * context.pixelSizeConversionFactor(), jsonFillTranslate.toList().value( 1 ).toDouble() * context.pixelSizeConversionFactor() );
390 }
391 else
392 {
393 ddProperties.setProperty( QgsSymbolLayer::Property::Offset, parseInterpolateListByZoom( jsonFillTranslate.toList(), PropertyType::Point, context, context.pixelSizeConversionFactor() ) );
394 }
395 break;
396 }
397
398 default:
399 context.pushWarning( QObject::tr( "%1: Skipping unsupported fill-translate type (%2)" ).arg( context.layerId(), jsonFillTranslate.metaType().name() ) );
400 break;
401 }
402 }
403
404 QgsSimpleFillSymbolLayer *fillSymbol = dynamic_cast< QgsSimpleFillSymbolLayer * >( symbol->symbolLayer( 0 ) );
405 Q_ASSERT( fillSymbol ); // should not fail since QgsFillSymbol() constructor instantiates a QgsSimpleFillSymbolLayer
406
407 // set render units
408 symbol->setOutputUnit( context.targetUnit() );
409 fillSymbol->setOutputUnit( context.targetUnit() );
410
411 if ( !fillTranslate.isNull() )
412 {
413 fillSymbol->setOffset( fillTranslate );
414 }
415 fillSymbol->setOffsetUnit( context.targetUnit() );
416
417 if ( jsonPaint.contains( isBackgroundStyle ? u"background-pattern"_s : u"fill-pattern"_s ) )
418 {
419 // get fill-pattern to set sprite
420
421 const QVariant fillPatternJson = jsonPaint.value( isBackgroundStyle ? u"background-pattern"_s : u"fill-pattern"_s );
422
423 // fill-pattern disabled dillcolor
424 fillColor = QColor();
425 fillOutlineColor = QColor();
426
427 // fill-pattern can be String or Object
428 // String: {"fill-pattern": "dash-t"}
429 // Object: {"fill-pattern":{"stops":[[11,"wetland8"],[12,"wetland16"]]}}
430
431 QSize spriteSize;
432 QString spriteProperty, spriteSizeProperty;
433 const QString sprite = retrieveSpriteAsBase64WithProperties( fillPatternJson, context, spriteSize, spriteProperty, spriteSizeProperty );
434 if ( !sprite.isEmpty() )
435 {
436 // when fill-pattern exists, set and insert QgsRasterFillSymbolLayer
438 rasterFill->setImageFilePath( sprite );
439 rasterFill->setWidth( spriteSize.width() );
440 rasterFill->setSizeUnit( context.targetUnit() );
442
443 if ( rasterOpacity >= 0 )
444 {
445 rasterFill->setOpacity( rasterOpacity );
446 }
447
448 if ( !spriteProperty.isEmpty() )
449 {
450 ddRasterProperties.setProperty( QgsSymbolLayer::Property::File, QgsProperty::fromExpression( spriteProperty ) );
451 ddRasterProperties.setProperty( QgsSymbolLayer::Property::Width, QgsProperty::fromExpression( spriteSizeProperty ) );
452 }
453
454 rasterFill->setDataDefinedProperties( ddRasterProperties );
455 symbol->appendSymbolLayer( rasterFill );
456 }
457 }
458
459 fillSymbol->setDataDefinedProperties( ddProperties );
460
461 if ( fillOpacity != -1 )
462 {
463 symbol->setOpacity( fillOpacity );
464 }
465
466 // some complex logic here!
467 // by default a MapBox fill style will share the same stroke color as the fill color.
468 // This is generally desirable and the 1px stroke can help to hide boundaries between features which
469 // would otherwise be visible due to antialiasing effects.
470 // BUT if the outline color is semi-transparent, then drawing the stroke will result in a double rendering
471 // of strokes for adjacent polygons, resulting in visible seams between tiles. Accordingly, we only
472 // set the stroke color if it's a completely different color to the fill (ie the style designer explicitly
473 // wants a visible stroke) OR the stroke color is opaque and the double-rendering artifacts aren't an issue
474 if ( fillOutlineColor.isValid() && ( fillOutlineColor.alpha() == 255 || fillOutlineColor != fillColor ) )
475 {
476 // mapbox fill strokes are always 1 px wide
477 fillSymbol->setStrokeWidth( 0 );
478 fillSymbol->setStrokeColor( fillOutlineColor );
479 }
480 else
481 {
482 fillSymbol->setStrokeStyle( Qt::NoPen );
483 }
484
485 if ( fillColor.isValid() )
486 {
487 fillSymbol->setFillColor( fillColor );
488 }
489 else if ( colorIsDataDefined )
490 {
491 fillSymbol->setFillColor( QColor( Qt::transparent ) );
492 }
493 else
494 {
495 fillSymbol->setBrushStyle( Qt::NoBrush );
496 }
497
499 style.setSymbol( symbol.release() );
500 return true;
501}
502
504{
505 if ( !jsonLayer.contains( u"paint"_s ) )
506 {
507 context.pushWarning( QObject::tr( "%1: Style has no paint property, skipping" ).arg( context.layerId() ) );
508 return false;
509 }
510
511 QgsPropertyCollection ddProperties;
512 QString rasterLineSprite;
513
514 const QVariantMap jsonPaint = jsonLayer.value( u"paint"_s ).toMap();
515 if ( jsonPaint.contains( u"line-pattern"_s ) )
516 {
517 const QVariant jsonLinePattern = jsonPaint.value( u"line-pattern"_s );
518 switch ( jsonLinePattern.userType() )
519 {
520 case QMetaType::Type::QVariantMap:
521 case QMetaType::Type::QString:
522 {
523 QSize spriteSize;
524 QString spriteProperty, spriteSizeProperty;
525 rasterLineSprite = retrieveSpriteAsBase64WithProperties( jsonLinePattern, context, spriteSize, spriteProperty, spriteSizeProperty );
527 break;
528 }
529
530 case QMetaType::Type::QVariantList:
531 case QMetaType::Type::QStringList:
532 default:
533 break;
534 }
535
536 if ( rasterLineSprite.isEmpty() )
537 {
538 // unsupported line-pattern definition, moving on
539 context.pushWarning( QObject::tr( "%1: Skipping unsupported line-pattern property" ).arg( context.layerId() ) );
540 return false;
541 }
542 }
543
544 // line color
545 QColor lineColor;
546 if ( jsonPaint.contains( u"line-color"_s ) )
547 {
548 const QVariant jsonLineColor = jsonPaint.value( u"line-color"_s );
549 switch ( jsonLineColor.userType() )
550 {
551 case QMetaType::Type::QVariantMap:
552 ddProperties.setProperty( QgsSymbolLayer::Property::FillColor, parseInterpolateColorByZoom( jsonLineColor.toMap(), context, &lineColor ) );
554 break;
555
556 case QMetaType::Type::QVariantList:
557 case QMetaType::Type::QStringList:
558 ddProperties.setProperty( QgsSymbolLayer::Property::FillColor, parseValueList( jsonLineColor.toList(), PropertyType::Color, context, 1, 255, &lineColor ) );
560 break;
561
562 case QMetaType::Type::QString:
563 lineColor = parseColor( jsonLineColor.toString(), context );
564 break;
565
566 default:
567 context.pushWarning( QObject::tr( "%1: Skipping unsupported line-color type (%2)" ).arg( context.layerId(), jsonLineColor.metaType().name() ) );
568 break;
569 }
570 }
571 else
572 {
573 // defaults to #000000
574 lineColor = QColor( 0, 0, 0 );
575 }
576
577
578 double lineWidth = 1.0 * context.pixelSizeConversionFactor();
579 QgsProperty lineWidthProperty;
580 if ( jsonPaint.contains( u"line-width"_s ) )
581 {
582 const QVariant jsonLineWidth = jsonPaint.value( u"line-width"_s );
583 switch ( jsonLineWidth.userType() )
584 {
585 case QMetaType::Type::Int:
586 case QMetaType::Type::LongLong:
587 case QMetaType::Type::Double:
588 lineWidth = jsonLineWidth.toDouble() * context.pixelSizeConversionFactor();
589 break;
590
591 case QMetaType::Type::QVariantMap:
592 {
593 lineWidth = -1;
594 lineWidthProperty = parseInterpolateByZoom( jsonLineWidth.toMap(), context, context.pixelSizeConversionFactor(), &lineWidth );
595 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeWidth, lineWidthProperty );
596 // set symbol layer visibility depending on line width since QGIS displays line with 0 width as hairlines
597 QgsProperty layerEnabledProperty = QgsProperty( lineWidthProperty );
598 layerEnabledProperty.setExpressionString( u"(%1) > 0"_s.arg( lineWidthProperty.expressionString() ) );
599 ddProperties.setProperty( QgsSymbolLayer::Property::LayerEnabled, layerEnabledProperty );
600 break;
601 }
602
603 case QMetaType::Type::QVariantList:
604 case QMetaType::Type::QStringList:
605 {
606 lineWidthProperty = parseValueList( jsonLineWidth.toList(), PropertyType::Numeric, context, context.pixelSizeConversionFactor(), 255, nullptr, &lineWidth );
607 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeWidth, lineWidthProperty );
608 // set symbol layer visibility depending on line width since QGIS displays line with 0 width as hairlines
609 QgsProperty layerEnabledProperty = QgsProperty( lineWidthProperty );
610 layerEnabledProperty.setExpressionString( u"(%1) > 0"_s.arg( lineWidthProperty.expressionString() ) );
611 ddProperties.setProperty( QgsSymbolLayer::Property::LayerEnabled, layerEnabledProperty );
612 break;
613 }
614
615 default:
616 context.pushWarning( QObject::tr( "%1: Skipping unsupported fill-width type (%2)" ).arg( context.layerId(), jsonLineWidth.metaType().name() ) );
617 break;
618 }
619 }
620
621 double lineOffset = 0.0;
622 if ( jsonPaint.contains( u"line-offset"_s ) )
623 {
624 const QVariant jsonLineOffset = jsonPaint.value( u"line-offset"_s );
625 switch ( jsonLineOffset.userType() )
626 {
627 case QMetaType::Type::Int:
628 case QMetaType::Type::LongLong:
629 case QMetaType::Type::Double:
630 lineOffset = -jsonLineOffset.toDouble() * context.pixelSizeConversionFactor();
631 break;
632
633 case QMetaType::Type::QVariantMap:
634 lineWidth = -1;
635 ddProperties.setProperty( QgsSymbolLayer::Property::Offset, parseInterpolateByZoom( jsonLineOffset.toMap(), context, context.pixelSizeConversionFactor() * -1, &lineOffset ) );
636 break;
637
638 case QMetaType::Type::QVariantList:
639 case QMetaType::Type::QStringList:
640 ddProperties.setProperty( QgsSymbolLayer::Property::Offset, parseValueList( jsonLineOffset.toList(), PropertyType::Numeric, context, context.pixelSizeConversionFactor() * -1, 255, nullptr, &lineOffset ) );
641 break;
642
643 default:
644 context.pushWarning( QObject::tr( "%1: Skipping unsupported line-offset type (%2)" ).arg( context.layerId(), jsonLineOffset.metaType().name() ) );
645 break;
646 }
647 }
648
649 double lineOpacity = -1.0;
650 QgsProperty lineOpacityProperty;
651 if ( jsonPaint.contains( u"line-opacity"_s ) )
652 {
653 const QVariant jsonLineOpacity = jsonPaint.value( u"line-opacity"_s );
654 switch ( jsonLineOpacity.userType() )
655 {
656 case QMetaType::Type::Int:
657 case QMetaType::Type::LongLong:
658 case QMetaType::Type::Double:
659 lineOpacity = jsonLineOpacity.toDouble();
660 break;
661
662 case QMetaType::Type::QVariantMap:
664 {
665 double defaultValue = 1.0;
666 lineOpacityProperty = parseInterpolateByZoom( jsonLineOpacity.toMap(), context, 100, &defaultValue );
667 }
668 else
669 {
670 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeColor, parseInterpolateOpacityByZoom( jsonLineOpacity.toMap(), lineColor.isValid() ? lineColor.alpha() : 255, &context ) );
671 }
672 break;
673
674 case QMetaType::Type::QVariantList:
675 case QMetaType::Type::QStringList:
677 {
678 double defaultValue = 1.0;
679 QColor invalidColor;
680 lineOpacityProperty = parseValueList( jsonLineOpacity.toList(), PropertyType::Numeric, context, 100, 255, &invalidColor, &defaultValue );
681 }
682 else
683 {
684 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeColor, parseValueList( jsonLineOpacity.toList(), PropertyType::Opacity, context, 1, lineColor.isValid() ? lineColor.alpha() : 255 ) );
685 }
686 break;
687
688 default:
689 context.pushWarning( QObject::tr( "%1: Skipping unsupported line-opacity type (%2)" ).arg( context.layerId(), jsonLineOpacity.metaType().name() ) );
690 break;
691 }
692 }
693
694 QVector< double > dashVector;
695 if ( jsonPaint.contains( u"line-dasharray"_s ) )
696 {
697 const QVariant jsonLineDashArray = jsonPaint.value( u"line-dasharray"_s );
698 switch ( jsonLineDashArray.userType() )
699 {
700 case QMetaType::Type::QVariantMap:
701 {
702 QString arrayExpression;
703 if ( !lineWidthProperty.asExpression().isEmpty() )
704 {
705 arrayExpression = u"array_to_string(array_foreach(%1,@element * (%2)), ';')"_s // skip-keyword-check
706 .arg( parseArrayStops( jsonLineDashArray.toMap().value( u"stops"_s ).toList(), context, 1 ), lineWidthProperty.asExpression() );
707 }
708 else
709 {
710 arrayExpression = u"array_to_string(%1, ';')"_s.arg( parseArrayStops( jsonLineDashArray.toMap().value( u"stops"_s ).toList(), context, lineWidth ) );
711 }
713
714 const QVariantList dashSource = jsonLineDashArray.toMap().value( u"stops"_s ).toList().first().toList().value( 1 ).toList();
715 for ( const QVariant &v : dashSource )
716 {
717 dashVector << v.toDouble() * lineWidth;
718 }
719 break;
720 }
721
722 case QMetaType::Type::QVariantList:
723 case QMetaType::Type::QStringList:
724 {
725 const QVariantList dashSource = jsonLineDashArray.toList();
726
727 if ( !dashSource.empty() )
728 {
729 if ( dashSource.at( 0 ).userType() == QMetaType::Type::QString )
730 {
731 QgsProperty property = parseValueList( dashSource, PropertyType::DashArray, context, 1, 255, nullptr, nullptr );
732 if ( !lineWidthProperty.asExpression().isEmpty() )
733 {
735 u"array_to_string(array_foreach(%1,@element * (%2)), ';')"_s // skip-keyword-check
736 .arg( property.asExpression(), lineWidthProperty.asExpression() )
737 );
738 }
739 else
740 {
741 property = QgsProperty::fromExpression( u"array_to_string(%1, ';')"_s.arg( property.asExpression() ) );
742 }
743 ddProperties.setProperty( QgsSymbolLayer::Property::CustomDash, property );
744 }
745 else
746 {
747 QVector< double > rawDashVectorSizes;
748 rawDashVectorSizes.reserve( dashSource.size() );
749 for ( const QVariant &v : dashSource )
750 {
751 rawDashVectorSizes << v.toDouble();
752 }
753
754 // handle non-compliant dash vector patterns
755 if ( rawDashVectorSizes.size() == 1 )
756 {
757 // match behavior of MapBox style rendering -- if a user makes a line dash array with one element, it's ignored
758 rawDashVectorSizes.clear();
759 }
760 else if ( rawDashVectorSizes.size() % 2 == 1 )
761 {
762 // odd number of dash pattern sizes -- this isn't permitted by Qt/QGIS, but isn't explicitly blocked by the MapBox specs
763 // MapBox seems to implicitly add a 0 length gap to the array if odd length.
764 rawDashVectorSizes.append( 0 );
765 }
766
767 if ( !rawDashVectorSizes.isEmpty() && ( !lineWidthProperty.asExpression().isEmpty() ) )
768 {
769 QStringList dashArrayStringParts;
770 dashArrayStringParts.reserve( rawDashVectorSizes.size() );
771 for ( double v : std::as_const( rawDashVectorSizes ) )
772 {
773 dashArrayStringParts << qgsDoubleToString( v );
774 }
775
776 QString arrayExpression = u"array_to_string(array_foreach(array(%1),@element * (%2)), ';')"_s // skip-keyword-check
777 .arg( dashArrayStringParts.join( ',' ), lineWidthProperty.asExpression() );
779 }
780
781 // dash vector sizes for QGIS symbols must be multiplied by the target line width
782 for ( double v : std::as_const( rawDashVectorSizes ) )
783 {
784 dashVector << v * lineWidth;
785 }
786 }
787 }
788 break;
789 }
790
791 default:
792 context.pushWarning( QObject::tr( "%1: Skipping unsupported line-dasharray type (%2)" ).arg( context.layerId(), jsonLineDashArray.metaType().name() ) );
793 break;
794 }
795 }
796
797 Qt::PenCapStyle penCapStyle = Qt::FlatCap;
798 Qt::PenJoinStyle penJoinStyle = Qt::MiterJoin;
799 if ( jsonLayer.contains( u"layout"_s ) )
800 {
801 const QVariantMap jsonLayout = jsonLayer.value( u"layout"_s ).toMap();
802 if ( jsonLayout.contains( u"line-cap"_s ) )
803 {
804 penCapStyle = parseCapStyle( jsonLayout.value( u"line-cap"_s ).toString() );
805 }
806 if ( jsonLayout.contains( u"line-join"_s ) )
807 {
808 penJoinStyle = parseJoinStyle( jsonLayout.value( u"line-join"_s ).toString() );
809 }
810 }
811
812 std::unique_ptr< QgsSymbol > symbol( std::make_unique< QgsLineSymbol >() );
813 symbol->setOutputUnit( context.targetUnit() );
814
815 if ( !rasterLineSprite.isEmpty() )
816 {
817 QgsRasterLineSymbolLayer *lineSymbol = new QgsRasterLineSymbolLayer( rasterLineSprite );
818 lineSymbol->setOutputUnit( context.targetUnit() );
819 lineSymbol->setPenCapStyle( penCapStyle );
820 lineSymbol->setPenJoinStyle( penJoinStyle );
821 lineSymbol->setDataDefinedProperties( ddProperties );
822 lineSymbol->setOffset( lineOffset );
823 lineSymbol->setOffsetUnit( context.targetUnit() );
824
825 if ( lineOpacity != -1 )
826 {
827 symbol->setOpacity( lineOpacity );
828 }
829 if ( !lineOpacityProperty.asExpression().isEmpty() )
830 {
831 QgsPropertyCollection ddProperties;
832 ddProperties.setProperty( QgsSymbol::Property::Opacity, lineOpacityProperty );
833 symbol->setDataDefinedProperties( ddProperties );
834 }
835 if ( lineWidth != -1 )
836 {
837 lineSymbol->setWidth( lineWidth );
838 }
839 symbol->changeSymbolLayer( 0, lineSymbol );
840 }
841 else
842 {
843 QgsSimpleLineSymbolLayer *lineSymbol = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) );
844 Q_ASSERT( lineSymbol ); // should not fail since QgsLineSymbol() constructor instantiates a QgsSimpleLineSymbolLayer
845
846 // set render units
847 lineSymbol->setOutputUnit( context.targetUnit() );
848 lineSymbol->setPenCapStyle( penCapStyle );
849 lineSymbol->setPenJoinStyle( penJoinStyle );
850 lineSymbol->setDataDefinedProperties( ddProperties );
851 lineSymbol->setOffset( lineOffset );
852 lineSymbol->setOffsetUnit( context.targetUnit() );
853
854 if ( lineOpacity != -1 )
855 {
856 symbol->setOpacity( lineOpacity );
857 }
858 if ( !lineOpacityProperty.asExpression().isEmpty() )
859 {
860 QgsPropertyCollection ddProperties;
861 ddProperties.setProperty( QgsSymbol::Property::Opacity, lineOpacityProperty );
862 symbol->setDataDefinedProperties( ddProperties );
863 }
864 if ( lineColor.isValid() )
865 {
866 lineSymbol->setColor( lineColor );
867 }
868 if ( lineWidth != -1 )
869 {
870 lineSymbol->setWidth( lineWidth );
871 }
872 if ( !dashVector.empty() )
873 {
874 lineSymbol->setUseCustomDashPattern( true );
875 lineSymbol->setCustomDashVector( dashVector );
876 }
877 }
878
880 style.setSymbol( symbol.release() );
881 return true;
882}
883
885{
886 if ( !jsonLayer.contains( u"paint"_s ) )
887 {
888 context.pushWarning( QObject::tr( "%1: Style has no paint property, skipping" ).arg( context.layerId() ) );
889 return false;
890 }
891
892 const QVariantMap jsonPaint = jsonLayer.value( u"paint"_s ).toMap();
893
894 QgsPropertyCollection ddProperties;
895
896 // circle color
897 QColor circleFillColor;
898 if ( jsonPaint.contains( u"circle-color"_s ) )
899 {
900 const QVariant jsonCircleColor = jsonPaint.value( u"circle-color"_s );
901 switch ( jsonCircleColor.userType() )
902 {
903 case QMetaType::Type::QVariantMap:
904 ddProperties.setProperty( QgsSymbolLayer::Property::FillColor, parseInterpolateColorByZoom( jsonCircleColor.toMap(), context, &circleFillColor ) );
905 break;
906
907 case QMetaType::Type::QVariantList:
908 case QMetaType::Type::QStringList:
909 ddProperties.setProperty( QgsSymbolLayer::Property::FillColor, parseValueList( jsonCircleColor.toList(), PropertyType::Color, context, 1, 255, &circleFillColor ) );
910 break;
911
912 case QMetaType::Type::QString:
913 circleFillColor = parseColor( jsonCircleColor.toString(), context );
914 break;
915
916 default:
917 context.pushWarning( QObject::tr( "%1: Skipping unsupported circle-color type (%2)" ).arg( context.layerId(), jsonCircleColor.metaType().name() ) );
918 break;
919 }
920 }
921 else
922 {
923 // defaults to #000000
924 circleFillColor = QColor( 0, 0, 0 );
925 }
926
927 // circle radius
928 double circleDiameter = 10.0;
929 if ( jsonPaint.contains( u"circle-radius"_s ) )
930 {
931 const QVariant jsonCircleRadius = jsonPaint.value( u"circle-radius"_s );
932 switch ( jsonCircleRadius.userType() )
933 {
934 case QMetaType::Type::Int:
935 case QMetaType::Type::LongLong:
936 case QMetaType::Type::Double:
937 circleDiameter = jsonCircleRadius.toDouble() * context.pixelSizeConversionFactor() * 2;
938 break;
939
940 case QMetaType::Type::QVariantMap:
941 circleDiameter = -1;
942 ddProperties.setProperty( QgsSymbolLayer::Property::Size, parseInterpolateByZoom( jsonCircleRadius.toMap(), context, context.pixelSizeConversionFactor() * 2, &circleDiameter ) );
943 break;
944
945 case QMetaType::Type::QVariantList:
946 case QMetaType::Type::QStringList:
947 ddProperties.setProperty( QgsSymbolLayer::Property::Size, parseValueList( jsonCircleRadius.toList(), PropertyType::Numeric, context, context.pixelSizeConversionFactor() * 2, 255, nullptr, &circleDiameter ) );
948 break;
949
950 default:
951 context.pushWarning( QObject::tr( "%1: Skipping unsupported circle-radius type (%2)" ).arg( context.layerId(), jsonCircleRadius.metaType().name() ) );
952 break;
953 }
954 }
955
956 double circleOpacity = -1.0;
957 if ( jsonPaint.contains( u"circle-opacity"_s ) )
958 {
959 const QVariant jsonCircleOpacity = jsonPaint.value( u"circle-opacity"_s );
960 switch ( jsonCircleOpacity.userType() )
961 {
962 case QMetaType::Type::Int:
963 case QMetaType::Type::LongLong:
964 case QMetaType::Type::Double:
965 circleOpacity = jsonCircleOpacity.toDouble();
966 break;
967
968 case QMetaType::Type::QVariantMap:
969 ddProperties.setProperty( QgsSymbolLayer::Property::FillColor, parseInterpolateOpacityByZoom( jsonCircleOpacity.toMap(), circleFillColor.isValid() ? circleFillColor.alpha() : 255, &context ) );
970 break;
971
972 case QMetaType::Type::QVariantList:
973 case QMetaType::Type::QStringList:
974 ddProperties.setProperty( QgsSymbolLayer::Property::FillColor, parseValueList( jsonCircleOpacity.toList(), PropertyType::Opacity, context, 1, circleFillColor.isValid() ? circleFillColor.alpha() : 255 ) );
975 break;
976
977 default:
978 context.pushWarning( QObject::tr( "%1: Skipping unsupported circle-opacity type (%2)" ).arg( context.layerId(), jsonCircleOpacity.metaType().name() ) );
979 break;
980 }
981 }
982 if ( ( circleOpacity != -1 ) && circleFillColor.isValid() )
983 {
984 circleFillColor.setAlphaF( circleOpacity );
985 }
986
987 // circle stroke color
988 QColor circleStrokeColor;
989 if ( jsonPaint.contains( u"circle-stroke-color"_s ) )
990 {
991 const QVariant jsonCircleStrokeColor = jsonPaint.value( u"circle-stroke-color"_s );
992 switch ( jsonCircleStrokeColor.userType() )
993 {
994 case QMetaType::Type::QVariantMap:
995 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeColor, parseInterpolateColorByZoom( jsonCircleStrokeColor.toMap(), context, &circleStrokeColor ) );
996 break;
997
998 case QMetaType::Type::QVariantList:
999 case QMetaType::Type::QStringList:
1000 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeColor, parseValueList( jsonCircleStrokeColor.toList(), PropertyType::Color, context, 1, 255, &circleStrokeColor ) );
1001 break;
1002
1003 case QMetaType::Type::QString:
1004 circleStrokeColor = parseColor( jsonCircleStrokeColor.toString(), context );
1005 break;
1006
1007 default:
1008 context.pushWarning( QObject::tr( "%1: Skipping unsupported circle-stroke-color type (%2)" ).arg( context.layerId(), jsonCircleStrokeColor.metaType().name() ) );
1009 break;
1010 }
1011 }
1012
1013 // circle stroke width
1014 double circleStrokeWidth = -1.0;
1015 if ( jsonPaint.contains( u"circle-stroke-width"_s ) )
1016 {
1017 const QVariant circleStrokeWidthJson = jsonPaint.value( u"circle-stroke-width"_s );
1018 switch ( circleStrokeWidthJson.userType() )
1019 {
1020 case QMetaType::Type::Int:
1021 case QMetaType::Type::LongLong:
1022 case QMetaType::Type::Double:
1023 circleStrokeWidth = circleStrokeWidthJson.toDouble() * context.pixelSizeConversionFactor();
1024 break;
1025
1026 case QMetaType::Type::QVariantMap:
1027 circleStrokeWidth = -1.0;
1028 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeWidth, parseInterpolateByZoom( circleStrokeWidthJson.toMap(), context, context.pixelSizeConversionFactor(), &circleStrokeWidth ) );
1029 break;
1030
1031 case QMetaType::Type::QVariantList:
1032 case QMetaType::Type::QStringList:
1033 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeWidth, parseValueList( circleStrokeWidthJson.toList(), PropertyType::Numeric, context, context.pixelSizeConversionFactor(), 255, nullptr, &circleStrokeWidth ) );
1034 break;
1035
1036 default:
1037 context.pushWarning( QObject::tr( "%1: Skipping unsupported circle-stroke-width type (%2)" ).arg( context.layerId(), circleStrokeWidthJson.metaType().name() ) );
1038 break;
1039 }
1040 }
1041
1042 double circleStrokeOpacity = -1.0;
1043 if ( jsonPaint.contains( u"circle-stroke-opacity"_s ) )
1044 {
1045 const QVariant jsonCircleStrokeOpacity = jsonPaint.value( u"circle-stroke-opacity"_s );
1046 switch ( jsonCircleStrokeOpacity.userType() )
1047 {
1048 case QMetaType::Type::Int:
1049 case QMetaType::Type::LongLong:
1050 case QMetaType::Type::Double:
1051 circleStrokeOpacity = jsonCircleStrokeOpacity.toDouble();
1052 break;
1053
1054 case QMetaType::Type::QVariantMap:
1055 ddProperties.setProperty( QgsSymbolLayer::Property::StrokeColor, parseInterpolateOpacityByZoom( jsonCircleStrokeOpacity.toMap(), circleStrokeColor.isValid() ? circleStrokeColor.alpha() : 255, &context ) );
1056 break;
1057
1058 case QMetaType::Type::QVariantList:
1059 case QMetaType::Type::QStringList:
1060 ddProperties
1061 .setProperty( QgsSymbolLayer::Property::StrokeColor, parseValueList( jsonCircleStrokeOpacity.toList(), PropertyType::Opacity, context, 1, circleStrokeColor.isValid() ? circleStrokeColor.alpha() : 255 ) );
1062 break;
1063
1064 default:
1065 context.pushWarning( QObject::tr( "%1: Skipping unsupported circle-stroke-opacity type (%2)" ).arg( context.layerId(), jsonCircleStrokeOpacity.metaType().name() ) );
1066 break;
1067 }
1068 }
1069 if ( ( circleStrokeOpacity != -1 ) && circleStrokeColor.isValid() )
1070 {
1071 circleStrokeColor.setAlphaF( circleStrokeOpacity );
1072 }
1073
1074 // translate
1075 QPointF circleTranslate;
1076 if ( jsonPaint.contains( u"circle-translate"_s ) )
1077 {
1078 const QVariant jsonCircleTranslate = jsonPaint.value( u"circle-translate"_s );
1079 switch ( jsonCircleTranslate.userType() )
1080 {
1081 case QMetaType::Type::QVariantMap:
1082 ddProperties.setProperty( QgsSymbolLayer::Property::Offset, parseInterpolatePointByZoom( jsonCircleTranslate.toMap(), context, context.pixelSizeConversionFactor(), &circleTranslate ) );
1083 break;
1084
1085 case QMetaType::Type::QVariantList:
1086 case QMetaType::Type::QStringList:
1087 circleTranslate
1088 = QPointF( jsonCircleTranslate.toList().value( 0 ).toDouble() * context.pixelSizeConversionFactor(), jsonCircleTranslate.toList().value( 1 ).toDouble() * context.pixelSizeConversionFactor() );
1089 break;
1090
1091 default:
1092 context.pushWarning( QObject::tr( "%1: Skipping unsupported circle-translate type (%2)" ).arg( context.layerId(), jsonCircleTranslate.metaType().name() ) );
1093 break;
1094 }
1095 }
1096
1097 std::unique_ptr< QgsSymbol > symbol( std::make_unique< QgsMarkerSymbol >() );
1098 QgsSimpleMarkerSymbolLayer *markerSymbolLayer = dynamic_cast< QgsSimpleMarkerSymbolLayer * >( symbol->symbolLayer( 0 ) );
1099 Q_ASSERT( markerSymbolLayer );
1100
1101 // set render units
1102 symbol->setOutputUnit( context.targetUnit() );
1103 markerSymbolLayer->setDataDefinedProperties( ddProperties );
1104
1105 if ( !circleTranslate.isNull() )
1106 {
1107 markerSymbolLayer->setOffset( circleTranslate );
1108 markerSymbolLayer->setOffsetUnit( context.targetUnit() );
1109 }
1110
1111 if ( circleFillColor.isValid() )
1112 {
1113 markerSymbolLayer->setFillColor( circleFillColor );
1114 }
1115 if ( circleDiameter != -1 )
1116 {
1117 markerSymbolLayer->setSize( circleDiameter );
1118 markerSymbolLayer->setSizeUnit( context.targetUnit() );
1119 }
1120 if ( circleStrokeColor.isValid() )
1121 {
1122 markerSymbolLayer->setStrokeColor( circleStrokeColor );
1123 }
1124 if ( circleStrokeWidth != -1 )
1125 {
1126 markerSymbolLayer->setStrokeWidth( circleStrokeWidth );
1127 markerSymbolLayer->setStrokeWidthUnit( context.targetUnit() );
1128 }
1129
1131 style.setSymbol( symbol.release() );
1132 return true;
1133}
1134
1136 const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &renderer, bool &hasRenderer, QgsVectorTileBasicLabelingStyle &labelingStyle, bool &hasLabeling, QgsMapBoxGlStyleConversionContext &context
1137)
1138{
1139 hasLabeling = false;
1140 hasRenderer = false;
1141
1142 if ( !jsonLayer.contains( u"layout"_s ) )
1143 {
1144 context.pushWarning( QObject::tr( "%1: Style layer has no layout property, skipping" ).arg( context.layerId() ) );
1145 return;
1146 }
1147 const QVariantMap jsonLayout = jsonLayer.value( u"layout"_s ).toMap();
1148 if ( !jsonLayout.contains( u"text-field"_s ) )
1149 {
1150 hasRenderer = parseSymbolLayerAsRenderer( jsonLayer, renderer, context );
1151 return;
1152 }
1153
1154 const QVariantMap jsonPaint = jsonLayer.value( u"paint"_s ).toMap();
1155
1156 QgsPropertyCollection ddLabelProperties;
1157
1158 double textSize = 16.0 * context.pixelSizeConversionFactor();
1159 QgsProperty textSizeProperty;
1160 if ( jsonLayout.contains( u"text-size"_s ) )
1161 {
1162 const QVariant jsonTextSize = jsonLayout.value( u"text-size"_s );
1163 switch ( jsonTextSize.userType() )
1164 {
1165 case QMetaType::Type::Int:
1166 case QMetaType::Type::LongLong:
1167 case QMetaType::Type::Double:
1168 textSize = jsonTextSize.toDouble() * context.pixelSizeConversionFactor();
1169 break;
1170
1171 case QMetaType::Type::QVariantMap:
1172 textSize = -1;
1173 textSizeProperty = parseInterpolateByZoom( jsonTextSize.toMap(), context, context.pixelSizeConversionFactor(), &textSize );
1174
1175 break;
1176
1177 case QMetaType::Type::QVariantList:
1178 case QMetaType::Type::QStringList:
1179 textSize = -1;
1180 textSizeProperty = parseValueList( jsonTextSize.toList(), PropertyType::Numeric, context, context.pixelSizeConversionFactor(), 255, nullptr, &textSize );
1181 break;
1182
1183 default:
1184 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-size type (%2)" ).arg( context.layerId(), jsonTextSize.metaType().name() ) );
1185 break;
1186 }
1187
1188 if ( textSizeProperty )
1189 {
1190 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::Size, textSizeProperty );
1191 }
1192 }
1193
1194 // a rough average of ems to character count conversion for a variety of fonts
1195 constexpr double EM_TO_CHARS = 2.0;
1196
1197 double textMaxWidth = -1;
1198 if ( jsonLayout.contains( u"text-max-width"_s ) )
1199 {
1200 const QVariant jsonTextMaxWidth = jsonLayout.value( u"text-max-width"_s );
1201 switch ( jsonTextMaxWidth.userType() )
1202 {
1203 case QMetaType::Type::Int:
1204 case QMetaType::Type::LongLong:
1205 case QMetaType::Type::Double:
1206 textMaxWidth = jsonTextMaxWidth.toDouble() * EM_TO_CHARS;
1207 break;
1208
1209 case QMetaType::Type::QVariantMap:
1210 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::AutoWrapLength, parseInterpolateByZoom( jsonTextMaxWidth.toMap(), context, EM_TO_CHARS, &textMaxWidth ) );
1211 break;
1212
1213 case QMetaType::Type::QVariantList:
1214 case QMetaType::Type::QStringList:
1215 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::AutoWrapLength, parseValueList( jsonTextMaxWidth.toList(), PropertyType::Numeric, context, EM_TO_CHARS, 255, nullptr, &textMaxWidth ) );
1216 break;
1217
1218 default:
1219 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-max-width type (%2)" ).arg( context.layerId(), jsonTextMaxWidth.metaType().name() ) );
1220 break;
1221 }
1222 }
1223 else
1224 {
1225 // defaults to 10
1226 textMaxWidth = 10 * EM_TO_CHARS;
1227 }
1228
1229 double textLetterSpacing = -1;
1230 if ( jsonLayout.contains( u"text-letter-spacing"_s ) )
1231 {
1232 const QVariant jsonTextLetterSpacing = jsonLayout.value( u"text-letter-spacing"_s );
1233 switch ( jsonTextLetterSpacing.userType() )
1234 {
1235 case QMetaType::Type::Int:
1236 case QMetaType::Type::LongLong:
1237 case QMetaType::Type::Double:
1238 textLetterSpacing = jsonTextLetterSpacing.toDouble();
1239 break;
1240
1241 case QMetaType::Type::QVariantMap:
1242 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::FontLetterSpacing, parseInterpolateByZoom( jsonTextLetterSpacing.toMap(), context, 1, &textLetterSpacing ) );
1243 break;
1244
1245 case QMetaType::Type::QVariantList:
1246 case QMetaType::Type::QStringList:
1247 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::FontLetterSpacing, parseValueList( jsonTextLetterSpacing.toList(), PropertyType::Numeric, context, 1, 255, nullptr, &textLetterSpacing ) );
1248 break;
1249
1250 default:
1251 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-letter-spacing type (%2)" ).arg( context.layerId(), jsonTextLetterSpacing.metaType().name() ) );
1252 break;
1253 }
1254 }
1255
1256 QFont textFont;
1257 bool foundFont = false;
1258 QString fontName;
1259 QString fontStyleName;
1260
1261 bool allowOverlap = jsonLayout.contains( u"text-allow-overlap"_s ) && jsonLayout.value( u"text-allow-overlap"_s ).toBool();
1262
1263 if ( jsonLayout.contains( u"text-font"_s ) )
1264 {
1265 auto splitFontFamily = []( const QString &fontName, QString &family, QString &style ) -> bool {
1266 QString matchedFamily;
1267 const QStringList textFontParts = fontName.split( ' ' );
1268 for ( int i = textFontParts.size() - 1; i >= 1; --i )
1269 {
1270 const QString candidateFontFamily = textFontParts.mid( 0, i ).join( ' ' );
1271 const QString candidateFontStyle = textFontParts.mid( i ).join( ' ' );
1272
1273 const QString processedFontFamily = QgsApplication::fontManager()->processFontFamilyName( candidateFontFamily );
1274 if ( QgsFontUtils::fontFamilyHasStyle( processedFontFamily, candidateFontStyle ) )
1275 {
1276 family = processedFontFamily;
1277 style = candidateFontStyle;
1278 return true;
1279 }
1280 else if ( QgsApplication::fontManager()->tryToDownloadFontFamily( processedFontFamily, matchedFamily ) )
1281 {
1282 if ( processedFontFamily == matchedFamily )
1283 {
1284 family = processedFontFamily;
1285 style = candidateFontStyle;
1286 }
1287 else
1288 {
1289 family = matchedFamily;
1290 style = processedFontFamily;
1291 style.replace( matchedFamily, QString() );
1292 style = style.trimmed();
1293 if ( !style.isEmpty() && !candidateFontStyle.isEmpty() )
1294 {
1295 style += u" %1"_s.arg( candidateFontStyle );
1296 }
1297 }
1298 return true;
1299 }
1300 }
1301
1302 const QString processedFontFamily = QgsApplication::fontManager()->processFontFamilyName( fontName );
1303 if ( QFontDatabase().hasFamily( processedFontFamily ) )
1304 {
1305 // the json isn't following the spec correctly!!
1306 family = processedFontFamily;
1307 style.clear();
1308 return true;
1309 }
1310 else if ( QgsApplication::fontManager()->tryToDownloadFontFamily( processedFontFamily, matchedFamily ) )
1311 {
1312 family = matchedFamily;
1313 style.clear();
1314 return true;
1315 }
1316 return false;
1317 };
1318
1319 const QVariant jsonTextFont = jsonLayout.value( u"text-font"_s );
1320 if ( jsonTextFont.userType() != QMetaType::Type::QVariantList
1321 && jsonTextFont.userType() != QMetaType::Type::QStringList
1322 && jsonTextFont.userType() != QMetaType::Type::QString
1323 && jsonTextFont.userType() != QMetaType::Type::QVariantMap )
1324 {
1325 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-font type (%2)" ).arg( context.layerId(), jsonTextFont.metaType().name() ) );
1326 }
1327 else
1328 {
1329 switch ( jsonTextFont.userType() )
1330 {
1331 case QMetaType::Type::QVariantList:
1332 case QMetaType::Type::QStringList:
1333 fontName = jsonTextFont.toList().value( 0 ).toString();
1334 break;
1335
1336 case QMetaType::Type::QString:
1337 fontName = jsonTextFont.toString();
1338 break;
1339
1340 case QMetaType::Type::QVariantMap:
1341 {
1342 QString familyCaseString = u"CASE "_s;
1343 QString styleCaseString = u"CASE "_s;
1344 QString fontFamily;
1345 const QVariantList stops = jsonTextFont.toMap().value( u"stops"_s ).toList();
1346
1347 bool error = false;
1348 for ( int i = 0; i < stops.length() - 1; ++i )
1349 {
1350 // bottom zoom and value
1351 const QVariant bz = stops.value( i ).toList().value( 0 );
1352 const QString bv = stops.value( i ).toList().value( 1 ).userType() == QMetaType::Type::QString ? stops.value( i ).toList().value( 1 ).toString()
1353 : stops.value( i ).toList().value( 1 ).toList().value( 0 ).toString();
1354 if ( bz.userType() == QMetaType::Type::QVariantList || bz.userType() == QMetaType::Type::QStringList )
1355 {
1356 context.pushWarning( QObject::tr( "%1: Expressions in interpolation function are not supported, skipping." ).arg( context.layerId() ) );
1357 error = true;
1358 break;
1359 }
1360
1361 // top zoom
1362 const QVariant tz = stops.value( i + 1 ).toList().value( 0 );
1363 if ( tz.userType() == QMetaType::Type::QVariantList || tz.userType() == QMetaType::Type::QStringList )
1364 {
1365 context.pushWarning( QObject::tr( "%1: Expressions in interpolation function are not supported, skipping." ).arg( context.layerId() ) );
1366 error = true;
1367 break;
1368 }
1369
1370 if ( splitFontFamily( bv, fontFamily, fontStyleName ) )
1371 {
1372 familyCaseString += QStringLiteral(
1373 "WHEN @vector_tile_zoom > %1 AND @vector_tile_zoom <= %2 "
1374 "THEN %3 "
1375 )
1376 .arg( bz.toString(), tz.toString(), QgsExpression::quotedValue( fontFamily ) );
1377 styleCaseString += QStringLiteral(
1378 "WHEN @vector_tile_zoom > %1 AND @vector_tile_zoom <= %2 "
1379 "THEN %3 "
1380 )
1381 .arg( bz.toString(), tz.toString(), QgsExpression::quotedValue( fontStyleName ) );
1382 }
1383 else
1384 {
1385 context.pushWarning( QObject::tr( "%1: Referenced font %2 is not available on system" ).arg( context.layerId(), bv ) );
1386 }
1387 }
1388 if ( error )
1389 break;
1390
1391 const QString bv = stops.constLast().toList().value( 1 ).userType() == QMetaType::Type::QString ? stops.constLast().toList().value( 1 ).toString()
1392 : stops.constLast().toList().value( 1 ).toList().value( 0 ).toString();
1393 if ( splitFontFamily( bv, fontFamily, fontStyleName ) )
1394 {
1395 familyCaseString += u"ELSE %1 END"_s.arg( QgsExpression::quotedValue( fontFamily ) );
1396 styleCaseString += u"ELSE %1 END"_s.arg( QgsExpression::quotedValue( fontStyleName ) );
1397 }
1398 else
1399 {
1400 context.pushWarning( QObject::tr( "%1: Referenced font %2 is not available on system" ).arg( context.layerId(), bv ) );
1401 }
1402
1403 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::Family, QgsProperty::fromExpression( familyCaseString ) );
1405
1406 foundFont = true;
1407 fontName = fontFamily;
1408
1409 break;
1410 }
1411
1412 default:
1413 break;
1414 }
1415
1416 QString fontFamily;
1417 if ( splitFontFamily( fontName, fontFamily, fontStyleName ) )
1418 {
1419 textFont = QgsFontUtils::createFont( fontFamily );
1420 if ( !fontStyleName.isEmpty() )
1421 textFont.setStyleName( fontStyleName );
1422 foundFont = true;
1423 }
1424 }
1425 }
1426 else
1427 {
1428 // Defaults to ["Open Sans Regular","Arial Unicode MS Regular"].
1429 if ( QgsFontUtils::fontFamilyHasStyle( u"Open Sans"_s, u"Regular"_s ) )
1430 {
1431 fontName = u"Open Sans"_s;
1432 textFont = QgsFontUtils::createFont( fontName );
1433 textFont.setStyleName( u"Regular"_s );
1434 fontStyleName = u"Regular"_s;
1435 foundFont = true;
1436 }
1437 else if ( QgsFontUtils::fontFamilyHasStyle( u"Arial Unicode MS"_s, u"Regular"_s ) )
1438 {
1439 fontName = u"Arial Unicode MS"_s;
1440 textFont = QgsFontUtils::createFont( fontName );
1441 textFont.setStyleName( u"Regular"_s );
1442 fontStyleName = u"Regular"_s;
1443 foundFont = true;
1444 }
1445 else
1446 {
1447 fontName = u"Open Sans, Arial Unicode MS"_s;
1448 }
1449 }
1450 if ( !foundFont && !fontName.isEmpty() )
1451 {
1452 context.pushWarning( QObject::tr( "%1: Referenced font %2 is not available on system" ).arg( context.layerId(), fontName ) );
1453 }
1454
1455 // text color
1456 QColor textColor;
1457 if ( jsonPaint.contains( u"text-color"_s ) )
1458 {
1459 const QVariant jsonTextColor = jsonPaint.value( u"text-color"_s );
1460 switch ( jsonTextColor.userType() )
1461 {
1462 case QMetaType::Type::QVariantMap:
1463 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::Color, parseInterpolateColorByZoom( jsonTextColor.toMap(), context, &textColor ) );
1464 break;
1465
1466 case QMetaType::Type::QVariantList:
1467 case QMetaType::Type::QStringList:
1468 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::Color, parseValueList( jsonTextColor.toList(), PropertyType::Color, context, 1, 255, &textColor ) );
1469 break;
1470
1471 case QMetaType::Type::QString:
1472 textColor = parseColor( jsonTextColor.toString(), context );
1473 break;
1474
1475 default:
1476 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-color type (%2)" ).arg( context.layerId(), jsonTextColor.metaType().name() ) );
1477 break;
1478 }
1479 }
1480 else
1481 {
1482 // defaults to #000000
1483 textColor = QColor( 0, 0, 0 );
1484 }
1485
1486 // buffer color
1487 QColor bufferColor( 0, 0, 0, 0 );
1488 if ( jsonPaint.contains( u"text-halo-color"_s ) )
1489 {
1490 const QVariant jsonBufferColor = jsonPaint.value( u"text-halo-color"_s );
1491 switch ( jsonBufferColor.userType() )
1492 {
1493 case QMetaType::Type::QVariantMap:
1494 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::BufferColor, parseInterpolateColorByZoom( jsonBufferColor.toMap(), context, &bufferColor ) );
1495 break;
1496
1497 case QMetaType::Type::QVariantList:
1498 case QMetaType::Type::QStringList:
1499 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::BufferColor, parseValueList( jsonBufferColor.toList(), PropertyType::Color, context, 1, 255, &bufferColor ) );
1500 break;
1501
1502 case QMetaType::Type::QString:
1503 bufferColor = parseColor( jsonBufferColor.toString(), context );
1504 break;
1505
1506 default:
1507 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-halo-color type (%2)" ).arg( context.layerId(), jsonBufferColor.metaType().name() ) );
1508 break;
1509 }
1510 }
1511
1512 double bufferSize = 0.0;
1513 // the pixel based text buffers appear larger when rendered on the web - so automatically scale
1514 // them up when converting to a QGIS style
1515 // (this number is based on trial-and-error comparisons only!)
1516 constexpr double BUFFER_SIZE_SCALE = 2.0;
1517 if ( jsonPaint.contains( u"text-halo-width"_s ) )
1518 {
1519 const QVariant jsonHaloWidth = jsonPaint.value( u"text-halo-width"_s );
1520 QString bufferSizeDataDefined;
1521 switch ( jsonHaloWidth.userType() )
1522 {
1523 case QMetaType::Type::Int:
1524 case QMetaType::Type::LongLong:
1525 case QMetaType::Type::Double:
1526 bufferSize = jsonHaloWidth.toDouble() * context.pixelSizeConversionFactor() * BUFFER_SIZE_SCALE;
1527 break;
1528
1529 case QMetaType::Type::QVariantMap:
1530 bufferSize = 1;
1531 bufferSizeDataDefined = parseInterpolateByZoom( jsonHaloWidth.toMap(), context, context.pixelSizeConversionFactor() * BUFFER_SIZE_SCALE, &bufferSize ).asExpression();
1532 break;
1533
1534 case QMetaType::Type::QVariantList:
1535 case QMetaType::Type::QStringList:
1536 bufferSize = 1;
1537 bufferSizeDataDefined = parseValueList( jsonHaloWidth.toList(), PropertyType::Numeric, context, context.pixelSizeConversionFactor() * BUFFER_SIZE_SCALE, 255, nullptr, &bufferSize ).asExpression();
1538 break;
1539
1540 default:
1541 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-halo-width type (%2)" ).arg( context.layerId(), jsonHaloWidth.metaType().name() ) );
1542 break;
1543 }
1544
1545 // from the specs halo should not be larger than 1/4 of the text-size
1546 // https://docs.mapbox.com/style-spec/reference/layers/#paint-symbol-text-halo-width
1547 if ( bufferSize > 0 )
1548 {
1549 if ( textSize > 0 && bufferSizeDataDefined.isEmpty() )
1550 {
1551 bufferSize = std::min( bufferSize, textSize * BUFFER_SIZE_SCALE / 4 );
1552 }
1553 else if ( textSize > 0 && !bufferSizeDataDefined.isEmpty() )
1554 {
1555 bufferSizeDataDefined = u"min(%1/4, %2)"_s.arg( textSize * BUFFER_SIZE_SCALE ).arg( bufferSizeDataDefined );
1556 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::BufferSize, QgsProperty::fromExpression( bufferSizeDataDefined ) );
1557 }
1558 else if ( !bufferSizeDataDefined.isEmpty() )
1559 {
1560 bufferSizeDataDefined = u"min(%1*%2/4, %3)"_s.arg( textSizeProperty.asExpression() ).arg( BUFFER_SIZE_SCALE ).arg( bufferSizeDataDefined );
1561 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::BufferSize, QgsProperty::fromExpression( bufferSizeDataDefined ) );
1562 }
1563 else if ( bufferSizeDataDefined.isEmpty() )
1564 {
1565 bufferSizeDataDefined = u"min(%1*%2/4, %3)"_s.arg( textSizeProperty.asExpression() ).arg( BUFFER_SIZE_SCALE ).arg( bufferSize );
1566 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::BufferSize, QgsProperty::fromExpression( bufferSizeDataDefined ) );
1567 }
1568 }
1569 }
1570
1571 double haloBlurSize = 0;
1572 if ( jsonPaint.contains( u"text-halo-blur"_s ) )
1573 {
1574 const QVariant jsonTextHaloBlur = jsonPaint.value( u"text-halo-blur"_s );
1575 switch ( jsonTextHaloBlur.userType() )
1576 {
1577 case QMetaType::Type::Int:
1578 case QMetaType::Type::LongLong:
1579 case QMetaType::Type::Double:
1580 {
1581 haloBlurSize = jsonTextHaloBlur.toDouble() * context.pixelSizeConversionFactor();
1582 break;
1583 }
1584
1585 default:
1586 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-halo-blur type (%2)" ).arg( context.layerId(), jsonTextHaloBlur.metaType().name() ) );
1587 break;
1588 }
1589 }
1590
1591 QgsTextFormat format;
1592 format.setSizeUnit( context.targetUnit() );
1593 if ( textColor.isValid() )
1594 format.setColor( textColor );
1595 if ( textSize >= 0 )
1596 format.setSize( textSize );
1597 if ( foundFont )
1598 {
1599 format.setFont( textFont );
1600 if ( !fontStyleName.isEmpty() )
1601 format.setNamedStyle( fontStyleName );
1602 }
1603 if ( textLetterSpacing > 0 )
1604 {
1605 QFont f = format.font();
1606 f.setLetterSpacing( QFont::AbsoluteSpacing, textLetterSpacing );
1607 format.setFont( f );
1608 }
1609
1610 if ( bufferSize > 0 )
1611 {
1612 // Color and opacity are separate components in QGIS
1613 const double opacity = bufferColor.alphaF();
1614 bufferColor.setAlphaF( 1.0 );
1615
1616 format.buffer().setEnabled( true );
1617 format.buffer().setSize( bufferSize );
1618 format.buffer().setSizeUnit( context.targetUnit() );
1619 format.buffer().setColor( bufferColor );
1620 format.buffer().setOpacity( opacity );
1621
1622 if ( haloBlurSize > 0 )
1623 {
1624 QgsEffectStack *stack = new QgsEffectStack();
1625 QgsBlurEffect *blur = new QgsBlurEffect();
1626 blur->setEnabled( true );
1627 blur->setBlurUnit( context.targetUnit() );
1628 blur->setBlurLevel( haloBlurSize );
1630 stack->appendEffect( blur );
1631 stack->setEnabled( true );
1632 format.buffer().setPaintEffect( stack );
1633 }
1634 }
1635
1636 QgsPalLayerSettings labelSettings;
1637 if ( allowOverlap )
1638 {
1639 QgsLabelPlacementSettings placementSettings = labelSettings.placementSettings();
1641 placementSettings.setAllowDegradedPlacement( true );
1642 labelSettings.setPlacementSettings( placementSettings );
1643 }
1644
1645 if ( textMaxWidth > 0 )
1646 {
1647 labelSettings.autoWrapLength = textMaxWidth;
1648 }
1649
1650 // convert field name
1651 if ( jsonLayout.contains( u"text-field"_s ) )
1652 {
1653 const QVariant jsonTextField = jsonLayout.value( u"text-field"_s );
1654 switch ( jsonTextField.userType() )
1655 {
1656 case QMetaType::Type::QString:
1657 {
1658 labelSettings.fieldName = processLabelField( jsonTextField.toString(), labelSettings.isExpression );
1659 break;
1660 }
1661
1662 case QMetaType::Type::QVariantList:
1663 case QMetaType::Type::QStringList:
1664 {
1665 const QVariantList textFieldList = jsonTextField.toList();
1666 /*
1667 * e.g.
1668 * "text-field": ["format",
1669 * "foo", { "font-scale": 1.2 },
1670 * "bar", { "font-scale": 0.8 }
1671 * ]
1672 */
1673 if ( textFieldList.size() > 2 && textFieldList.at( 0 ).toString() == "format"_L1 )
1674 {
1675 QStringList parts;
1676 for ( int i = 1; i < textFieldList.size(); ++i )
1677 {
1678 bool isExpression = false;
1679 const QString part = processLabelField( textFieldList.at( i ).toString(), isExpression );
1680 if ( !isExpression )
1681 parts << QgsExpression::quotedColumnRef( part );
1682 else
1683 parts << part;
1684 // TODO -- we could also translate font color, underline, overline, strikethrough to HTML tags!
1685 i += 1;
1686 }
1687 labelSettings.fieldName = u"concat(%1)"_s.arg( parts.join( ',' ) );
1688 labelSettings.isExpression = true;
1689 }
1690 else
1691 {
1692 /*
1693 * e.g.
1694 * "text-field": ["to-string", ["get", "name"]]
1695 */
1696 labelSettings.fieldName = parseExpression( textFieldList, context );
1697 labelSettings.isExpression = true;
1698 }
1699 break;
1700 }
1701
1702 case QMetaType::Type::QVariantMap:
1703 {
1704 const QVariantList stops = jsonTextField.toMap().value( u"stops"_s ).toList();
1705 if ( !stops.empty() )
1706 {
1707 labelSettings.fieldName = parseLabelStops( stops, context );
1708 labelSettings.isExpression = true;
1709 }
1710 else
1711 {
1712 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-field dictionary" ).arg( context.layerId() ) );
1713 }
1714 break;
1715 }
1716
1717 default:
1718 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-field type (%2)" ).arg( context.layerId(), jsonTextField.metaType().name() ) );
1719 break;
1720 }
1721 }
1722
1723 if ( jsonLayout.contains( u"text-rotate"_s ) )
1724 {
1725 const QVariant jsonTextRotate = jsonLayout.value( u"text-rotate"_s );
1726 switch ( jsonTextRotate.userType() )
1727 {
1728 case QMetaType::Type::Double:
1729 case QMetaType::Type::Int:
1730 {
1731 labelSettings.angleOffset = jsonTextRotate.toDouble();
1732 break;
1733 }
1734
1735 case QMetaType::Type::QVariantList:
1736 case QMetaType::Type::QStringList:
1737 {
1738 const QgsProperty property = parseValueList( jsonTextRotate.toList(), PropertyType::Numeric, context );
1739 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::LabelRotation, property );
1740 break;
1741 }
1742
1743 case QMetaType::Type::QVariantMap:
1744 {
1745 QVariantMap rotateMap = jsonTextRotate.toMap();
1746 if ( rotateMap.contains( u"property"_s ) && rotateMap[u"type"_s].toString() == "identity"_L1 )
1747 {
1748 const QgsProperty property = QgsProperty::fromExpression( rotateMap[u"property"_s].toString() );
1749 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::LabelRotation, property );
1750 }
1751 else
1752 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-rotate map content (%2)" ).arg( context.layerId(), QString( QJsonDocument::fromVariant( rotateMap ).toJson() ) ) );
1753 break;
1754 }
1755
1756 default:
1757 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-rotate type (%2)" ).arg( context.layerId(), jsonTextRotate.metaType().name() ) );
1758 break;
1759 }
1760 }
1761
1762 if ( jsonLayout.contains( u"text-transform"_s ) )
1763 {
1764 const QString textTransform = jsonLayout.value( u"text-transform"_s ).toString();
1765 if ( textTransform == "uppercase"_L1 )
1766 {
1767 labelSettings.fieldName = u"upper(%1)"_s.arg( labelSettings.isExpression ? labelSettings.fieldName : QgsExpression::quotedColumnRef( labelSettings.fieldName ) );
1768 }
1769 else if ( textTransform == "lowercase"_L1 )
1770 {
1771 labelSettings.fieldName = u"lower(%1)"_s.arg( labelSettings.isExpression ? labelSettings.fieldName : QgsExpression::quotedColumnRef( labelSettings.fieldName ) );
1772 }
1773 labelSettings.isExpression = true;
1774 }
1775
1778 if ( jsonLayout.contains( u"symbol-placement"_s ) )
1779 {
1780 const QString symbolPlacement = jsonLayout.value( u"symbol-placement"_s ).toString();
1781 if ( symbolPlacement == "line"_L1 )
1782 {
1785 geometryType = Qgis::GeometryType::Line;
1786
1787 if ( jsonLayout.contains( u"text-rotation-alignment"_s ) )
1788 {
1789 const QString textRotationAlignment = jsonLayout.value( u"text-rotation-alignment"_s ).toString();
1790 if ( textRotationAlignment == "viewport"_L1 )
1791 {
1793 }
1794 }
1795
1796 if ( labelSettings.placement == Qgis::LabelPlacement::Curved )
1797 {
1798 QPointF textOffset;
1799 QgsProperty textOffsetProperty;
1800 if ( jsonLayout.contains( u"text-offset"_s ) )
1801 {
1802 const QVariant jsonTextOffset = jsonLayout.value( u"text-offset"_s );
1803
1804 // units are ems!
1805 switch ( jsonTextOffset.userType() )
1806 {
1807 case QMetaType::Type::QVariantMap:
1808 textOffsetProperty = parseInterpolatePointByZoom( jsonTextOffset.toMap(), context, !textSizeProperty ? textSize : 1.0, &textOffset );
1809 if ( !textSizeProperty )
1810 {
1811 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::LabelDistance, u"abs(array_get(%1,1))-%2"_s.arg( textOffsetProperty.asExpression() ).arg( textSize ) );
1812 }
1813 else
1814 {
1815 ddLabelProperties.setProperty(
1817 u"with_variable('text_size',%2,abs(array_get(%1,1))*@text_size-@text_size)"_s.arg( textOffsetProperty.asExpression(), textSizeProperty.asExpression() )
1818 );
1819 }
1820 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::LinePlacementOptions, u"if(array_get(%1,1)>0,'BL','AL')"_s.arg( textOffsetProperty.asExpression() ) );
1821 break;
1822
1823 case QMetaType::Type::QVariantList:
1824 case QMetaType::Type::QStringList:
1825 textOffset = QPointF( jsonTextOffset.toList().value( 0 ).toDouble() * textSize, jsonTextOffset.toList().value( 1 ).toDouble() * textSize );
1826 break;
1827
1828 default:
1829 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-offset type (%2)" ).arg( context.layerId(), jsonTextOffset.metaType().name() ) );
1830 break;
1831 }
1832
1833 if ( !textOffset.isNull() )
1834 {
1835 labelSettings.distUnits = context.targetUnit();
1836 labelSettings.dist = std::abs( textOffset.y() ) - textSize;
1838 if ( textSizeProperty && !textOffsetProperty )
1839 {
1840 ddLabelProperties.setProperty(
1842 u"with_variable('text_size',%2,%1*@text_size-@text_size)"_s.arg( std::abs( textOffset.y() / textSize ) ).arg( textSizeProperty.asExpression() )
1843 );
1844 }
1845 }
1846 }
1847
1848 if ( textOffset.isNull() )
1849 {
1851 }
1852 }
1853 }
1854 }
1855
1856 if ( jsonLayout.contains( u"text-justify"_s ) )
1857 {
1858 const QVariant jsonTextJustify = jsonLayout.value( u"text-justify"_s );
1859
1860 // default is center
1861 QString textAlign = u"center"_s;
1862
1863 const QVariantMap conversionMap { { u"left"_s, u"left"_s }, { u"center"_s, u"center"_s }, { u"right"_s, u"right"_s }, { u"auto"_s, u"follow"_s } };
1864
1865 switch ( jsonTextJustify.userType() )
1866 {
1867 case QMetaType::Type::QString:
1868 textAlign = jsonTextJustify.toString();
1869 break;
1870
1871 case QMetaType::Type::QVariantList:
1872 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::OffsetQuad, QgsProperty::fromExpression( parseStringStops( jsonTextJustify.toList(), context, conversionMap, &textAlign ) ) );
1873 break;
1874
1875 case QMetaType::Type::QVariantMap:
1876 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::OffsetQuad, parseInterpolateStringByZoom( jsonTextJustify.toMap(), context, conversionMap, &textAlign ) );
1877 break;
1878
1879 default:
1880 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-justify type (%2)" ).arg( context.layerId(), jsonTextJustify.metaType().name() ) );
1881 break;
1882 }
1883
1884 if ( textAlign == "left"_L1 )
1886 else if ( textAlign == "right"_L1 )
1888 else if ( textAlign == "center"_L1 )
1890 else if ( textAlign == "follow"_L1 )
1892 }
1893 else
1894 {
1896 }
1897
1898 if ( labelSettings.placement == Qgis::LabelPlacement::OverPoint )
1899 {
1900 if ( jsonLayout.contains( u"text-anchor"_s ) )
1901 {
1902 const QVariant jsonTextAnchor = jsonLayout.value( u"text-anchor"_s );
1903 QString textAnchor;
1904
1905 const QVariantMap conversionMap {
1906 { u"center"_s, 4 },
1907 { u"left"_s, 5 },
1908 { u"right"_s, 3 },
1909 { u"top"_s, 7 },
1910 { u"bottom"_s, 1 },
1911 { u"top-left"_s, 8 },
1912 { u"top-right"_s, 6 },
1913 { u"bottom-left"_s, 2 },
1914 { u"bottom-right"_s, 0 },
1915 };
1916
1917 switch ( jsonTextAnchor.userType() )
1918 {
1919 case QMetaType::Type::QString:
1920 textAnchor = jsonTextAnchor.toString();
1921 break;
1922
1923 case QMetaType::Type::QVariantList:
1924 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::OffsetQuad, QgsProperty::fromExpression( parseStringStops( jsonTextAnchor.toList(), context, conversionMap, &textAnchor ) ) );
1925 break;
1926
1927 case QMetaType::Type::QVariantMap:
1928 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::OffsetQuad, parseInterpolateStringByZoom( jsonTextAnchor.toMap(), context, conversionMap, &textAnchor ) );
1929 break;
1930
1931 default:
1932 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-anchor type (%2)" ).arg( context.layerId(), jsonTextAnchor.metaType().name() ) );
1933 break;
1934 }
1935
1936 if ( textAnchor == "center"_L1 )
1938 else if ( textAnchor == "left"_L1 )
1940 else if ( textAnchor == "right"_L1 )
1942 else if ( textAnchor == "top"_L1 )
1944 else if ( textAnchor == "bottom"_L1 )
1946 else if ( textAnchor == "top-left"_L1 )
1948 else if ( textAnchor == "top-right"_L1 )
1950 else if ( textAnchor == "bottom-left"_L1 )
1952 else if ( textAnchor == "bottom-right"_L1 )
1954 }
1955
1956 QPointF textOffset;
1957 if ( jsonLayout.contains( u"text-offset"_s ) )
1958 {
1959 const QVariant jsonTextOffset = jsonLayout.value( u"text-offset"_s );
1960
1961 // units are ems!
1962 switch ( jsonTextOffset.userType() )
1963 {
1964 case QMetaType::Type::QVariantMap:
1965 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::OffsetXY, parseInterpolatePointByZoom( jsonTextOffset.toMap(), context, textSize, &textOffset ) );
1966 break;
1967
1968 case QMetaType::Type::QVariantList:
1969 case QMetaType::Type::QStringList:
1970 textOffset = QPointF( jsonTextOffset.toList().value( 0 ).toDouble() * textSize, jsonTextOffset.toList().value( 1 ).toDouble() * textSize );
1971 break;
1972
1973 default:
1974 context.pushWarning( QObject::tr( "%1: Skipping unsupported text-offset type (%2)" ).arg( context.layerId(), jsonTextOffset.metaType().name() ) );
1975 break;
1976 }
1977
1978 if ( !textOffset.isNull() )
1979 {
1980 labelSettings.offsetUnits = context.targetUnit();
1981 labelSettings.xOffset = textOffset.x();
1982 labelSettings.yOffset = textOffset.y();
1983 }
1984 }
1985 }
1986
1987 if ( jsonLayout.contains( u"icon-image"_s ) && ( labelSettings.placement == Qgis::LabelPlacement::Horizontal || labelSettings.placement == Qgis::LabelPlacement::Curved ) )
1988 {
1989 QSize spriteSize;
1990 QString spriteProperty, spriteSizeProperty;
1991 const QString sprite = retrieveSpriteAsBase64WithProperties( jsonLayout.value( u"icon-image"_s ), context, spriteSize, spriteProperty, spriteSizeProperty );
1992 if ( !sprite.isEmpty() )
1993 {
1994 double size = 1.0;
1995 if ( jsonLayout.contains( u"icon-size"_s ) )
1996 {
1997 QgsProperty property;
1998 const QVariant jsonIconSize = jsonLayout.value( u"icon-size"_s );
1999 switch ( jsonIconSize.userType() )
2000 {
2001 case QMetaType::Type::Int:
2002 case QMetaType::Type::LongLong:
2003 case QMetaType::Type::Double:
2004 {
2005 size = jsonIconSize.toDouble();
2006 if ( !spriteSizeProperty.isEmpty() )
2007 {
2008 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::ShapeSizeX, QgsProperty::fromExpression( u"with_variable('marker_size',%1,%2*@marker_size)"_s.arg( spriteSizeProperty ).arg( size ) ) );
2009 }
2010 break;
2011 }
2012
2013 case QMetaType::Type::QVariantMap:
2014 property = parseInterpolateByZoom( jsonIconSize.toMap(), context, 1, &size );
2015 break;
2016
2017 case QMetaType::Type::QVariantList:
2018 case QMetaType::Type::QStringList:
2019 property = parseValueList( jsonIconSize.toList(), PropertyType::Numeric, context );
2020 break;
2021 default:
2022 context.pushWarning( QObject::tr( "%1: Skipping non-implemented icon-size type (%2)" ).arg( context.layerId(), jsonIconSize.metaType().name() ) );
2023 break;
2024 }
2025
2026 if ( !property.expressionString().isEmpty() )
2027 {
2028 if ( !spriteSizeProperty.isEmpty() )
2029 {
2030 ddLabelProperties
2031 .setProperty( QgsPalLayerSettings::Property::ShapeSizeX, QgsProperty::fromExpression( u"with_variable('marker_size',%1,(%2)*@marker_size)"_s.arg( spriteSizeProperty ).arg( property.expressionString() ) ) );
2032 }
2033 else
2034 {
2035 ddLabelProperties.setProperty( QgsPalLayerSettings::Property::ShapeSizeX, QgsProperty::fromExpression( u"(%2)*%1"_s.arg( spriteSize.width() ).arg( property.expressionString() ) ) );
2036 }
2037 }
2038 }
2039
2041 markerLayer->setPath( sprite );
2042 markerLayer->setSize( spriteSize.width() );
2043 markerLayer->setSizeUnit( context.targetUnit() );
2044
2045 if ( !spriteProperty.isEmpty() )
2046 {
2047 QgsPropertyCollection markerDdProperties;
2048 markerDdProperties.setProperty( QgsSymbolLayer::Property::Name, QgsProperty::fromExpression( spriteProperty ) );
2049 markerLayer->setDataDefinedProperties( markerDdProperties );
2050 }
2051
2052 QgsTextBackgroundSettings backgroundSettings;
2053 backgroundSettings.setEnabled( true );
2055 backgroundSettings.setSize( spriteSize * size );
2056 backgroundSettings.setSizeUnit( context.targetUnit() );
2058 backgroundSettings.setMarkerSymbol( new QgsMarkerSymbol( QgsSymbolLayerList() << markerLayer ) );
2059 format.setBackground( backgroundSettings );
2060 }
2061 }
2062
2063#if 0
2064 // TODO: re-enable when the cost of label duplicate removal within distance is more reasonable
2065 if ( jsonLayout.contains( u"symbol-spacing"_s ) )
2066 {
2067 double spacing;
2068 const QVariant jsonSpacing = jsonLayout.value( u"symbol-spacing"_s );
2069
2070 // main checkbox in labeling GUI
2071 QgsLabelThinningSettings thinningSettings = labelSettings.thinningSettings();
2072 thinningSettings.setAllowDuplicateRemoval( true );
2073 thinningSettings.setMinimumDistanceToDuplicateUnit( context.targetUnit() );
2074 labelSettings.setThinningSettings( thinningSettings );
2075
2076 QgsProperty spacingProp;
2077
2078 switch ( jsonSpacing.userType() )
2079 {
2080 case QMetaType::Type::Int:
2081 case QMetaType::Type::LongLong:
2082 case QMetaType::Type::Double:
2083 {
2084 spacing = jsonSpacing.toDouble() * context.pixelSizeConversionFactor();
2085 spacingProp = QgsProperty::fromValue( spacing );
2086 break;
2087 }
2088
2089 case QMetaType::Type::QVariantMap:
2090 {
2091 spacingProp = parseInterpolateByZoom( jsonSpacing.toMap(), context, context.pixelSizeConversionFactor(), &spacing );
2092 break;
2093 }
2094
2095 case QMetaType::Type::QVariantList:
2096 case QMetaType::Type::QStringList:
2097 {
2098 spacingProp = parseValueList( jsonSpacing.toList(), PropertyType::Numeric, context, context.pixelSizeConversionFactor(), 255, nullptr, &spacing );
2099 break;
2100 }
2101
2102 default:
2103 context.pushWarning( QObject::tr( "%1: Skipping unsupported symbol-spacing type (%2)" ).arg( context.layerId(), jsonSpacing.metaType().name() ) );
2104 break;
2105 }
2106
2107 spacingProp.setActive( true );
2109 }
2110#endif
2111
2112 if ( textSize >= 0 )
2113 {
2114 // TODO -- this probably needs revisiting -- it was copied from the MapTiler code, but may be wrong...
2115 labelSettings.priority = std::min( textSize / ( context.pixelSizeConversionFactor() * 3 ), 10.0 );
2116 }
2117
2118 labelSettings.setFormat( format );
2119
2120 // use a low obstacle weight for layers by default -- we'd rather have more labels for these layers, even if placement isn't ideal
2121 labelSettings.obstacleSettings().setFactor( 0.1 );
2122
2123 labelSettings.setDataDefinedProperties( ddLabelProperties );
2124
2125 labelingStyle.setGeometryType( geometryType );
2126 labelingStyle.setLabelSettings( labelSettings );
2127
2128 hasLabeling = true;
2129
2130 hasRenderer = parseSymbolLayerAsRenderer( jsonLayer, renderer, context );
2131}
2132
2134{
2135 if ( !jsonLayer.contains( u"layout"_s ) )
2136 {
2137 context.pushWarning( QObject::tr( "%1: Style layer has no layout property, skipping" ).arg( context.layerId() ) );
2138 return false;
2139 }
2140 const QVariantMap jsonLayout = jsonLayer.value( u"layout"_s ).toMap();
2141
2142 if ( jsonLayout.value( u"symbol-placement"_s ).toString() == "line"_L1 && !jsonLayout.contains( u"text-field"_s ) )
2143 {
2144 QgsPropertyCollection ddProperties;
2145
2146 double spacing = -1.0;
2147 if ( jsonLayout.contains( u"symbol-spacing"_s ) )
2148 {
2149 const QVariant jsonSpacing = jsonLayout.value( u"symbol-spacing"_s );
2150 switch ( jsonSpacing.userType() )
2151 {
2152 case QMetaType::Type::Int:
2153 case QMetaType::Type::LongLong:
2154 case QMetaType::Type::Double:
2155 spacing = jsonSpacing.toDouble() * context.pixelSizeConversionFactor();
2156 break;
2157
2158 case QMetaType::Type::QVariantMap:
2159 ddProperties.setProperty( QgsSymbolLayer::Property::Interval, parseInterpolateByZoom( jsonSpacing.toMap(), context, context.pixelSizeConversionFactor(), &spacing ) );
2160 break;
2161
2162 case QMetaType::Type::QVariantList:
2163 case QMetaType::Type::QStringList:
2164 ddProperties.setProperty( QgsSymbolLayer::Property::Interval, parseValueList( jsonSpacing.toList(), PropertyType::Numeric, context, context.pixelSizeConversionFactor(), 255, nullptr, &spacing ) );
2165 break;
2166
2167 default:
2168 context.pushWarning( QObject::tr( "%1: Skipping unsupported symbol-spacing type (%2)" ).arg( context.layerId(), jsonSpacing.metaType().name() ) );
2169 break;
2170 }
2171 }
2172 else
2173 {
2174 // defaults to 250
2175 spacing = 250 * context.pixelSizeConversionFactor();
2176 }
2177
2178 bool rotateMarkers = true;
2179 if ( jsonLayout.contains( u"icon-rotation-alignment"_s ) )
2180 {
2181 const QString alignment = jsonLayout.value( u"icon-rotation-alignment"_s ).toString();
2182 if ( alignment == "map"_L1 || alignment == "auto"_L1 )
2183 {
2184 rotateMarkers = true;
2185 }
2186 else if ( alignment == "viewport"_L1 )
2187 {
2188 rotateMarkers = false;
2189 }
2190 }
2191
2192 QgsPropertyCollection markerDdProperties;
2193 double rotation = 0.0;
2194 if ( jsonLayout.contains( u"icon-rotate"_s ) )
2195 {
2196 const QVariant jsonIconRotate = jsonLayout.value( u"icon-rotate"_s );
2197 switch ( jsonIconRotate.userType() )
2198 {
2199 case QMetaType::Type::Int:
2200 case QMetaType::Type::LongLong:
2201 case QMetaType::Type::Double:
2202 rotation = jsonIconRotate.toDouble();
2203 break;
2204
2205 case QMetaType::Type::QVariantMap:
2206 markerDdProperties.setProperty( QgsSymbolLayer::Property::Angle, parseInterpolateByZoom( jsonIconRotate.toMap(), context, context.pixelSizeConversionFactor(), &rotation ) );
2207 break;
2208
2209 case QMetaType::Type::QVariantList:
2210 case QMetaType::Type::QStringList:
2211 markerDdProperties.setProperty( QgsSymbolLayer::Property::Angle, parseValueList( jsonIconRotate.toList(), PropertyType::Numeric, context, context.pixelSizeConversionFactor(), 255, nullptr, &rotation ) );
2212 break;
2213
2214 default:
2215 context.pushWarning( QObject::tr( "%1: Skipping unsupported icon-rotate type (%2)" ).arg( context.layerId(), jsonIconRotate.metaType().name() ) );
2216 break;
2217 }
2218 }
2219
2220 QgsMarkerLineSymbolLayer *lineSymbol = new QgsMarkerLineSymbolLayer( rotateMarkers, spacing > 0 ? spacing : 1 );
2221 lineSymbol->setOutputUnit( context.targetUnit() );
2222 lineSymbol->setDataDefinedProperties( ddProperties );
2223 if ( spacing < 1 )
2224 {
2225 // if spacing isn't specified, it's a central point marker only
2227 }
2228
2229 const QVariantMap jsonPaint = jsonLayer.value( u"paint"_s ).toMap();
2230
2232 QSize spriteSize;
2233 QString spriteProperty, spriteSizeProperty;
2234 const QString sprite = retrieveSpriteAsBase64WithProperties( jsonLayout.value( u"icon-image"_s ), context, spriteSize, spriteProperty, spriteSizeProperty );
2235 if ( !sprite.isNull() )
2236 {
2237 markerLayer->setPath( sprite );
2238 markerLayer->setSize( spriteSize.width() );
2239 markerLayer->setSizeUnit( context.targetUnit() );
2240
2241 if ( !spriteProperty.isEmpty() )
2242 {
2243 markerDdProperties.setProperty( QgsSymbolLayer::Property::Name, QgsProperty::fromExpression( spriteProperty ) );
2244 markerDdProperties.setProperty( QgsSymbolLayer::Property::Width, QgsProperty::fromExpression( spriteSizeProperty ) );
2245 }
2246 }
2247
2248 if ( jsonLayout.contains( u"icon-size"_s ) )
2249 {
2250 const QVariant jsonIconSize = jsonLayout.value( u"icon-size"_s );
2251 double size = 1.0;
2252 QgsProperty property;
2253 switch ( jsonIconSize.userType() )
2254 {
2255 case QMetaType::Type::Int:
2256 case QMetaType::Type::LongLong:
2257 case QMetaType::Type::Double:
2258 {
2259 size = jsonIconSize.toDouble();
2260 if ( !spriteSizeProperty.isEmpty() )
2261 {
2262 markerDdProperties.setProperty( QgsSymbolLayer::Property::Width, QgsProperty::fromExpression( u"with_variable('marker_size',%1,%2*@marker_size)"_s.arg( spriteSizeProperty ).arg( size ) ) );
2263 }
2264 break;
2265 }
2266
2267 case QMetaType::Type::QVariantMap:
2268 property = parseInterpolateByZoom( jsonIconSize.toMap(), context, 1, &size );
2269 break;
2270
2271 case QMetaType::Type::QVariantList:
2272 case QMetaType::Type::QStringList:
2273 property = parseValueList( jsonIconSize.toList(), PropertyType::Numeric, context );
2274 break;
2275 default:
2276 context.pushWarning( QObject::tr( "%1: Skipping non-implemented icon-size type (%2)" ).arg( context.layerId(), jsonIconSize.metaType().name() ) );
2277 break;
2278 }
2279 markerLayer->setSize( size * spriteSize.width() );
2280 if ( !property.expressionString().isEmpty() )
2281 {
2282 if ( !spriteSizeProperty.isEmpty() )
2283 {
2284 markerDdProperties
2285 .setProperty( QgsSymbolLayer::Property::Width, QgsProperty::fromExpression( u"with_variable('marker_size',%1,(%2)*@marker_size)"_s.arg( spriteSizeProperty ).arg( property.expressionString() ) ) );
2286 }
2287 else
2288 {
2289 markerDdProperties.setProperty( QgsSymbolLayer::Property::Width, QgsProperty::fromExpression( u"(%2)*%1"_s.arg( spriteSize.width() ).arg( property.expressionString() ) ) );
2290 }
2291 }
2292 }
2293
2294 double iconOpacity = -1.0;
2295 if ( jsonPaint.contains( u"icon-opacity"_s ) )
2296 {
2297 const QVariant jsonIconOpacity = jsonPaint.value( u"icon-opacity"_s );
2298 switch ( jsonIconOpacity.userType() )
2299 {
2300 case QMetaType::Type::Int:
2301 case QMetaType::Type::LongLong:
2302 case QMetaType::Type::Double:
2303 iconOpacity = jsonIconOpacity.toDouble();
2304 break;
2305
2306 case QMetaType::Type::QVariantMap:
2307 markerDdProperties.setProperty( QgsSymbolLayer::Property::Opacity, parseInterpolateByZoom( jsonIconOpacity.toMap(), context, 100, &iconOpacity ) );
2308 break;
2309
2310 case QMetaType::Type::QVariantList:
2311 case QMetaType::Type::QStringList:
2312 markerDdProperties.setProperty( QgsSymbolLayer::Property::Opacity, parseValueList( jsonIconOpacity.toList(), PropertyType::Numeric, context, 100, 255, nullptr, &iconOpacity ) );
2313 break;
2314
2315 default:
2316 context.pushWarning( QObject::tr( "%1: Skipping unsupported icon-opacity type (%2)" ).arg( context.layerId(), jsonIconOpacity.metaType().name() ) );
2317 break;
2318 }
2319 }
2320
2321 markerLayer->setDataDefinedProperties( markerDdProperties );
2322 markerLayer->setAngle( rotation );
2323 if ( iconOpacity >= 0 )
2324 {
2325 markerLayer->setOpacity( iconOpacity );
2326 }
2327
2328 lineSymbol->setSubSymbol( new QgsMarkerSymbol( QgsSymbolLayerList() << markerLayer ) );
2329
2330 std::unique_ptr< QgsSymbol > symbol = std::make_unique< QgsLineSymbol >( QgsSymbolLayerList() << lineSymbol );
2331
2332 // set render units
2333 symbol->setOutputUnit( context.targetUnit() );
2334 lineSymbol->setOutputUnit( context.targetUnit() );
2335
2337 rendererStyle.setSymbol( symbol.release() );
2338 return true;
2339 }
2340 else if ( jsonLayout.contains( u"icon-image"_s ) )
2341 {
2342 const QVariantMap jsonPaint = jsonLayer.value( u"paint"_s ).toMap();
2343
2344 QSize spriteSize;
2345 QString spriteProperty, spriteSizeProperty;
2346 const QString sprite = retrieveSpriteAsBase64WithProperties( jsonLayout.value( u"icon-image"_s ), context, spriteSize, spriteProperty, spriteSizeProperty );
2347 if ( !sprite.isEmpty() || !spriteProperty.isEmpty() )
2348 {
2350 rasterMarker->setPath( sprite );
2351 rasterMarker->setSize( spriteSize.width() );
2352 rasterMarker->setSizeUnit( context.targetUnit() );
2353
2354 QgsPropertyCollection markerDdProperties;
2355 if ( !spriteProperty.isEmpty() )
2356 {
2357 markerDdProperties.setProperty( QgsSymbolLayer::Property::Name, QgsProperty::fromExpression( spriteProperty ) );
2358 markerDdProperties.setProperty( QgsSymbolLayer::Property::Width, QgsProperty::fromExpression( spriteSizeProperty ) );
2359 }
2360
2361 if ( jsonLayout.contains( u"icon-size"_s ) )
2362 {
2363 const QVariant jsonIconSize = jsonLayout.value( u"icon-size"_s );
2364 double size = 1.0;
2365 QgsProperty property;
2366 switch ( jsonIconSize.userType() )
2367 {
2368 case QMetaType::Type::Int:
2369 case QMetaType::Type::LongLong:
2370 case QMetaType::Type::Double:
2371 {
2372 size = jsonIconSize.toDouble();
2373 if ( !spriteSizeProperty.isEmpty() )
2374 {
2375 markerDdProperties.setProperty( QgsSymbolLayer::Property::Width, QgsProperty::fromExpression( u"with_variable('marker_size',%1,%2*@marker_size)"_s.arg( spriteSizeProperty ).arg( size ) ) );
2376 }
2377 break;
2378 }
2379
2380 case QMetaType::Type::QVariantMap:
2381 property = parseInterpolateByZoom( jsonIconSize.toMap(), context, 1, &size );
2382 break;
2383
2384 case QMetaType::Type::QVariantList:
2385 case QMetaType::Type::QStringList:
2386 property = parseValueList( jsonIconSize.toList(), PropertyType::Numeric, context );
2387 break;
2388 default:
2389 context.pushWarning( QObject::tr( "%1: Skipping non-implemented icon-size type (%2)" ).arg( context.layerId(), jsonIconSize.metaType().name() ) );
2390 break;
2391 }
2392 rasterMarker->setSize( size * spriteSize.width() );
2393 if ( !property.expressionString().isEmpty() )
2394 {
2395 if ( !spriteSizeProperty.isEmpty() )
2396 {
2397 markerDdProperties
2398 .setProperty( QgsSymbolLayer::Property::Width, QgsProperty::fromExpression( u"with_variable('marker_size',%1,(%2)*@marker_size)"_s.arg( spriteSizeProperty ).arg( property.expressionString() ) ) );
2399 }
2400 else
2401 {
2402 markerDdProperties.setProperty( QgsSymbolLayer::Property::Width, QgsProperty::fromExpression( u"(%2)*%1"_s.arg( spriteSize.width() ).arg( property.expressionString() ) ) );
2403 }
2404 }
2405 }
2406
2407 double rotation = 0.0;
2408 if ( jsonLayout.contains( u"icon-rotate"_s ) )
2409 {
2410 const QVariant jsonIconRotate = jsonLayout.value( u"icon-rotate"_s );
2411 switch ( jsonIconRotate.userType() )
2412 {
2413 case QMetaType::Type::Int:
2414 case QMetaType::Type::LongLong:
2415 case QMetaType::Type::Double:
2416 rotation = jsonIconRotate.toDouble();
2417 break;
2418
2419 case QMetaType::Type::QVariantMap:
2420 markerDdProperties.setProperty( QgsSymbolLayer::Property::Angle, parseInterpolateByZoom( jsonIconRotate.toMap(), context, context.pixelSizeConversionFactor(), &rotation ) );
2421 break;
2422
2423 case QMetaType::Type::QVariantList:
2424 case QMetaType::Type::QStringList:
2425 markerDdProperties.setProperty( QgsSymbolLayer::Property::Angle, parseValueList( jsonIconRotate.toList(), PropertyType::Numeric, context, context.pixelSizeConversionFactor(), 255, nullptr, &rotation ) );
2426 break;
2427
2428 default:
2429 context.pushWarning( QObject::tr( "%1: Skipping unsupported icon-rotate type (%2)" ).arg( context.layerId(), jsonIconRotate.metaType().name() ) );
2430 break;
2431 }
2432 }
2433
2434 double iconOpacity = -1.0;
2435 if ( jsonPaint.contains( u"icon-opacity"_s ) )
2436 {
2437 const QVariant jsonIconOpacity = jsonPaint.value( u"icon-opacity"_s );
2438 switch ( jsonIconOpacity.userType() )
2439 {
2440 case QMetaType::Type::Int:
2441 case QMetaType::Type::LongLong:
2442 case QMetaType::Type::Double:
2443 iconOpacity = jsonIconOpacity.toDouble();
2444 break;
2445
2446 case QMetaType::Type::QVariantMap:
2447 markerDdProperties.setProperty( QgsSymbolLayer::Property::Opacity, parseInterpolateByZoom( jsonIconOpacity.toMap(), context, 100, &iconOpacity ) );
2448 break;
2449
2450 case QMetaType::Type::QVariantList:
2451 case QMetaType::Type::QStringList:
2452 markerDdProperties.setProperty( QgsSymbolLayer::Property::Opacity, parseValueList( jsonIconOpacity.toList(), PropertyType::Numeric, context, 100, 255, nullptr, &iconOpacity ) );
2453 break;
2454
2455 default:
2456 context.pushWarning( QObject::tr( "%1: Skipping unsupported icon-opacity type (%2)" ).arg( context.layerId(), jsonIconOpacity.metaType().name() ) );
2457 break;
2458 }
2459 }
2460
2461 rasterMarker->setDataDefinedProperties( markerDdProperties );
2462 rasterMarker->setAngle( rotation );
2463 if ( iconOpacity >= 0 )
2464 rasterMarker->setOpacity( iconOpacity );
2465
2466 QgsMarkerSymbol *markerSymbol = new QgsMarkerSymbol( QgsSymbolLayerList() << rasterMarker );
2467 rendererStyle.setSymbol( markerSymbol );
2469 return true;
2470 }
2471 }
2472
2473 return false;
2474}
2475
2477{
2478 const double base = json.value( u"base"_s, u"1"_s ).toDouble();
2479 const QVariantList stops = json.value( u"stops"_s ).toList();
2480 if ( stops.empty() )
2481 return QgsProperty();
2482
2483 QString caseString = u"CASE "_s;
2484 const QString colorComponent( "color_part(%1,'%2')" );
2485
2486 for ( int i = 0; i < stops.length() - 1; ++i )
2487 {
2488 // step bottom zoom
2489 const QString bz = stops.at( i ).toList().value( 0 ).toString();
2490 // step top zoom
2491 const QString tz = stops.at( i + 1 ).toList().value( 0 ).toString();
2492
2493 const QVariant bcVariant = stops.at( i ).toList().value( 1 );
2494 const QVariant tcVariant = stops.at( i + 1 ).toList().value( 1 );
2495
2496 const QColor bottomColor = parseColor( bcVariant.toString(), context );
2497 const QColor topColor = parseColor( tcVariant.toString(), context );
2498
2499 if ( i == 0 && bottomColor.isValid() )
2500 {
2501 int bcHue;
2502 int bcSat;
2503 int bcLight;
2504 int bcAlpha;
2505 colorAsHslaComponents( bottomColor, bcHue, bcSat, bcLight, bcAlpha );
2506 caseString += u"WHEN @vector_tile_zoom < %1 THEN color_hsla(%2, %3, %4, %5) "_s.arg( bz ).arg( bcHue ).arg( bcSat ).arg( bcLight ).arg( bcAlpha );
2507 }
2508
2509 if ( bottomColor.isValid() && topColor.isValid() )
2510 {
2511 int bcHue;
2512 int bcSat;
2513 int bcLight;
2514 int bcAlpha;
2515 colorAsHslaComponents( bottomColor, bcHue, bcSat, bcLight, bcAlpha );
2516 int tcHue;
2517 int tcSat;
2518 int tcLight;
2519 int tcAlpha;
2520 colorAsHslaComponents( topColor, tcHue, tcSat, tcLight, tcAlpha );
2521 caseString += QStringLiteral(
2522 "WHEN @vector_tile_zoom >= %1 AND @vector_tile_zoom < %2 THEN color_hsla("
2523 "%3, %4, %5, %6) "
2524 )
2525 .arg(
2526 bz,
2527 tz,
2529 bz.toDouble(),
2530 tz.toDouble(),
2531 bcHue,
2532 tcHue,
2533 base,
2534 1,
2535 json.value( u"x1"_s ).toDouble(),
2536 json.value( u"y1"_s ).toDouble(),
2537 json.value( u"x2"_s ).toDouble(),
2538 json.value( u"y2"_s ).toDouble(),
2539 type,
2540 &context
2541 ),
2543 bz.toDouble(),
2544 tz.toDouble(),
2545 bcSat,
2546 tcSat,
2547 base,
2548 1,
2549 json.value( u"x1"_s ).toDouble(),
2550 json.value( u"y1"_s ).toDouble(),
2551 json.value( u"x2"_s ).toDouble(),
2552 json.value( u"y2"_s ).toDouble(),
2553 type,
2554 &context
2555 ),
2557 bz.toDouble(),
2558 tz.toDouble(),
2559 bcLight,
2560 tcLight,
2561 base,
2562 1,
2563 json.value( u"x1"_s ).toDouble(),
2564 json.value( u"y1"_s ).toDouble(),
2565 json.value( u"x2"_s ).toDouble(),
2566 json.value( u"y2"_s ).toDouble(),
2567 type,
2568 &context
2569 ),
2571 bz.toDouble(),
2572 tz.toDouble(),
2573 bcAlpha,
2574 tcAlpha,
2575 base,
2576 1,
2577 json.value( u"x1"_s ).toDouble(),
2578 json.value( u"y1"_s ).toDouble(),
2579 json.value( u"x2"_s ).toDouble(),
2580 json.value( u"y2"_s ).toDouble(),
2581 type,
2582 &context
2583 )
2584 );
2585 }
2586 else
2587 {
2588 const QString bottomColorExpr = parseColorExpression( bcVariant, context );
2589 const QString topColorExpr = parseColorExpression( tcVariant, context );
2590
2591 caseString += QStringLiteral(
2592 "WHEN @vector_tile_zoom >= %1 AND @vector_tile_zoom < %2 THEN color_hsla("
2593 "%3, %4, %5, %6) "
2594 )
2595 .arg(
2596 bz,
2597 tz,
2599 bz.toDouble(),
2600 tz.toDouble(),
2601 colorComponent.arg( bottomColorExpr ).arg( "hsl_hue" ),
2602 colorComponent.arg( topColorExpr ).arg( "hsl_hue" ),
2603 base,
2604 1,
2605 json.value( u"x1"_s ).toDouble(),
2606 json.value( u"y1"_s ).toDouble(),
2607 json.value( u"x2"_s ).toDouble(),
2608 json.value( u"y2"_s ).toDouble(),
2609 type,
2610 &context
2611 ),
2613 bz.toDouble(),
2614 tz.toDouble(),
2615 colorComponent.arg( bottomColorExpr ).arg( "hsl_saturation" ),
2616 colorComponent.arg( topColorExpr ).arg( "hsl_saturation" ),
2617 base,
2618 1,
2619 json.value( u"x1"_s ).toDouble(),
2620 json.value( u"y1"_s ).toDouble(),
2621 json.value( u"x2"_s ).toDouble(),
2622 json.value( u"y2"_s ).toDouble(),
2623 type,
2624 &context
2625 ),
2627 bz.toDouble(),
2628 tz.toDouble(),
2629 colorComponent.arg( bottomColorExpr ).arg( "lightness" ),
2630 colorComponent.arg( topColorExpr ).arg( "lightness" ),
2631 base,
2632 1,
2633 json.value( u"x1"_s ).toDouble(),
2634 json.value( u"y1"_s ).toDouble(),
2635 json.value( u"x2"_s ).toDouble(),
2636 json.value( u"y2"_s ).toDouble(),
2637 type,
2638 &context
2639 ),
2641 bz.toDouble(),
2642 tz.toDouble(),
2643 colorComponent.arg( bottomColorExpr ).arg( "alpha" ),
2644 colorComponent.arg( topColorExpr ).arg( "alpha" ),
2645 base,
2646 1,
2647 json.value( u"x1"_s ).toDouble(),
2648 json.value( u"y1"_s ).toDouble(),
2649 json.value( u"x2"_s ).toDouble(),
2650 json.value( u"y2"_s ).toDouble(),
2651 type,
2652 &context
2653 )
2654 );
2655 }
2656 }
2657
2658 // top color
2659 const QString tz = stops.last().toList().value( 0 ).toString();
2660 const QVariant tcVariant = stops.last().toList().value( 1 );
2661 QColor topColor;
2662 if ( tcVariant.userType() == QMetaType::Type::QString )
2663 {
2664 topColor = parseColor( tcVariant, context );
2665 if ( topColor.isValid() )
2666 {
2667 int tcHue;
2668 int tcSat;
2669 int tcLight;
2670 int tcAlpha;
2671 colorAsHslaComponents( topColor, tcHue, tcSat, tcLight, tcAlpha );
2672 caseString += QStringLiteral(
2673 "WHEN @vector_tile_zoom >= %1 THEN color_hsla(%2, %3, %4, %5) "
2674 "ELSE color_hsla(%2, %3, %4, %5) END"
2675 )
2676 .arg( tz )
2677 .arg( tcHue )
2678 .arg( tcSat )
2679 .arg( tcLight )
2680 .arg( tcAlpha );
2681 }
2682 }
2683 else if ( tcVariant.userType() == QMetaType::QVariantList )
2684 {
2685 const QString topColorExpr = parseColorExpression( tcVariant, context );
2686
2687 caseString += QStringLiteral(
2688 "WHEN @vector_tile_zoom >= %1 THEN color_hsla(%2, %3, %4, %5) "
2689 "ELSE color_hsla(%2, %3, %4, %5) END"
2690 )
2691 .arg( tz )
2692 .arg( colorComponent.arg( topColorExpr ).arg( "hsl_hue" ) )
2693 .arg( colorComponent.arg( topColorExpr ).arg( "hsl_saturation" ) )
2694 .arg( colorComponent.arg( topColorExpr ).arg( "lightness" ) )
2695 .arg( colorComponent.arg( topColorExpr ).arg( "alpha" ) );
2696 }
2697
2698 if ( !stops.empty() && defaultColor )
2699 *defaultColor = parseColor( stops.value( 0 ).toList().value( 1 ).toString(), context );
2700
2701 return QgsProperty::fromExpression( caseString );
2702}
2703
2704QgsProperty QgsMapBoxGlStyleConverter::parseInterpolateByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, double multiplier, double *defaultNumber, InterpolationType type )
2705{
2706 const double base = json.value( u"base"_s, u"1"_s ).toDouble();
2707 const QVariantList stops = json.value( u"stops"_s ).toList();
2708 if ( stops.empty() )
2709 return QgsProperty();
2710
2711 QString scaleExpression;
2712 if ( stops.size() <= 2 )
2713 {
2714 scaleExpression = interpolateExpression(
2715 stops.value( 0 ).toList().value( 0 ).toDouble(), // zoomMin
2716 stops.last().toList().value( 0 ).toDouble(), // zoomMax
2717 stops.value( 0 ).toList().value( 1 ), // valueMin
2718 stops.last().toList().value( 1 ), // valueMax
2719 base,
2720 multiplier,
2721 json.value( u"x1"_s ).toDouble(),
2722 json.value( u"y1"_s ).toDouble(),
2723 json.value( u"x2"_s ).toDouble(),
2724 json.value( u"y2"_s ).toDouble(),
2725 type,
2726 &context
2727 );
2728 }
2729 else
2730 {
2731 scaleExpression
2732 = parseStops( base, stops, multiplier, context, type, json.value( u"x1"_s ).toDouble(), json.value( u"y1"_s ).toDouble(), json.value( u"x2"_s ).toDouble(), json.value( u"y2"_s ).toDouble() );
2733 }
2734
2735 if ( !stops.empty() && defaultNumber )
2736 *defaultNumber = stops.value( 0 ).toList().value( 1 ).toDouble() * multiplier;
2737
2738 return QgsProperty::fromExpression( scaleExpression );
2739}
2740
2742{
2744 if ( contextPtr )
2745 {
2746 context = *contextPtr;
2747 }
2748 const double base = json.value( u"base"_s, u"1"_s ).toDouble();
2749 const QVariantList stops = json.value( u"stops"_s ).toList();
2750 if ( stops.empty() )
2751 return QgsProperty();
2752
2753 QString scaleExpression;
2754 if ( stops.length() <= 2 )
2755 {
2756 const QVariant bv = stops.value( 0 ).toList().value( 1 );
2757 const QVariant tv = stops.last().toList().value( 1 );
2758 double bottom = 0.0;
2759 double top = 0.0;
2760 const bool numeric = numericArgumentsOnly( bv, tv, bottom, top );
2761 scaleExpression = u"set_color_part(@symbol_color, 'alpha', %1)"_s.arg( interpolateExpression(
2762 stops.value( 0 ).toList().value( 0 ).toDouble(),
2763 stops.last().toList().value( 0 ).toDouble(),
2764 numeric ? QString::number( bottom * maxOpacity ) : QString( "(%1) * %2" ).arg( parseValue( bv, context ) ).arg( maxOpacity ),
2765 numeric ? QString::number( top * maxOpacity ) : QString( "(%1) * %2" ).arg( parseValue( tv, context ) ).arg( maxOpacity ),
2766 base,
2767 1,
2768 json.value( u"x1"_s ).toDouble(),
2769 json.value( u"y1"_s ).toDouble(),
2770 json.value( u"x2"_s ).toDouble(),
2771 json.value( u"y2"_s ).toDouble(),
2772 type,
2773 &context
2774 ) );
2775 }
2776 else
2777 {
2778 scaleExpression
2779 = parseOpacityStops( base, stops, maxOpacity, context, type, json.value( u"x1"_s ).toDouble(), json.value( u"y1"_s ).toDouble(), json.value( u"x2"_s ).toDouble(), json.value( u"y2"_s ).toDouble() );
2780 }
2781 return QgsProperty::fromExpression( scaleExpression );
2782}
2783
2785 double base, const QVariantList &stops, int maxOpacity, QgsMapBoxGlStyleConversionContext &context, InterpolationType type, double x1, double y1, double x2, double y2
2786)
2787{
2788 QString caseString = u"CASE WHEN @vector_tile_zoom < %1 THEN set_color_part(@symbol_color, 'alpha', %2)"_s.arg( stops.value( 0 ).toList().value( 0 ).toString() )
2789 .arg( stops.value( 0 ).toList().value( 1 ).toDouble() * maxOpacity );
2790
2791 for ( int i = 0; i < stops.size() - 1; ++i )
2792 {
2793 const QVariant bv = stops.value( i ).toList().value( 1 );
2794 const QVariant tv = stops.value( i + 1 ).toList().value( 1 );
2795 double bottom = 0.0;
2796 double top = 0.0;
2797 const bool numeric = numericArgumentsOnly( bv, tv, bottom, top );
2798
2799 caseString += QStringLiteral(
2800 " WHEN @vector_tile_zoom >= %1 AND @vector_tile_zoom < %2 "
2801 "THEN set_color_part(@symbol_color, 'alpha', %3)"
2802 )
2803 .arg(
2804 stops.value( i ).toList().value( 0 ).toString(),
2805 stops.value( i + 1 ).toList().value( 0 ).toString(),
2807 stops.value( i ).toList().value( 0 ).toDouble(),
2808 stops.value( i + 1 ).toList().value( 0 ).toDouble(),
2809 numeric ? QString::number( bottom * maxOpacity ) : QString( "(%1) * %2" ).arg( parseValue( bv, context ) ).arg( maxOpacity ),
2810 numeric ? QString::number( top * maxOpacity ) : QString( "(%1) * %2" ).arg( parseValue( tv, context ) ).arg( maxOpacity ),
2811 base,
2812 1,
2813 x1,
2814 y1,
2815 x2,
2816 y2,
2817 type,
2818 &context
2819 )
2820 );
2821 }
2822
2823
2824 bool numeric = false;
2825 const QVariant vv = stops.last().toList().value( 1 );
2826 double dv = vv.toDouble( &numeric );
2827
2828 caseString += QStringLiteral(
2829 " WHEN @vector_tile_zoom >= %1 "
2830 "THEN set_color_part(@symbol_color, 'alpha', %2) END"
2831 )
2832 .arg( stops.last().toList().value( 0 ).toString(), numeric ? QString::number( dv * maxOpacity ) : QString( "(%1) * %2" ).arg( parseValue( vv, context ) ).arg( maxOpacity ) );
2833 return caseString;
2834}
2835
2836QgsProperty QgsMapBoxGlStyleConverter::parseInterpolatePointByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, double multiplier, QPointF *defaultPoint, InterpolationType type )
2837{
2838 const double base = json.value( u"base"_s, u"1"_s ).toDouble();
2839 const QVariantList stops = json.value( u"stops"_s ).toList();
2840 if ( stops.empty() )
2841 {
2842 return QgsProperty();
2843 }
2844
2845 QString scaleExpression;
2846 if ( stops.size() <= 2 )
2847 {
2848 const bool isLiteralRepresentation = stops.value( 0 ).toList().value( 1 ).toList().value( 0 ).toString() == "literal"_L1;
2849 scaleExpression = u"array(%1,%2)"_s.arg(
2851 stops.value( 0 ).toList().value( 0 ).toDouble(),
2852 stops.last().toList().value( 0 ).toDouble(),
2853 isLiteralRepresentation ? stops.value( 0 ).toList().value( 1 ).toList().value( 1 ).toList().value( 0 ) : stops.value( 0 ).toList().value( 1 ).toList().value( 0 ),
2854 isLiteralRepresentation ? stops.last().toList().value( 1 ).toList().value( 1 ).toList().value( 0 ) : stops.last().toList().value( 1 ).toList().value( 0 ),
2855 base,
2856 multiplier,
2857 json.value( u"x1"_s ).toDouble(),
2858 json.value( u"y1"_s ).toDouble(),
2859 json.value( u"x2"_s ).toDouble(),
2860 json.value( u"y2"_s ).toDouble(),
2861 type,
2862 &context
2863 ),
2865 stops.value( 0 ).toList().value( 0 ).toDouble(),
2866 stops.last().toList().value( 0 ).toDouble(),
2867 isLiteralRepresentation ? stops.value( 0 ).toList().value( 1 ).toList().value( 1 ).toList().value( 1 ) : stops.value( 0 ).toList().value( 1 ).toList().value( 1 ),
2868 isLiteralRepresentation ? stops.last().toList().value( 1 ).toList().value( 1 ).toList().value( 1 ) : stops.last().toList().value( 1 ).toList().value( 1 ),
2869 base,
2870 multiplier,
2871 json.value( u"x1"_s ).toDouble(),
2872 json.value( u"y1"_s ).toDouble(),
2873 json.value( u"x2"_s ).toDouble(),
2874 json.value( u"y2"_s ).toDouble(),
2875 type,
2876 &context
2877 )
2878 );
2879 }
2880 else
2881 {
2882 scaleExpression
2883 = parsePointStops( base, stops, context, multiplier, type, json.value( u"x1"_s ).toDouble(), json.value( u"y1"_s ).toDouble(), json.value( u"x2"_s ).toDouble(), json.value( u"y2"_s ).toDouble() );
2884 }
2885
2886 if ( !stops.empty() && defaultPoint )
2887 *defaultPoint = QPointF( stops.value( 0 ).toList().value( 1 ).toList().value( 0 ).toDouble() * multiplier, stops.value( 0 ).toList().value( 1 ).toList().value( 1 ).toDouble() * multiplier );
2888
2889 return QgsProperty::fromExpression( scaleExpression );
2890}
2891
2892QgsProperty QgsMapBoxGlStyleConverter::parseInterpolateStringByZoom( const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, const QVariantMap &conversionMap, QString *defaultString )
2893{
2894 const QVariantList stops = json.value( u"stops"_s ).toList();
2895 if ( stops.empty() )
2896 return QgsProperty();
2897
2898 const QString scaleExpression = parseStringStops( stops, context, conversionMap, defaultString );
2899
2900 return QgsProperty::fromExpression( scaleExpression );
2901}
2902
2904 double base, const QVariantList &stops, QgsMapBoxGlStyleConversionContext &context, double multiplier, InterpolationType type, double x1, double y1, double x2, double y2
2905)
2906{
2907 QString caseString = u"CASE "_s;
2908
2909 for ( int i = 0; i < stops.length() - 1; ++i )
2910 {
2911 // bottom zoom and value
2912 const QVariant bz = stops.value( i ).toList().value( 0 );
2913 const QVariant bv = stops.value( i ).toList().value( 1 );
2914 if ( bv.userType() != QMetaType::Type::QVariantList && bv.userType() != QMetaType::Type::QStringList )
2915 {
2916 context.pushWarning( QObject::tr( "%1: Skipping unsupported offset interpolation type (%2)." ).arg( context.layerId(), bz.metaType().name() ) );
2917 return QString();
2918 }
2919
2920 // top zoom and value
2921 const QVariant tz = stops.value( i + 1 ).toList().value( 0 );
2922 const QVariant tv = stops.value( i + 1 ).toList().value( 1 );
2923 if ( tv.userType() != QMetaType::Type::QVariantList && tv.userType() != QMetaType::Type::QStringList )
2924 {
2925 context.pushWarning( QObject::tr( "%1: Skipping unsupported offset interpolation type (%2)." ).arg( context.layerId(), tz.metaType().name() ) );
2926 return QString();
2927 }
2928
2929 const bool isLiteralRepresentation = bv.toList().value( 0 ).toString() == "literal"_L1;
2930 caseString += QStringLiteral(
2931 "WHEN @vector_tile_zoom > %1 AND @vector_tile_zoom <= %2 "
2932 "THEN array(%3,%4)"
2933 )
2934 .arg(
2935 bz.toString(),
2936 tz.toString(),
2938 bz.toDouble(),
2939 tz.toDouble(),
2940 isLiteralRepresentation ? bv.toList().value( 1 ).toList().value( 0 ) : bv.toList().value( 0 ),
2941 isLiteralRepresentation ? tv.toList().value( 1 ).toList().value( 0 ) : tv.toList().value( 0 ),
2942 base,
2943 multiplier,
2944 x1,
2945 y1,
2946 x2,
2947 y2,
2948 type,
2949 &context
2950 ),
2952 bz.toDouble(),
2953 tz.toDouble(),
2954 isLiteralRepresentation ? bv.toList().value( 1 ).toList().value( 1 ) : bv.toList().value( 1 ),
2955 isLiteralRepresentation ? tv.toList().value( 1 ).toList().value( 1 ) : tv.toList().value( 1 ),
2956 base,
2957 multiplier,
2958 x1,
2959 y1,
2960 x2,
2961 y2,
2962 type,
2963 &context
2964 )
2965 );
2966 }
2967 caseString += "END"_L1;
2968 return caseString;
2969}
2970
2971QString QgsMapBoxGlStyleConverter::parseArrayStops( const QVariantList &stops, QgsMapBoxGlStyleConversionContext &, double multiplier )
2972{
2973 if ( stops.length() < 2 )
2974 return QString();
2975
2976 QString caseString = u"CASE"_s;
2977
2978 for ( int i = 0; i < stops.length(); ++i )
2979 {
2980 caseString += " WHEN "_L1;
2981 QStringList conditions;
2982 if ( i > 0 )
2983 {
2984 const QVariant bottomZoom = stops.value( i ).toList().value( 0 );
2985 conditions << u"@vector_tile_zoom > %1"_s.arg( bottomZoom.toString() );
2986 }
2987 if ( i < stops.length() - 1 )
2988 {
2989 const QVariant topZoom = stops.value( i + 1 ).toList().value( 0 );
2990 conditions << u"@vector_tile_zoom <= %1"_s.arg( topZoom.toString() );
2991 }
2992
2993 const QVariantList values = stops.value( i ).toList().value( 1 ).toList();
2994 QStringList valuesFixed;
2995 bool ok = false;
2996 for ( const QVariant &value : values )
2997 {
2998 const double number = value.toDouble( &ok );
2999 if ( ok )
3000 valuesFixed << QString::number( number * multiplier );
3001 }
3002
3003 // top zoom and value
3004 caseString += u"%1 THEN array(%3)"_s.arg( conditions.join( " AND "_L1 ), valuesFixed.join( ',' ) );
3005 }
3006 caseString += " END"_L1;
3007 return caseString;
3008}
3009
3011 double base, const QVariantList &stops, double multiplier, QgsMapBoxGlStyleConversionContext &context, InterpolationType type, double x1, double y1, double x2, double y2
3012)
3013{
3014 QString caseString = u"CASE "_s;
3015
3016 for ( int i = 0; i < stops.length() - 1; ++i )
3017 {
3018 // bottom zoom and value
3019 const QVariant bz = stops.value( i ).toList().value( 0 );
3020 const QVariant bv = stops.value( i ).toList().value( 1 );
3021 if ( bz.userType() == QMetaType::Type::QVariantList || bz.userType() == QMetaType::Type::QStringList )
3022 {
3023 context.pushWarning( QObject::tr( "%1: Expressions in interpolation function are not supported, skipping." ).arg( context.layerId() ) );
3024 return QString();
3025 }
3026
3027 // top zoom and value
3028 const QVariant tz = stops.value( i + 1 ).toList().value( 0 );
3029 const QVariant tv = stops.value( i + 1 ).toList().value( 1 );
3030 if ( tz.userType() == QMetaType::Type::QVariantList || tz.userType() == QMetaType::Type::QStringList )
3031 {
3032 context.pushWarning( QObject::tr( "%1: Expressions in interpolation function are not supported, skipping." ).arg( context.layerId() ) );
3033 return QString();
3034 }
3035
3036 const QString lowerComparator = i == 0 ? u">="_s : u">"_s;
3037
3038 caseString += QStringLiteral(
3039 "WHEN @vector_tile_zoom %1 %2 AND @vector_tile_zoom <= %3 "
3040 "THEN %4 "
3041 )
3042 .arg( lowerComparator, bz.toString(), tz.toString(), interpolateExpression( bz.toDouble(), tz.toDouble(), bv, tv, base, multiplier, x1, y1, x2, y2, type, &context ) );
3043 }
3044
3045 const QVariant z = stops.last().toList().value( 0 );
3046 const QVariant v = stops.last().toList().value( 1 );
3047 QString vStr = v.toString();
3048 if ( ( QMetaType::Type ) v.userType() == QMetaType::QVariantList )
3049 {
3050 vStr = parseExpression( v.toList(), context );
3051 caseString += QStringLiteral(
3052 "WHEN @vector_tile_zoom > %1 "
3053 "THEN ( ( %2 ) * %3 ) END"
3054 )
3055 .arg( z.toString() )
3056 .arg( vStr )
3057 .arg( multiplier );
3058 }
3059 else
3060 {
3061 caseString += QStringLiteral(
3062 "WHEN @vector_tile_zoom > %1 "
3063 "THEN %2 END"
3064 )
3065 .arg( z.toString() )
3066 .arg( v.toDouble() * multiplier );
3067 }
3068
3069 return caseString;
3070}
3071
3072QString QgsMapBoxGlStyleConverter::parseStringStops( const QVariantList &stops, QgsMapBoxGlStyleConversionContext &context, const QVariantMap &conversionMap, QString *defaultString )
3073{
3074 QString caseString = u"CASE "_s;
3075
3076 for ( int i = 0; i < stops.length() - 1; ++i )
3077 {
3078 // bottom zoom and value
3079 const QVariant bz = stops.value( i ).toList().value( 0 );
3080 const QString bv = stops.value( i ).toList().value( 1 ).toString();
3081 if ( bz.userType() == QMetaType::Type::QVariantList || bz.userType() == QMetaType::Type::QStringList )
3082 {
3083 context.pushWarning( QObject::tr( "%1: Expressions in interpolation function are not supported, skipping." ).arg( context.layerId() ) );
3084 return QString();
3085 }
3086
3087 // top zoom
3088 const QVariant tz = stops.value( i + 1 ).toList().value( 0 );
3089 if ( tz.userType() == QMetaType::Type::QVariantList || tz.userType() == QMetaType::Type::QStringList )
3090 {
3091 context.pushWarning( QObject::tr( "%1: Expressions in interpolation function are not supported, skipping." ).arg( context.layerId() ) );
3092 return QString();
3093 }
3094
3095 caseString += QStringLiteral(
3096 "WHEN @vector_tile_zoom > %1 AND @vector_tile_zoom <= %2 "
3097 "THEN %3 "
3098 )
3099 .arg( bz.toString(), tz.toString(), QgsExpression::quotedValue( conversionMap.value( bv, bv ) ) );
3100 }
3101 caseString += u"ELSE %1 END"_s.arg( QgsExpression::quotedValue( conversionMap.value( stops.constLast().toList().value( 1 ).toString(), stops.constLast().toList().value( 1 ) ) ) );
3102 if ( defaultString )
3103 *defaultString = stops.constLast().toList().value( 1 ).toString();
3104 return caseString;
3105}
3106
3108{
3109 QString caseString = u"CASE "_s;
3110
3111 bool isExpression = false;
3112 for ( int i = 0; i < stops.length() - 1; ++i )
3113 {
3114 // bottom zoom and value
3115 const QVariant bz = stops.value( i ).toList().value( 0 );
3116 if ( bz.userType() == QMetaType::Type::QVariantList || bz.userType() == QMetaType::Type::QStringList )
3117 {
3118 context.pushWarning( QObject::tr( "%1: Lists in label interpolation function are not supported, skipping." ).arg( context.layerId() ) );
3119 return QString();
3120 }
3121
3122 // top zoom
3123 const QVariant tz = stops.value( i + 1 ).toList().value( 0 );
3124 if ( tz.userType() == QMetaType::Type::QVariantList || tz.userType() == QMetaType::Type::QStringList )
3125 {
3126 context.pushWarning( QObject::tr( "%1: Lists in label interpolation function are not supported, skipping." ).arg( context.layerId() ) );
3127 return QString();
3128 }
3129
3130 QString fieldPart = processLabelField( stops.constLast().toList().value( 1 ).toString(), isExpression );
3131 if ( fieldPart.isEmpty() )
3132 fieldPart = u"''"_s;
3133 else if ( !isExpression )
3134 fieldPart = QgsExpression::quotedColumnRef( fieldPart );
3135
3136 caseString += QStringLiteral(
3137 "WHEN @vector_tile_zoom > %1 AND @vector_tile_zoom < %2 "
3138 "THEN %3 "
3139 )
3140 .arg( bz.toString(), tz.toString(), fieldPart );
3141 }
3142
3143 {
3144 const QVariant bz = stops.constLast().toList().value( 0 );
3145 if ( bz.userType() == QMetaType::Type::QVariantList || bz.userType() == QMetaType::Type::QStringList )
3146 {
3147 context.pushWarning( QObject::tr( "%1: Lists in label interpolation function are not supported, skipping." ).arg( context.layerId() ) );
3148 return QString();
3149 }
3150
3151 QString fieldPart = processLabelField( stops.constLast().toList().value( 1 ).toString(), isExpression );
3152 if ( fieldPart.isEmpty() )
3153 fieldPart = u"''"_s;
3154 else if ( !isExpression )
3155 fieldPart = QgsExpression::quotedColumnRef( fieldPart );
3156
3157 caseString += QStringLiteral(
3158 "WHEN @vector_tile_zoom >= %1 "
3159 "THEN %3 "
3160 )
3161 .arg( bz.toString(), fieldPart );
3162 }
3163
3164 QString defaultPart = processLabelField( stops.constFirst().toList().value( 1 ).toString(), isExpression );
3165 if ( defaultPart.isEmpty() )
3166 defaultPart = u"''"_s;
3167 else if ( !isExpression )
3168 defaultPart = QgsExpression::quotedColumnRef( defaultPart );
3169 caseString += u"ELSE %1 END"_s.arg( defaultPart );
3170
3171 return caseString;
3172}
3173
3175 const QVariantList &json, QgsMapBoxGlStyleConverter::PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier, int maxOpacity, QColor *defaultColor, double *defaultNumber
3176)
3177{
3178 const QString method = json.value( 0 ).toString();
3179 if ( method == "interpolate"_L1 )
3180 {
3181 return parseInterpolateListByZoom( json, type, context, multiplier, maxOpacity, defaultColor, defaultNumber );
3182 }
3183 else if ( method == "match"_L1 )
3184 {
3185 return parseMatchList( json, type, context, multiplier, maxOpacity, defaultColor, defaultNumber );
3186 }
3187 else if ( method == "step"_L1 )
3188 {
3189 return parseStepList( json, type, context, multiplier, maxOpacity, defaultColor, defaultNumber );
3190 }
3191 else
3192 {
3193 return QgsProperty::fromExpression( parseExpression( json, context, type == PropertyType::Color ) );
3194 }
3195}
3196
3198 const QVariantList &json, QgsMapBoxGlStyleConverter::PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier, int maxOpacity, QColor *defaultColor, double *defaultNumber
3199)
3200{
3201 const QString attribute = parseExpression( json.value( 1 ).toList(), context );
3202 if ( attribute.isEmpty() )
3203 {
3204 context.pushWarning( QObject::tr( "%1: Could not interpret match list" ).arg( context.layerId() ) );
3205 return QgsProperty();
3206 }
3207
3208 QString caseString = u"CASE "_s;
3209
3210 for ( int i = 2; i < json.length() - 1; i += 2 )
3211 {
3212 QVariantList keys;
3213 QVariant variantKeys = json.value( i );
3214 if ( variantKeys.userType() == QMetaType::Type::QVariantList || variantKeys.userType() == QMetaType::Type::QStringList )
3215 keys = variantKeys.toList();
3216 else
3217 keys = { variantKeys };
3218
3219 QStringList matchString;
3220 for ( const QVariant &key : keys )
3221 {
3222 matchString << QgsExpression::quotedValue( key );
3223 }
3224
3225 const QVariant value = json.value( i + 1 );
3226
3227 QString valueString;
3228 switch ( type )
3229 {
3231 {
3232 if ( value.userType() == QMetaType::Type::QVariantList || value.userType() == QMetaType::Type::QStringList )
3233 {
3234 valueString = parseMatchList( value.toList(), PropertyType::Color, context, multiplier, maxOpacity, defaultColor, defaultNumber ).asExpression();
3235 }
3236 else
3237 {
3238 const QColor color = parseColor( value, context );
3239 valueString = QgsExpression::quotedString( color.name() );
3240 }
3241 break;
3242 }
3243
3245 {
3246 const double v = value.toDouble() * multiplier;
3247 valueString = QString::number( v );
3248 break;
3249 }
3250
3252 {
3253 const double v = value.toDouble() * maxOpacity;
3254 valueString = QString::number( v );
3255 break;
3256 }
3257
3259 {
3260 valueString = u"array(%1,%2)"_s.arg( value.toList().value( 0 ).toDouble() * multiplier, value.toList().value( 0 ).toDouble() * multiplier );
3261 break;
3262 }
3263
3265 {
3266 if ( value.toList().count() == 2 && value.toList().first().toString() == "literal"_L1 )
3267 {
3268 valueString = u"array(%1)"_s.arg( value.toList().at( 1 ).toStringList().join( ',' ) );
3269 }
3270 else
3271 {
3272 valueString = u"array(%1)"_s.arg( value.toStringList().join( ',' ) );
3273 }
3274 break;
3275 }
3276
3278 {
3279 if ( value.toList().count() == 2 && value.toList().first().toString() == "literal"_L1 )
3280 {
3281 QStringList dashValues = value.toList().at( 1 ).toStringList();
3282 if ( dashValues.length() % 2 == 1 )
3283 {
3284 dashValues << u"0"_s;
3285 }
3286 valueString = u"array(%1)"_s.arg( dashValues.join( ',' ) );
3287 }
3288 else
3289 {
3290 QStringList dashValues = value.toStringList();
3291 if ( dashValues.length() % 2 == 1 )
3292 {
3293 dashValues << u"0"_s;
3294 }
3295 valueString = u"array(%1)"_s.arg( dashValues.join( ',' ) );
3296 }
3297 break;
3298 }
3299 }
3300
3301 if ( matchString.count() == 1 )
3302 {
3303 caseString += u"WHEN %1 IS %2 THEN %3 "_s.arg( attribute, matchString.at( 0 ), valueString );
3304 }
3305 else
3306 {
3307 caseString += u"WHEN %1 IN (%2) THEN %3 "_s.arg( attribute, matchString.join( ',' ), valueString );
3308 }
3309 }
3310
3311 QVariant lastValue = json.constLast();
3312 QString elseValue;
3313
3314 switch ( lastValue.userType() )
3315 {
3316 case QMetaType::Type::QVariantList:
3317 case QMetaType::Type::QStringList:
3318 elseValue = parseValueList( lastValue.toList(), type, context, multiplier, maxOpacity, defaultColor, defaultNumber ).asExpression();
3319 break;
3320
3321 default:
3322 {
3323 switch ( type )
3324 {
3326 {
3327 const QColor color = parseColor( lastValue, context );
3328 if ( defaultColor )
3329 *defaultColor = color;
3330
3331 elseValue = QgsExpression::quotedString( color.name() );
3332 break;
3333 }
3334
3336 {
3337 const double v = json.constLast().toDouble() * multiplier;
3338 if ( defaultNumber )
3339 *defaultNumber = v;
3340 elseValue = QString::number( v );
3341 break;
3342 }
3343
3345 {
3346 const double v = json.constLast().toDouble() * maxOpacity;
3347 if ( defaultNumber )
3348 *defaultNumber = v;
3349 elseValue = QString::number( v );
3350 break;
3351 }
3352
3354 {
3355 elseValue = u"array(%1,%2)"_s.arg( json.constLast().toList().value( 0 ).toDouble() * multiplier ).arg( json.constLast().toList().value( 0 ).toDouble() * multiplier );
3356 break;
3357 }
3358
3360 {
3361 if ( json.constLast().toList().count() == 2 && json.constLast().toList().first().toString() == "literal"_L1 )
3362 {
3363 elseValue = u"array(%1)"_s.arg( json.constLast().toList().at( 1 ).toStringList().join( ',' ) );
3364 }
3365 else
3366 {
3367 elseValue = u"array(%1)"_s.arg( json.constLast().toStringList().join( ',' ) );
3368 }
3369 break;
3370 }
3371
3373 {
3374 if ( json.constLast().toList().count() == 2 && json.constLast().toList().first().toString() == "literal"_L1 )
3375 {
3376 QStringList dashValues = json.constLast().toList().at( 1 ).toStringList();
3377 if ( dashValues.length() % 2 == 1 )
3378 {
3379 dashValues << u"0"_s;
3380 }
3381 elseValue = u"array(%1)"_s.arg( dashValues.join( ',' ) );
3382 }
3383 else
3384 {
3385 QStringList dashValues = json.constLast().toStringList();
3386 if ( dashValues.length() % 2 == 1 )
3387 {
3388 dashValues << u"0"_s;
3389 }
3390 elseValue = u"array(%1)"_s.arg( dashValues.join( ',' ) );
3391 }
3392 break;
3393 }
3394 }
3395 break;
3396 }
3397 }
3398
3399 caseString += u"ELSE %1 END"_s.arg( elseValue );
3400 return QgsProperty::fromExpression( caseString );
3401}
3402
3404 const QVariantList &json, PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier, int maxOpacity, QColor *defaultColor, double *defaultNumber
3405)
3406{
3407 const QString expression = parseExpression( json.value( 1 ).toList(), context );
3408 if ( expression.isEmpty() )
3409 {
3410 context.pushWarning( QObject::tr( "%1: Could not interpret step list" ).arg( context.layerId() ) );
3411 return QgsProperty();
3412 }
3413
3414 QString caseString = u"CASE "_s;
3415
3416
3417 for ( int i = json.length() - 2; i > 0; i -= 2 )
3418 {
3419 const QVariant stepValue = json.value( i + 1 );
3420
3421 QString valueString;
3422 if ( stepValue.canConvert<QVariantList>() && ( stepValue.toList().count() != 2 || type != PropertyType::Point ) && type != PropertyType::NumericArray && type != PropertyType::DashArray )
3423 {
3424 valueString = parseValueList( stepValue.toList(), type, context, multiplier, maxOpacity, defaultColor, defaultNumber ).expressionString();
3425 }
3426 else
3427 {
3428 switch ( type )
3429 {
3431 {
3432 const QColor color = parseColor( stepValue, context );
3433 valueString = QgsExpression::quotedString( color.name() );
3434 break;
3435 }
3436
3438 {
3439 const double v = stepValue.toDouble() * multiplier;
3440 valueString = QString::number( v );
3441 break;
3442 }
3443
3445 {
3446 const double v = stepValue.toDouble() * maxOpacity;
3447 valueString = QString::number( v );
3448 break;
3449 }
3450
3452 {
3453 valueString = u"array(%1,%2)"_s.arg( stepValue.toList().value( 0 ).toDouble() * multiplier ).arg( stepValue.toList().value( 0 ).toDouble() * multiplier );
3454 break;
3455 }
3456
3458 {
3459 if ( stepValue.toList().count() == 2 && stepValue.toList().first().toString() == "literal"_L1 )
3460 {
3461 valueString = u"array(%1)"_s.arg( stepValue.toList().at( 1 ).toStringList().join( ',' ) );
3462 }
3463 else
3464 {
3465 valueString = u"array(%1)"_s.arg( stepValue.toStringList().join( ',' ) );
3466 }
3467 break;
3468 }
3469
3471 {
3472 if ( stepValue.toList().count() == 2 && stepValue.toList().first().toString() == "literal"_L1 )
3473 {
3474 QStringList dashValues = stepValue.toList().at( 1 ).toStringList();
3475 if ( dashValues.length() % 2 == 1 )
3476 {
3477 dashValues << u"0"_s;
3478 }
3479 valueString = u"array(%1)"_s.arg( dashValues.join( ',' ) );
3480 }
3481 else
3482 {
3483 QStringList dashValues = stepValue.toStringList();
3484 if ( dashValues.length() % 2 == 1 )
3485 {
3486 dashValues << u"0"_s;
3487 }
3488 valueString = u"array(%1)"_s.arg( dashValues.join( ',' ) );
3489 }
3490 break;
3491 }
3492 }
3493 }
3494
3495 if ( i > 1 )
3496 {
3497 const QString stepKey = QgsExpression::quotedValue( json.value( i ) );
3498 caseString += u" WHEN %1 >= %2 THEN (%3) "_s.arg( expression, stepKey, valueString );
3499 }
3500 else
3501 {
3502 caseString += u"ELSE (%1) END"_s.arg( valueString );
3503 }
3504 }
3505 return QgsProperty::fromExpression( caseString );
3506}
3507
3509 const QVariantList &json, PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier, int maxOpacity, QColor *defaultColor, double *defaultNumber
3510)
3511{
3512 if ( json.value( 0 ).toString() != "interpolate"_L1 )
3513 {
3514 context.pushWarning( QObject::tr( "%1: Could not interpret value list" ).arg( context.layerId() ) );
3515 return QgsProperty();
3516 }
3517
3518 const QVariantList parts = json.value( 1 ).toList();
3519 const QString technique = parts.value( 0 ).toString();
3520 InterpolationType interpolationType = InterpolationType::Linear;
3521 QVariantMap props;
3522
3523 if ( technique == "linear"_L1 )
3524 {
3525 props.insert( u"base"_s, 1 );
3526 interpolationType = InterpolationType::Linear;
3527 }
3528 else if ( technique == "exponential"_L1 )
3529 {
3530 props.insert( u"base"_s, parts.value( 1 ).toDouble() );
3531 interpolationType = InterpolationType::Exponential;
3532 }
3533 else if ( technique == "cubic-bezier"_L1 )
3534 {
3535 interpolationType = InterpolationType::CubicBezier;
3536
3537 props.insert( u"x1"_s, parts.value( 1 ).toDouble() );
3538 props.insert( u"y1"_s, parts.value( 2 ).toDouble() );
3539 props.insert( u"x2"_s, parts.value( 3 ).toDouble() );
3540 props.insert( u"y2"_s, parts.value( 4 ).toDouble() );
3541 }
3542 else
3543 {
3544 context.pushWarning( QObject::tr( "%1: Skipping not implemented interpolation method %2" ).arg( context.layerId(), technique ) );
3545 return QgsProperty();
3546 }
3547
3548 if ( json.value( 2 ).toList().value( 0 ).toString() != "zoom"_L1 )
3549 {
3550 context.pushWarning( QObject::tr( "%1: Skipping not implemented interpolation input %2" ).arg( context.layerId(), json.value( 2 ).toString() ) );
3551 return QgsProperty();
3552 }
3553
3554 // Convert stops into list of lists
3555 QVariantList stops;
3556 for ( int i = 3; i < json.length(); i += 2 )
3557 {
3558 stops.push_back( QVariantList() << json.value( i ).toString() << json.value( i + 1 ) );
3559 }
3560
3561 props.insert( u"stops"_s, stops );
3562
3563 switch ( type )
3564 {
3566 return parseInterpolateColorByZoom( props, context, defaultColor, interpolationType );
3567
3569 return parseInterpolateByZoom( props, context, multiplier, defaultNumber, interpolationType );
3570
3572 return parseInterpolateOpacityByZoom( props, maxOpacity, &context, interpolationType );
3573
3575 return parseInterpolatePointByZoom( props, context, multiplier, nullptr, interpolationType );
3576
3579 context.pushWarning( QObject::tr( "%1: Skipping unsupported numeric array in interpolate" ).arg( context.layerId() ) );
3580 return QgsProperty();
3581 }
3582 return QgsProperty();
3583}
3584
3586{
3587 if ( ( QMetaType::Type ) colorExpression.userType() == QMetaType::QVariantList )
3588 {
3589 return parseExpression( colorExpression.toList(), context, true );
3590 }
3591 return parseValue( colorExpression, context, true );
3592}
3593
3595{
3596 if ( color.userType() != QMetaType::Type::QString )
3597 {
3598 context.pushWarning( QObject::tr( "%1: Could not parse non-string color %2, skipping" ).arg( context.layerId(), color.toString() ) );
3599 return QColor();
3600 }
3601
3602 return QgsSymbolLayerUtils::parseColor( color.toString() );
3603}
3604
3605void QgsMapBoxGlStyleConverter::colorAsHslaComponents( const QColor &color, int &hue, int &saturation, int &lightness, int &alpha )
3606{
3607 hue = std::max( 0, color.hslHue() );
3608 saturation = color.hslSaturation() / 255.0 * 100;
3609 lightness = color.lightness() / 255.0 * 100;
3610 alpha = color.alpha();
3611}
3612
3614 double zoomMin, double zoomMax, QVariant valueMin, QVariant valueMax, double base, double multiplier, double x1, double y1, double x2, double y2, InterpolationType type, QgsMapBoxGlStyleConversionContext *contextPtr
3615)
3616{
3618 if ( contextPtr )
3619 {
3620 context = *contextPtr;
3621 }
3622
3623 // special case where min = max !
3624 if ( valueMin.canConvert<double>() && valueMax.canConvert<double>() )
3625 {
3626 bool minDoubleOk = true;
3627 const double min = valueMin.toDouble( &minDoubleOk );
3628 bool maxDoubleOk = true;
3629 const double max = valueMax.toDouble( &maxDoubleOk );
3630 if ( minDoubleOk && maxDoubleOk && qgsDoubleNear( min, max ) )
3631 {
3632 return QString::number( min * multiplier );
3633 }
3634 }
3635
3636 QString minValueExpr = valueMin.toString();
3637 QString maxValueExpr = valueMax.toString();
3638 if ( valueMin.userType() == QMetaType::Type::QVariantList )
3639 {
3640 minValueExpr = parseExpression( valueMin.toList(), context );
3641 }
3642 if ( valueMax.userType() == QMetaType::Type::QVariantList )
3643 {
3644 maxValueExpr = parseExpression( valueMax.toList(), context );
3645 }
3646
3647 QString expression;
3648 if ( minValueExpr == maxValueExpr )
3649 {
3650 expression = minValueExpr;
3651 }
3652 else
3653 {
3654 switch ( type )
3655 {
3657 expression = u"scale_linear(@vector_tile_zoom,%1,%2,%3,%4)"_s.arg( zoomMin ).arg( zoomMax ).arg( minValueExpr ).arg( maxValueExpr );
3658 break;
3660 if ( base == 1 )
3661 {
3662 expression = u"scale_linear(@vector_tile_zoom,%1,%2,%3,%4)"_s.arg( zoomMin ).arg( zoomMax ).arg( minValueExpr ).arg( maxValueExpr );
3663 }
3664 else
3665 {
3666 expression = u"scale_exponential(@vector_tile_zoom,%1,%2,%3,%4,%5)"_s.arg( zoomMin ).arg( zoomMax ).arg( minValueExpr ).arg( maxValueExpr ).arg( base );
3667 }
3668 break;
3669
3671 expression = u"scale_cubic_bezier(@vector_tile_zoom,%1,%2,%3,%4,%5,%6,%7,%8)"_s.arg( zoomMin ).arg( zoomMax ).arg( minValueExpr ).arg( maxValueExpr ).arg( x1 ).arg( y1 ).arg( x2 ).arg( y2 );
3672 break;
3673 }
3674 }
3675
3676 if ( multiplier != 1 )
3677 return u"(%1) * %2"_s.arg( expression ).arg( multiplier );
3678 else
3679 return expression;
3680}
3681
3682Qt::PenCapStyle QgsMapBoxGlStyleConverter::parseCapStyle( const QString &style )
3683{
3684 if ( style == "round"_L1 )
3685 return Qt::RoundCap;
3686 else if ( style == "square"_L1 )
3687 return Qt::SquareCap;
3688 else
3689 return Qt::FlatCap; // "butt" is default
3690}
3691
3692Qt::PenJoinStyle QgsMapBoxGlStyleConverter::parseJoinStyle( const QString &style )
3693{
3694 if ( style == "bevel"_L1 )
3695 return Qt::BevelJoin;
3696 else if ( style == "round"_L1 )
3697 return Qt::RoundJoin;
3698 else
3699 return Qt::MiterJoin; // "miter" is default
3700}
3701
3702QString QgsMapBoxGlStyleConverter::parseExpression( const QVariantList &expression, QgsMapBoxGlStyleConversionContext &context, bool colorExpected )
3703{
3704 QString op = expression.value( 0 ).toString();
3705 if ( ( op == "%"_L1 || op == "/"_L1 || op == "-"_L1 || op == "^"_L1 ) && expression.size() >= 3 )
3706 {
3707 if ( expression.size() != 3 )
3708 {
3709 context.pushWarning( QObject::tr( "%1: Operator %2 requires exactly two operands, skipping extra operands" ).arg( context.layerId() ).arg( op ) );
3710 }
3711 QString v1 = parseValue( expression.value( 1 ), context, colorExpected );
3712 QString v2 = parseValue( expression.value( 2 ), context, colorExpected );
3713 return u"(%1 %2 %3)"_s.arg( v1, op, v2 );
3714 }
3715 else if ( ( op == "*"_L1 || op == "+"_L1 ) && expression.size() >= 3 )
3716 {
3717 QStringList operands;
3718 std::transform( std::next( expression.begin() ), expression.end(), std::back_inserter( operands ), [&context, colorExpected]( const QVariant &val ) {
3719 return parseValue( val, context, colorExpected );
3720 } );
3721 return u"(%1)"_s.arg( operands.join( u" %1 "_s.arg( op ) ) );
3722 }
3723 else if ( op == "to-number"_L1 )
3724 {
3725 return u"to_real(%1)"_s.arg( parseValue( expression.value( 1 ), context ) );
3726 }
3727 else if ( op == "sqrt"_L1 )
3728 {
3729 return u"sqrt(%1)"_s.arg( parseValue( expression.value( 1 ), context ) );
3730 }
3731 else if ( op == "literal"_L1 )
3732 {
3733 return expression.value( 1 ).toString();
3734 }
3735 else if ( op == "all"_L1 || op == "any"_L1 || op == "none"_L1 )
3736 {
3737 QStringList parts;
3738 for ( int i = 1; i < expression.size(); ++i )
3739 {
3740 const QString part = parseValue( expression.at( i ), context );
3741 if ( part.isEmpty() )
3742 {
3743 context.pushWarning( QObject::tr( "%1: Skipping unsupported expression" ).arg( context.layerId() ) );
3744 return QString();
3745 }
3746 parts << part;
3747 }
3748
3749 if ( op == "none"_L1 )
3750 return u"NOT (%1)"_s.arg( parts.join( ") AND NOT ("_L1 ) );
3751
3752 QString operatorString;
3753 if ( op == "all"_L1 )
3754 operatorString = u") AND ("_s;
3755 else if ( op == "any"_L1 )
3756 operatorString = u") OR ("_s;
3757
3758 return u"(%1)"_s.arg( parts.join( operatorString ) );
3759 }
3760 else if ( op == '!' )
3761 {
3762 // ! inverts next expression's meaning
3763 QVariantList contraJsonExpr = expression.value( 1 ).toList();
3764 contraJsonExpr[0] = QString( op + contraJsonExpr[0].toString() );
3765 // ['!', ['has', 'level']] -> ['!has', 'level']
3766 return parseKey( contraJsonExpr, context );
3767 }
3768 else if ( op == "=="_L1 || op == "!="_L1 || op == ">="_L1 || op == '>' || op == "<="_L1 || op == '<' )
3769 {
3770 // use IS and NOT IS instead of = and != because they can deal with NULL values
3771 if ( op == "=="_L1 )
3772 op = u"IS"_s;
3773 else if ( op == "!="_L1 )
3774 op = u"IS NOT"_s;
3775 return u"%1 %2 %3"_s.arg( parseKey( expression.value( 1 ), context ), op, parseValue( expression.value( 2 ), context ) );
3776 }
3777 else if ( op == "has"_L1 )
3778 {
3779 return parseKey( expression.value( 1 ), context ) + u" IS NOT NULL"_s;
3780 }
3781 else if ( op == "!has"_L1 )
3782 {
3783 return parseKey( expression.value( 1 ), context ) + u" IS NULL"_s;
3784 }
3785 else if ( op == "in"_L1 || op == "!in"_L1 )
3786 {
3787 const QString key = parseKey( expression.value( 1 ), context );
3788 QStringList parts;
3789
3790 QVariantList values = expression.mid( 2 );
3791 if ( expression.size() == 3 && expression.at( 2 ).userType() == QMetaType::Type::QVariantList && expression.at( 2 ).toList().count() > 1 && expression.at( 2 ).toList().at( 0 ).toString() == "literal"_L1 )
3792 {
3793 values = expression.at( 2 ).toList().at( 1 ).toList();
3794 }
3795
3796 for ( const QVariant &value : std::as_const( values ) )
3797 {
3798 const QString part = parseValue( value, context );
3799 if ( part.isEmpty() )
3800 {
3801 context.pushWarning( QObject::tr( "%1: Skipping unsupported expression" ).arg( context.layerId() ) );
3802 return QString();
3803 }
3804 parts << part;
3805 }
3806
3807 if ( parts.size() == 1 )
3808 {
3809 if ( op == "in"_L1 )
3810 return u"%1 IS %2"_s.arg( key, parts.at( 0 ) );
3811 else
3812 return u"(%1 IS NULL OR %1 IS NOT %2)"_s.arg( key, parts.at( 0 ) );
3813 }
3814 else
3815 {
3816 if ( op == "in"_L1 )
3817 return u"%1 IN (%2)"_s.arg( key, parts.join( ", "_L1 ) );
3818 else
3819 return u"(%1 IS NULL OR %1 NOT IN (%2))"_s.arg( key, parts.join( ", "_L1 ) );
3820 }
3821 }
3822 else if ( op == "get"_L1 )
3823 {
3824 return parseKey( expression.value( 1 ), context );
3825 }
3826 else if ( op == "match"_L1 )
3827 {
3828 const QVariantList attributeList = expression.value( 1 ).toList();
3829 QString attribute = attributeList.size() > 1 ? expression.value( 1 ).toList().value( 1 ).toString() : expression.value( 1 ).toList().value( 0 ).toString();
3830 if ( attribute == "geometry-type"_L1 )
3831 {
3832 attribute = u"_geom_type"_s;
3833 }
3834
3835 if ( expression.size() == 5
3836 && expression.at( 3 ).userType() == QMetaType::Type::Bool
3837 && expression.at( 3 ).toBool() == true
3838 && expression.at( 4 ).userType() == QMetaType::Type::Bool
3839 && expression.at( 4 ).toBool() == false )
3840 {
3841 // simple case, make a nice simple expression instead of a CASE statement
3842 if ( expression.at( 2 ).userType() == QMetaType::Type::QVariantList || expression.at( 2 ).userType() == QMetaType::Type::QStringList )
3843 {
3844 QStringList parts;
3845 for ( const QVariant &p : expression.at( 2 ).toList() )
3846 {
3847 parts << parseValue( p, context );
3848 }
3849
3850 if ( parts.size() > 1 )
3851 return u"%1 IN (%2)"_s.arg( QgsExpression::quotedColumnRef( attribute ), parts.join( ", " ) );
3852 else
3853 return QgsExpression::createFieldEqualityExpression( attribute, expression.at( 2 ).toList().value( 0 ) );
3854 }
3855 else if ( expression.at( 2 ).userType() == QMetaType::Type::QString
3856 || expression.at( 2 ).userType() == QMetaType::Type::Int
3857 || expression.at( 2 ).userType() == QMetaType::Type::Double
3858 || expression.at( 2 ).userType() == QMetaType::Type::LongLong )
3859 {
3860 return QgsExpression::createFieldEqualityExpression( attribute, expression.at( 2 ) );
3861 }
3862 else
3863 {
3864 context.pushWarning( QObject::tr( "%1: Skipping unsupported expression" ).arg( context.layerId() ) );
3865 return QString();
3866 }
3867 }
3868 else
3869 {
3870 QString caseString = u"CASE "_s;
3871 for ( int i = 2; i < expression.size() - 2; i += 2 )
3872 {
3873 if ( expression.at( i ).userType() == QMetaType::Type::QVariantList || expression.at( i ).userType() == QMetaType::Type::QStringList )
3874 {
3875 QStringList parts;
3876 for ( const QVariant &p : expression.at( i ).toList() )
3877 {
3878 parts << QgsExpression::quotedValue( p );
3879 }
3880
3881 if ( parts.size() > 1 )
3882 caseString += u"WHEN %1 IN (%2) "_s.arg( QgsExpression::quotedColumnRef( attribute ), parts.join( ", " ) );
3883 else
3884 caseString += u"WHEN %1 "_s.arg( QgsExpression::createFieldEqualityExpression( attribute, expression.at( i ).toList().value( 0 ) ) );
3885 }
3886 else if ( expression.at( i ).userType() == QMetaType::Type::QString
3887 || expression.at( i ).userType() == QMetaType::Type::Int
3888 || expression.at( i ).userType() == QMetaType::Type::Double
3889 || expression.at( i ).userType() == QMetaType::Type::LongLong )
3890 {
3891 caseString += u"WHEN (%1) "_s.arg( QgsExpression::createFieldEqualityExpression( attribute, expression.at( i ) ) );
3892 }
3893
3894 caseString += u"THEN %1 "_s.arg( parseValue( expression.at( i + 1 ), context, colorExpected ) );
3895 }
3896 caseString += u"ELSE %1 END"_s.arg( parseValue( expression.last(), context, colorExpected ) );
3897 return caseString;
3898 }
3899 }
3900 else if ( op == "to-string"_L1 )
3901 {
3902 return u"to_string(%1)"_s.arg( parseExpression( expression.value( 1 ).toList(), context ) );
3903 }
3904 else if ( op == "to-boolean"_L1 )
3905 {
3906 return u"to_bool(%1)"_s.arg( parseExpression( expression.value( 1 ).toList(), context ) );
3907 }
3908 else if ( op == "case"_L1 )
3909 {
3910 QString caseString = u"CASE"_s;
3911 for ( int i = 1; i < expression.size() - 2; i += 2 )
3912 {
3913 const QString condition = parseExpression( expression.value( i ).toList(), context );
3914 const QString value = parseValue( expression.value( i + 1 ), context, colorExpected );
3915 caseString += u" WHEN (%1) THEN %2"_s.arg( condition, value );
3916 }
3917 const QString value = parseValue( expression.constLast(), context, colorExpected );
3918 caseString += u" ELSE %1 END"_s.arg( value );
3919 return caseString;
3920 }
3921 else if ( op == "zoom"_L1 && expression.count() == 1 )
3922 {
3923 return u"@vector_tile_zoom"_s;
3924 }
3925 else if ( op == "coalesce"_L1 )
3926 {
3927 QString coalesceString = u"coalesce("_s;
3928 for ( int i = 1; i < expression.size(); i++ )
3929 {
3930 if ( i > 1 )
3931 coalesceString += ", "_L1;
3932 coalesceString += parseValue( expression.value( i ), context );
3933 }
3934 coalesceString += ')'_L1;
3935 return coalesceString;
3936 }
3937 else if ( op == "concat"_L1 )
3938 {
3939 QString concatString = u"concat("_s;
3940 for ( int i = 1; i < expression.size(); i++ )
3941 {
3942 if ( i > 1 )
3943 concatString += ", "_L1;
3944 concatString += parseValue( expression.value( i ), context );
3945 }
3946 concatString += ')'_L1;
3947 return concatString;
3948 }
3949 else if ( op == "length"_L1 )
3950 {
3951 return u"length(%1)"_s.arg( parseExpression( expression.value( 1 ).toList(), context ) );
3952 }
3953 else if ( op == "step"_L1 )
3954 {
3955 const QString stepExpression = parseExpression( expression.value( 1 ).toList(), context );
3956 if ( stepExpression.isEmpty() )
3957 {
3958 context.pushWarning( QObject::tr( "%1: Could not interpret step list" ).arg( context.layerId() ) );
3959 return QString();
3960 }
3961
3962 QString caseString = u"CASE "_s;
3963
3964 for ( int i = expression.length() - 2; i > 0; i -= 2 )
3965 {
3966 const QString stepValue = parseValue( expression.value( i + 1 ), context, colorExpected );
3967 if ( i > 1 )
3968 {
3969 const QString stepKey = QgsExpression::quotedValue( expression.value( i ) );
3970 caseString += u" WHEN %1 >= %2 THEN (%3) "_s.arg( stepExpression, stepKey, stepValue );
3971 }
3972 else
3973 {
3974 caseString += u"ELSE (%1) END"_s.arg( stepValue );
3975 }
3976 }
3977 return caseString;
3978 }
3979 else if ( op == "pitch"_L1 )
3980 {
3981 return u"0"_s;
3982 }
3983 else if ( op == "slice"_L1 )
3984 {
3985 // ["slice", input, startIndex, endIndex?] returns a substring/sublist of input.
3986 // MapBox indices are 0-based and endIndex is exclusive, while QGIS substr() is
3987 // 1-based and takes a length
3988 const QString inputExpression = parseValue( expression.value( 1 ), context );
3989 if ( inputExpression.isEmpty() )
3990 {
3991 context.pushWarning( QObject::tr( "%1: Could not interpret slice list" ).arg( context.layerId() ) );
3992 return QString();
3993 }
3994
3995 // When the indices are constant integers
3996 // we can fold the index arithmetic at conversion time
3997 const auto constantInt = []( const QVariant &value, int &result ) -> bool {
3998 switch ( value.userType() )
3999 {
4000 case QMetaType::Int:
4001 case QMetaType::UInt:
4002 case QMetaType::LongLong:
4003 case QMetaType::ULongLong:
4004 {
4005 bool ok = false;
4006 result = value.toInt( &ok );
4007 return ok;
4008 }
4009 default:
4010 return false;
4011 }
4012 };
4013
4014 int startValue = 0;
4015 const bool startIsConstant = constantInt( expression.value( 2 ), startValue );
4016 const QString startExpression = parseValue( expression.value( 2 ), context );
4017 const QString startOffset = startIsConstant ? QString::number( startValue + 1 ) : u"(%1) + 1"_s.arg( startExpression );
4018
4019 if ( expression.size() > 3 )
4020 {
4021 int endValue = 0;
4022 const bool endIsConstant = constantInt( expression.value( 3 ), endValue );
4023 const QString endExpression = parseValue( expression.value( 3 ), context );
4024 const QString length = ( startIsConstant && endIsConstant ) ? QString::number( endValue - startValue ) : u"(%1) - (%2)"_s.arg( endExpression, startExpression );
4025 return u"substr(%1, %2, %3)"_s.arg( inputExpression, startOffset, length );
4026 }
4027 else
4028 {
4029 return u"substr(%1, %2)"_s.arg( inputExpression, startOffset );
4030 }
4031 }
4032 else
4033 {
4034 context.pushWarning( QObject::tr( "%1: Skipping unsupported expression \"%2\"" ).arg( context.layerId(), op ) );
4035 return QString();
4036 }
4037}
4038
4039QImage QgsMapBoxGlStyleConverter::retrieveSprite( const QString &name, QgsMapBoxGlStyleConversionContext &context, QSize &spriteSize )
4040{
4041 QImage spriteImage;
4042
4043 if ( name.isEmpty() )
4044 {
4045 return QImage();
4046 }
4047
4048 QString category;
4049 QString actualName = name;
4050 const int categorySeparator = name.indexOf( ':' );
4051 if ( categorySeparator > 0 )
4052 {
4053 category = name.left( categorySeparator );
4054 if ( context.spriteCategories().contains( category ) )
4055 {
4056 actualName = name.mid( categorySeparator + 1 );
4057 spriteImage = context.spriteImage( category );
4058 }
4059 else
4060 {
4061 category.clear();
4062 }
4063 }
4064
4065 if ( category.isEmpty() )
4066 {
4067 // Images referenced without a category prefix belong to the sprite source with
4068 // the id "default" (if present), otherwise the single unnamed sprite sheet.
4069 if ( context.spriteCategories().contains( "default"_L1 ) )
4070 category = u"default"_s;
4071 spriteImage = context.spriteImage( category );
4072 }
4073
4074 if ( spriteImage.isNull() )
4075 {
4076 context.pushWarning( QObject::tr( "%1: Could not retrieve sprite '%2'" ).arg( context.layerId(), name ) );
4077 return QImage();
4078 }
4079
4080 const QVariantMap spriteDefinition = context.spriteDefinitions( category ).value( actualName ).toMap();
4081 if ( spriteDefinition.size() == 0 )
4082 {
4083 context.pushWarning( QObject::tr( "%1: Could not retrieve sprite '%2'" ).arg( context.layerId(), name ) );
4084 return QImage();
4085 }
4086
4087 const QImage sprite
4088 = spriteImage.copy( spriteDefinition.value( u"x"_s ).toInt(), spriteDefinition.value( u"y"_s ).toInt(), spriteDefinition.value( u"width"_s ).toInt(), spriteDefinition.value( u"height"_s ).toInt() );
4089 if ( sprite.isNull() )
4090 {
4091 context.pushWarning( QObject::tr( "%1: Could not retrieve sprite '%2'" ).arg( context.layerId(), name ) );
4092 return QImage();
4093 }
4094
4095 spriteSize = sprite.size() / spriteDefinition.value( u"pixelRatio"_s ).toDouble() * context.pixelSizeConversionFactor();
4096 return sprite;
4097}
4098
4100 const QVariant &value, QgsMapBoxGlStyleConversionContext &context, QSize &spriteSize, QString &spriteProperty, QString &spriteSizeProperty
4101)
4102{
4103 QString spritePath;
4104
4105 auto prepareBase64 = []( const QImage &sprite ) {
4106 QString path;
4107 if ( !sprite.isNull() )
4108 {
4109 QByteArray blob;
4110 QBuffer buffer( &blob );
4111 buffer.open( QIODevice::WriteOnly );
4112 sprite.save( &buffer, "PNG" );
4113 buffer.close();
4114 const QByteArray encoded = blob.toBase64();
4115 path = QString( encoded );
4116 path.prepend( "base64:"_L1 );
4117 }
4118 return path;
4119 };
4120
4121 switch ( value.userType() )
4122 {
4123 case QMetaType::Type::QString:
4124 {
4125 QString spriteName = value.toString();
4126 const thread_local QRegularExpression fieldNameMatch( u"{([^}]+)}"_s );
4127 QRegularExpressionMatch match = fieldNameMatch.match( spriteName );
4128 if ( match.hasMatch() )
4129 {
4130 const QString fieldName = match.captured( 1 );
4131 spriteProperty = u"CASE"_s;
4132 spriteSizeProperty = u"CASE"_s;
4133
4134 spriteName.replace( "(", "\\("_L1 );
4135 spriteName.replace( ")", "\\)"_L1 );
4136 spriteName.replace( fieldNameMatch, u"([^\\/\\\\]+)"_s );
4137 const QRegularExpression fieldValueMatch( spriteName );
4138 const QStringList spriteNames = context.spriteDefinitions().keys();
4139 for ( const QString &name : spriteNames )
4140 {
4141 match = fieldValueMatch.match( name );
4142 if ( match.hasMatch() )
4143 {
4144 QSize size;
4145 QString path;
4146 const QString fieldValue = match.captured( 1 );
4147 const QImage sprite = retrieveSprite( name, context, size );
4148 path = prepareBase64( sprite );
4149 if ( spritePath.isEmpty() && !path.isEmpty() )
4150 {
4151 spritePath = path;
4152 spriteSize = size;
4153 }
4154
4155 spriteProperty += u" WHEN \"%1\" = '%2' THEN '%3'"_s.arg( fieldName, fieldValue, path );
4156 spriteSizeProperty += u" WHEN \"%1\" = '%2' THEN %3"_s.arg( fieldName ).arg( fieldValue ).arg( size.width() );
4157 }
4158 }
4159
4160 spriteProperty += " END"_L1;
4161 spriteSizeProperty += " END"_L1;
4162 }
4163 else
4164 {
4165 spriteProperty.clear();
4166 spriteSizeProperty.clear();
4167 const QImage sprite = retrieveSprite( spriteName, context, spriteSize );
4168 spritePath = prepareBase64( sprite );
4169 }
4170 break;
4171 }
4172
4173 case QMetaType::Type::QVariantMap:
4174 {
4175 const QVariantList stops = value.toMap().value( u"stops"_s ).toList();
4176 if ( stops.size() == 0 )
4177 break;
4178
4179 QString path;
4180 QSize size;
4181 QImage sprite;
4182
4183 sprite = retrieveSprite( stops.value( 0 ).toList().value( 1 ).toString(), context, spriteSize );
4184 spritePath = prepareBase64( sprite );
4185
4186 spriteProperty = u"CASE WHEN @vector_tile_zoom < %1 THEN '%2'"_s.arg( stops.value( 0 ).toList().value( 0 ).toString() ).arg( spritePath );
4187 spriteSizeProperty = u"CASE WHEN @vector_tile_zoom < %1 THEN %2"_s.arg( stops.value( 0 ).toList().value( 0 ).toString() ).arg( spriteSize.width() );
4188
4189 for ( int i = 0; i < stops.size() - 1; ++i )
4190 {
4191 ;
4192 sprite = retrieveSprite( stops.value( 0 ).toList().value( 1 ).toString(), context, size );
4193 path = prepareBase64( sprite );
4194
4195 spriteProperty += QStringLiteral(
4196 " WHEN @vector_tile_zoom >= %1 AND @vector_tile_zoom < %2 "
4197 "THEN '%3'"
4198 )
4199 .arg( stops.value( i ).toList().value( 0 ).toString(), stops.value( i + 1 ).toList().value( 0 ).toString(), path );
4200 spriteSizeProperty += QStringLiteral(
4201 " WHEN @vector_tile_zoom >= %1 AND @vector_tile_zoom < %2 "
4202 "THEN %3"
4203 )
4204 .arg( stops.value( i ).toList().value( 0 ).toString(), stops.value( i + 1 ).toList().value( 0 ).toString() )
4205 .arg( size.width() );
4206 }
4207 sprite = retrieveSprite( stops.last().toList().value( 1 ).toString(), context, size );
4208 path = prepareBase64( sprite );
4209
4210 spriteProperty += QStringLiteral(
4211 " WHEN @vector_tile_zoom >= %1 "
4212 "THEN '%2' END"
4213 )
4214 .arg( stops.last().toList().value( 0 ).toString() )
4215 .arg( path );
4216 spriteSizeProperty += QStringLiteral(
4217 " WHEN @vector_tile_zoom >= %1 "
4218 "THEN %2 END"
4219 )
4220 .arg( stops.last().toList().value( 0 ).toString() )
4221 .arg( size.width() );
4222 break;
4223 }
4224
4225 case QMetaType::Type::QVariantList:
4226 {
4227 const QVariantList json = value.toList();
4228 const QString method = json.value( 0 ).toString();
4229
4230 if ( method == "match"_L1 )
4231 {
4232 const QString attribute = parseExpression( json.value( 1 ).toList(), context );
4233 if ( attribute.isEmpty() )
4234 {
4235 context.pushWarning( QObject::tr( "%1: Could not interpret match list" ).arg( context.layerId() ) );
4236 break;
4237 }
4238
4239 spriteProperty = u"CASE"_s;
4240 spriteSizeProperty = u"CASE"_s;
4241
4242 for ( int i = 2; i < json.length() - 1; i += 2 )
4243 {
4244 const QVariant matchKey = json.value( i );
4245 const QVariant matchValue = json.value( i + 1 );
4246 QString matchString;
4247 switch ( matchKey.userType() )
4248 {
4249 case QMetaType::Type::QVariantList:
4250 case QMetaType::Type::QStringList:
4251 {
4252 const QVariantList keys = matchKey.toList();
4253 QStringList matchStringList;
4254 for ( const QVariant &key : keys )
4255 {
4256 matchStringList << QgsExpression::quotedValue( key );
4257 }
4258 matchString = matchStringList.join( ',' );
4259 break;
4260 }
4261
4262 case QMetaType::Type::Bool:
4263 case QMetaType::Type::QString:
4264 case QMetaType::Type::Int:
4265 case QMetaType::Type::LongLong:
4266 case QMetaType::Type::Double:
4267 {
4268 matchString = QgsExpression::quotedValue( matchKey );
4269 break;
4270 }
4271
4272 default:
4273 context.pushWarning( QObject::tr( "%1: Skipping unsupported sprite type (%2)." ).arg( context.layerId(), value.metaType().name() ) );
4274 break;
4275 }
4276
4277 QString valuePathExpression;
4278 QString valueSizeExpression;
4279 if ( matchValue.userType() == QMetaType::Type::QVariantList || matchValue.userType() == QMetaType::Type::QStringList )
4280 {
4281 // nested expression (e.g. a nested "match"/"step"/"case") -- resolve recursively
4282 QSize nestedSize;
4283 QString nestedProperty;
4284 QString nestedSizeProperty;
4285 const QString nestedPath = retrieveSpriteAsBase64WithProperties( matchValue, context, nestedSize, nestedProperty, nestedSizeProperty );
4286 valuePathExpression = nestedProperty.isEmpty() ? u"'%1'"_s.arg( nestedPath ) : nestedProperty;
4287 valueSizeExpression = nestedSizeProperty.isEmpty() ? QString::number( nestedSize.width() ) : nestedSizeProperty;
4288 spritePath = nestedPath;
4289 spriteSize = nestedSize;
4290 }
4291 else
4292 {
4293 const QImage sprite = retrieveSprite( matchValue.toString(), context, spriteSize );
4294 spritePath = prepareBase64( sprite );
4295 valuePathExpression = u"'%1'"_s.arg( spritePath );
4296 valueSizeExpression = QString::number( spriteSize.width() );
4297 }
4298
4299 spriteProperty += u" WHEN %1 IN (%2) THEN %3"_s.arg( attribute, matchString, valuePathExpression );
4300 spriteSizeProperty += u" WHEN %1 IN (%2) THEN %3"_s.arg( attribute, matchString, valueSizeExpression );
4301 }
4302
4303 const QVariant defaultValue = json.constLast();
4304 if ( defaultValue.userType() == QMetaType::Type::QVariantList || defaultValue.userType() == QMetaType::Type::QStringList )
4305 {
4306 // default is a nested expression (e.g. a nested "match"/"step"/"case") -- resolve recursively
4307 QSize nestedSize;
4308 QString nestedProperty;
4309 QString nestedSizeProperty;
4310 const QString nestedPath = retrieveSpriteAsBase64WithProperties( defaultValue, context, nestedSize, nestedProperty, nestedSizeProperty );
4311 spriteProperty += u" ELSE %1 END"_s.arg( nestedProperty.isEmpty() ? u"'%1'"_s.arg( nestedPath ) : nestedProperty );
4312 spriteSizeProperty += u" ELSE %1 END"_s.arg( nestedSizeProperty.isEmpty() ? QString::number( nestedSize.width() ) : nestedSizeProperty );
4313 spritePath = nestedPath;
4314 spriteSize = nestedSize;
4315 }
4316 else
4317 {
4318 if ( !defaultValue.toString().isEmpty() )
4319 {
4320 const QImage sprite = retrieveSprite( defaultValue.toString(), context, spriteSize );
4321 spritePath = prepareBase64( sprite );
4322 }
4323 else
4324 {
4325 spritePath = QString();
4326 }
4327
4328 spriteProperty += u" ELSE '%1' END"_s.arg( spritePath );
4329 spriteSizeProperty += u" ELSE %3 END"_s.arg( spriteSize.width() );
4330 }
4331 break;
4332 }
4333 else if ( method == "step"_L1 )
4334 {
4335 const QString expression = parseExpression( json.value( 1 ).toList(), context );
4336 if ( expression.isEmpty() )
4337 {
4338 context.pushWarning( QObject::tr( "%1: Could not interpret step list" ).arg( context.layerId() ) );
4339 break;
4340 }
4341
4342 spriteProperty = u"CASE"_s;
4343 spriteSizeProperty = u"CASE"_s;
4344 for ( int i = json.length() - 2; i > 2; i -= 2 )
4345 {
4346 const QString stepKey = QgsExpression::quotedValue( json.value( i ) );
4347 const QString stepValue = json.value( i + 1 ).toString();
4348
4349 const QImage sprite = retrieveSprite( stepValue, context, spriteSize );
4350 spritePath = prepareBase64( sprite );
4351
4352 spriteProperty += u" WHEN %1 >= %2 THEN '%3' "_s.arg( expression, stepKey, spritePath );
4353 spriteSizeProperty += u" WHEN %1 >= %2 THEN %3 "_s.arg( expression ).arg( stepKey ).arg( spriteSize.width() );
4354 }
4355
4356 const QImage sprite = retrieveSprite( json.at( 2 ).toString(), context, spriteSize );
4357 spritePath = prepareBase64( sprite );
4358
4359 spriteProperty += u"ELSE '%1' END"_s.arg( spritePath );
4360 spriteSizeProperty += u"ELSE %3 END"_s.arg( spriteSize.width() );
4361 break;
4362 }
4363 else if ( method == "concat"_L1 )
4364 {
4365 QString concatString = u"concat("_s;
4366 QStringList partialStringMatches;
4367 for ( int i = 1; i < json.size(); i++ )
4368 {
4369 if ( i > 1 )
4370 {
4371 concatString += ", "_L1;
4372 }
4373 concatString += parseValue( json.value( i ), context );
4374
4375 if ( json.value( i ).userType() == QMetaType::Type::QString )
4376 {
4377 partialStringMatches << json.value( i ).toString();
4378 }
4379 }
4380 concatString += ')'_L1;
4381
4382 spriteProperty = u"CASE"_s;
4383 spriteSizeProperty = u"CASE"_s;
4384
4385 const QStringList spriteCategories = context.spriteCategories();
4386 for ( const QString &category : spriteCategories )
4387 {
4388 const QStringList spriteNames = context.spriteDefinitions( category ).keys();
4389 for ( const QString &name : spriteNames )
4390 {
4391 QSize size;
4392 QString path;
4393 const QString spriteName = category.isEmpty() ? name : u"%1:%2"_s.arg( category, name );
4394 if ( !partialStringMatches.isEmpty() )
4395 {
4396 if ( !std::all_of( partialStringMatches.begin(), partialStringMatches.end(), [&spriteName]( const QString &string ) { return spriteName.contains( string ); } ) )
4397 {
4398 continue;
4399 }
4400 }
4401
4402 const QImage sprite = retrieveSprite( spriteName, context, size );
4403 path = prepareBase64( sprite );
4404 if ( spritePath.isEmpty() && !path.isEmpty() )
4405 {
4406 spritePath = path;
4407 spriteSize = size;
4408 }
4409
4410 spriteProperty += u" WHEN %1 = '%2' THEN '%3'"_s.arg( concatString, spriteName, path );
4411 spriteSizeProperty += u" WHEN %1 = '%2' THEN %3"_s.arg( concatString ).arg( spriteName ).arg( size.width() );
4412 }
4413
4414 spriteProperty += " ELSE '' END"_L1;
4415 spriteSizeProperty += " ELSE 0 END"_L1;
4416 }
4417 break;
4418 }
4419 else if ( method == "case"_L1 )
4420 {
4421 spriteProperty = u"CASE"_s;
4422 spriteSizeProperty = u"CASE"_s;
4423 for ( int i = 1; i < json.length() - 2; i += 2 )
4424 {
4425 const QString caseExpression = parseExpression( json.value( i ).toList(), context );
4426 const QString caseValue = json.value( i + 1 ).toString();
4427
4428 const QImage sprite = retrieveSprite( caseValue, context, spriteSize );
4429 spritePath = prepareBase64( sprite );
4430
4431 spriteProperty += u" WHEN %1 THEN '%2' "_s.arg( caseExpression, spritePath );
4432 spriteSizeProperty += u" WHEN %1 THEN %2 "_s.arg( caseExpression ).arg( spriteSize.width() );
4433 }
4434 const QImage sprite = retrieveSprite( json.last().toString(), context, spriteSize );
4435 spritePath = prepareBase64( sprite );
4436
4437 spriteProperty += u"ELSE '%1' END"_s.arg( spritePath );
4438 spriteSizeProperty += u"ELSE %3 END"_s.arg( spriteSize.width() );
4439 break;
4440 }
4441 else if ( method == "get"_L1 )
4442 {
4443 const QString attribute = parseKey( json.value( 1 ), context );
4444
4445 spriteProperty = u"CASE"_s;
4446 spriteSizeProperty = u"CASE"_s;
4447
4448 const QStringList spriteCategories = context.spriteCategories();
4449 for ( const QString &category : spriteCategories )
4450 {
4451 const QStringList spriteNames = context.spriteDefinitions( category ).keys();
4452 for ( const QString &name : spriteNames )
4453 {
4454 QSize size;
4455 QString path;
4456 const QString spriteName = category.isEmpty() ? name : u"%1:%2"_s.arg( category, name );
4457 const QImage sprite = retrieveSprite( spriteName, context, size );
4458 path = prepareBase64( sprite );
4459 if ( spritePath.isEmpty() && !path.isEmpty() )
4460 {
4461 spritePath = path;
4462 spriteSize = size;
4463 }
4464
4465 spriteProperty += u" WHEN %1 = '%2' THEN '%3'"_s.arg( attribute, spriteName, path );
4466 spriteSizeProperty += u" WHEN %1 = '%2' THEN %3"_s.arg( attribute ).arg( spriteName ).arg( size.width() );
4467 }
4468
4469 spriteProperty += " ELSE '' END"_L1;
4470 spriteSizeProperty += " ELSE 0 END"_L1;
4471 }
4472 break;
4473 }
4474 else
4475 {
4476 context.pushWarning( QObject::tr( "%1: Could not interpret sprite value list with method %2" ).arg( context.layerId(), method ) );
4477 break;
4478 }
4479 }
4480
4481 default:
4482 context.pushWarning( QObject::tr( "%1: Skipping unsupported sprite type (%2)." ).arg( context.layerId(), value.metaType().name() ) );
4483 break;
4484 }
4485
4486 return spritePath;
4487}
4488
4489QString QgsMapBoxGlStyleConverter::parseValue( const QVariant &value, QgsMapBoxGlStyleConversionContext &context, bool colorExpected )
4490{
4491 QColor c;
4492 switch ( value.userType() )
4493 {
4494 case QMetaType::Type::QVariantList:
4495 case QMetaType::Type::QStringList:
4496 return parseExpression( value.toList(), context, colorExpected );
4497
4498 case QMetaType::Type::Bool:
4499 case QMetaType::Type::QString:
4500 if ( colorExpected )
4501 {
4502 QColor c = parseColor( value, context );
4503 if ( c.isValid() )
4504 {
4505 return parseValue( c, context );
4506 }
4507 }
4508 return QgsExpression::quotedValue( value );
4509
4510 case QMetaType::Type::Int:
4511 case QMetaType::Type::LongLong:
4512 case QMetaType::Type::Double:
4513 return value.toString();
4514
4515 case QMetaType::Type::QColor:
4516 c = value.value<QColor>();
4517 return QString( "color_rgba(%1,%2,%3,%4)" ).arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
4518
4519 default:
4520 context.pushWarning( QObject::tr( "%1: Skipping unsupported expression part" ).arg( context.layerId() ) );
4521 break;
4522 }
4523 return QString();
4524}
4525
4526QString QgsMapBoxGlStyleConverter::parseKey( const QVariant &value, QgsMapBoxGlStyleConversionContext &context )
4527{
4528 if ( value.toString() == "$type"_L1 )
4529 {
4530 return u"_geom_type"_s;
4531 }
4532 if ( value.toString() == "level"_L1 )
4533 {
4534 return u"level"_s;
4535 }
4536 else if ( ( value.userType() == QMetaType::Type::QVariantList && value.toList().size() == 1 ) || value.userType() == QMetaType::Type::QStringList )
4537 {
4538 if ( value.toList().size() > 1 )
4539 return value.toList().at( 1 ).toString();
4540 else
4541 {
4542 QString valueString = value.toList().value( 0 ).toString();
4543 if ( valueString == "geometry-type"_L1 )
4544 {
4545 return u"_geom_type"_s;
4546 }
4547 return valueString;
4548 }
4549 }
4550 else if ( value.userType() == QMetaType::Type::QVariantList && value.toList().size() > 1 )
4551 {
4552 return parseExpression( value.toList(), context );
4553 }
4554 return QgsExpression::quotedColumnRef( value.toString() );
4555}
4556
4557QString QgsMapBoxGlStyleConverter::processLabelField( const QString &string, bool &isExpression )
4558{
4559 // {field_name} is permitted in string -- if multiple fields are present, convert them to an expression
4560 // but if single field is covered in {}, return it directly
4561 const thread_local QRegularExpression singleFieldRx( u"^{([^}]+)}$"_s );
4562 const QRegularExpressionMatch match = singleFieldRx.match( string );
4563 if ( match.hasMatch() )
4564 {
4565 isExpression = false;
4566 return match.captured( 1 );
4567 }
4568
4569 const thread_local QRegularExpression multiFieldRx( u"(?={[^}]+})"_s );
4570 const QStringList parts = string.split( multiFieldRx );
4571 if ( parts.size() > 1 )
4572 {
4573 isExpression = true;
4574
4575 QStringList res;
4576 for ( const QString &part : parts )
4577 {
4578 if ( part.isEmpty() )
4579 continue;
4580
4581 if ( !part.contains( '{' ) )
4582 {
4583 res << QgsExpression::quotedValue( part );
4584 continue;
4585 }
4586
4587 // part will start at a {field} reference
4588 const QStringList split = part.split( '}' );
4589 res << QgsExpression::quotedColumnRef( split.at( 0 ).mid( 1 ) );
4590 if ( !split.at( 1 ).isEmpty() )
4591 res << QgsExpression::quotedValue( split.at( 1 ) );
4592 }
4593 return u"concat(%1)"_s.arg( res.join( ',' ) );
4594 }
4595 else
4596 {
4597 isExpression = false;
4598 return string;
4599 }
4600}
4601
4602std::unique_ptr<QgsVectorTileRenderer> QgsMapBoxGlStyleConverter::renderer() const
4603{
4604 return mRenderer ? std::unique_ptr<QgsVectorTileRenderer>( mRenderer->clone() ) : nullptr;
4605}
4606
4607std::unique_ptr<QgsVectorTileLabeling> QgsMapBoxGlStyleConverter::labeling() const
4608{
4609 return mLabeling ? std::unique_ptr<QgsVectorTileLabeling>( mLabeling->clone() ) : nullptr;
4610}
4611
4612QList<QgsMapBoxGlStyleAbstractSource *> QgsMapBoxGlStyleConverter::sources()
4613{
4614 return mSources;
4615}
4616
4617QList<QgsMapBoxGlStyleRasterSubLayer> QgsMapBoxGlStyleConverter::rasterSubLayers() const
4618{
4619 return mRasterSubLayers;
4620}
4621
4623{
4624 QList<QgsMapLayer *> subLayers;
4625 for ( const QgsMapBoxGlStyleRasterSubLayer &subLayer : mRasterSubLayers )
4626 {
4627 const QString sourceName = subLayer.source();
4628 std::unique_ptr< QgsRasterLayer > rl;
4629 for ( const QgsMapBoxGlStyleAbstractSource *source : mSources )
4630 {
4631 if ( source->type() == Qgis::MapBoxGlStyleSourceType::Raster && source->name() == sourceName )
4632 {
4633 const QgsMapBoxGlStyleRasterSource *rasterSource = qgis::down_cast< const QgsMapBoxGlStyleRasterSource * >( source );
4634 rl.reset( rasterSource->toRasterLayer() );
4635 rl->pipe()->setDataDefinedProperties( subLayer.dataDefinedProperties() );
4636 break;
4637 }
4638 }
4639
4640 if ( rl )
4641 {
4642 subLayers.append( rl.release() );
4643 }
4644 }
4645 return subLayers;
4646}
4647
4648
4650{
4651 std::unique_ptr< QgsMapBoxGlStyleConversionContext > tmpContext;
4652 if ( !context )
4653 {
4654 tmpContext = std::make_unique< QgsMapBoxGlStyleConversionContext >();
4655 context = tmpContext.get();
4656 }
4657
4658 auto typeFromString = [context]( const QString &string, const QString &name ) -> Qgis::MapBoxGlStyleSourceType {
4659 if ( string.compare( "vector"_L1, Qt::CaseInsensitive ) == 0 )
4661 else if ( string.compare( "raster"_L1, Qt::CaseInsensitive ) == 0 )
4663 else if ( string.compare( "raster-dem"_L1, Qt::CaseInsensitive ) == 0 )
4665 else if ( string.compare( "geojson"_L1, Qt::CaseInsensitive ) == 0 )
4667 else if ( string.compare( "image"_L1, Qt::CaseInsensitive ) == 0 )
4669 else if ( string.compare( "video"_L1, Qt::CaseInsensitive ) == 0 )
4671 context->pushWarning( QObject::tr( "Invalid source type \"%1\" for source \"%2\"" ).arg( string, name ) );
4673 };
4674
4675 for ( auto it = sources.begin(); it != sources.end(); ++it )
4676 {
4677 const QString name = it.key();
4678 const QVariantMap jsonSource = it.value().toMap();
4679 const QString typeString = jsonSource.value( u"type"_s ).toString();
4680
4681 const Qgis::MapBoxGlStyleSourceType type = typeFromString( typeString, name );
4682
4683 switch ( type )
4684 {
4686 parseRasterSource( jsonSource, name, context );
4687 break;
4694 QgsDebugError( u"Ignoring vector tile style source %1 (%2)"_s.arg( name, qgsEnumValueToKey( type ) ) );
4695 continue;
4696 }
4697 }
4698}
4699
4700void QgsMapBoxGlStyleConverter::parseRasterSource( const QVariantMap &source, const QString &name, QgsMapBoxGlStyleConversionContext *context )
4701{
4702 std::unique_ptr< QgsMapBoxGlStyleConversionContext > tmpContext;
4703 if ( !context )
4704 {
4705 tmpContext = std::make_unique< QgsMapBoxGlStyleConversionContext >();
4706 context = tmpContext.get();
4707 }
4708
4709 auto raster = std::make_unique< QgsMapBoxGlStyleRasterSource >( name );
4710 if ( raster->setFromJson( source, context ) )
4711 mSources.append( raster.release() );
4712}
4713
4714bool QgsMapBoxGlStyleConverter::numericArgumentsOnly( const QVariant &bottomVariant, const QVariant &topVariant, double &bottom, double &top )
4715{
4716 if ( bottomVariant.canConvert<double>() && topVariant.canConvert<double>() )
4717 {
4718 bool bDoubleOk, tDoubleOk;
4719 bottom = bottomVariant.toDouble( &bDoubleOk );
4720 top = topVariant.toDouble( &tDoubleOk );
4721 return ( bDoubleOk && tDoubleOk );
4722 }
4723 return false;
4724}
4725
4726//
4727// QgsMapBoxGlStyleConversionContext
4728//
4730{
4731 QgsDebugError( warning );
4732 mWarnings << warning;
4733}
4734
4736{
4737 return mTargetUnit;
4738}
4739
4744
4746{
4747 return mSizeConversionFactor;
4748}
4749
4751{
4752 mSizeConversionFactor = sizeConversionFactor;
4753}
4754
4756{
4757 return mSpriteImage.keys();
4758}
4759
4760QImage QgsMapBoxGlStyleConversionContext::spriteImage( const QString &category ) const
4761{
4762 return mSpriteImage.contains( category ) ? mSpriteImage[category] : QImage();
4763}
4764
4765QVariantMap QgsMapBoxGlStyleConversionContext::spriteDefinitions( const QString &category ) const
4766{
4767 return mSpriteDefinitions.contains( category ) ? mSpriteDefinitions[category] : QVariantMap();
4768}
4769
4770void QgsMapBoxGlStyleConversionContext::setSprites( const QImage &image, const QVariantMap &definitions, const QString &category )
4771{
4772 mSpriteImage[category] = image;
4773 mSpriteDefinitions[category] = definitions;
4774}
4775
4776void QgsMapBoxGlStyleConversionContext::setSprites( const QImage &image, const QString &definitions, const QString &category )
4777{
4778 setSprites( image, QgsJsonUtils::parseJson( definitions ).toMap(), category );
4779}
4780
4782{
4783 return mLayerId;
4784}
4785
4787{
4788 mLayerId = value;
4789}
4790
4791//
4792// QgsMapBoxGlStyleAbstractSource
4793//
4797
4799{
4800 return mName;
4801}
4802
4804
4805//
4806// QgsMapBoxGlStyleRasterSource
4807//
4808
4812
4817
4819{
4820 mAttribution = json.value( u"attribution"_s ).toString();
4821
4822 const QString scheme = json.value( u"scheme"_s, u"xyz"_s ).toString();
4823 if ( scheme.compare( "xyz"_L1 ) == 0 )
4824 {
4825 // xyz scheme is supported
4826 }
4827 else
4828 {
4829 context->pushWarning( QObject::tr( "%1 scheme is not supported for raster source %2" ).arg( scheme, name() ) );
4830 return false;
4831 }
4832
4833 mMinZoom = json.value( u"minzoom"_s, u"0"_s ).toInt();
4834 mMaxZoom = json.value( u"maxzoom"_s, u"22"_s ).toInt();
4835 mTileSize = json.value( u"tileSize"_s, u"512"_s ).toInt();
4836
4837 const QVariantList tiles = json.value( u"tiles"_s ).toList();
4838 for ( const QVariant &tile : tiles )
4839 {
4840 mTiles.append( tile.toString() );
4841 }
4842
4843 return true;
4844}
4845
4847{
4848 QVariantMap parts;
4849 parts.insert( u"type"_s, u"xyz"_s );
4850 parts.insert( u"url"_s, mTiles.value( 0 ) );
4851
4852 if ( mTileSize == 256 )
4853 parts.insert( u"tilePixelRation"_s, u"1"_s );
4854 else if ( mTileSize == 512 )
4855 parts.insert( u"tilePixelRation"_s, u"2"_s );
4856
4857 parts.insert( u"zmax"_s, QString::number( mMaxZoom ) );
4858 parts.insert( u"zmin"_s, QString::number( mMinZoom ) );
4859
4860 auto rl = std::make_unique< QgsRasterLayer >( QgsProviderRegistry::instance()->encodeUri( u"wms"_s, parts ), name(), u"wms"_s );
4861 return rl.release();
4862}
4863
4864//
4865// QgsMapBoxGlStyleRasterSubLayer
4866//
4868 : mId( id )
4869 , mSource( source )
4870{}
@ BelowLine
Labels can be placed below a line feature. Unless MapOrientation is also specified this mode respects...
Definition qgis.h:1419
@ OnLine
Labels can be placed directly over a line feature.
Definition qgis.h:1417
@ AboveLine
Labels can be placed above a line feature. Unless MapOrientation is also specified this mode respects...
Definition qgis.h:1418
@ CentralPoint
Place symbols at the mid point of the line.
Definition qgis.h:3399
@ OverPoint
Arranges candidates over a point (or centroid of a polygon), or at a preset offset from the point....
Definition qgis.h:1310
@ Curved
Arranges candidates following the curvature of a line feature. Applies to line layers only.
Definition qgis.h:1312
@ Horizontal
Arranges horizontal candidates scattered throughout a polygon feature or along a line feature....
Definition qgis.h:1313
@ AboveRight
Above right.
Definition qgis.h:1399
@ BelowLeft
Below left.
Definition qgis.h:1403
@ Above
Above center.
Definition qgis.h:1398
@ BelowRight
Below right.
Definition qgis.h:1405
@ Right
Right middle.
Definition qgis.h:1402
@ AboveLeft
Above left.
Definition qgis.h:1397
@ Below
Below center.
Definition qgis.h:1404
@ Over
Center middle.
Definition qgis.h:1401
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:399
@ Point
Points.
Definition qgis.h:400
@ Line
Lines.
Definition qgis.h:401
@ Polygon
Polygons.
Definition qgis.h:402
@ Center
Center align.
Definition qgis.h:1481
@ FollowPlacement
Alignment follows placement of label, e.g., labels to the left of a feature will be drawn with right ...
Definition qgis.h:1483
RenderUnit
Rendering size units.
Definition qgis.h:5785
MapBoxGlStyleSourceType
Available MapBox GL style source types.
Definition qgis.h:4851
@ Vector
Vector source.
Definition qgis.h:4852
@ RasterDem
Raster DEM source.
Definition qgis.h:4854
@ Raster
Raster source.
Definition qgis.h:4853
@ Unknown
Other/unknown source type.
Definition qgis.h:4858
@ GeoJson
GeoJSON source.
Definition qgis.h:4855
@ Viewport
Relative to the whole viewport/output device.
Definition qgis.h:3474
@ AllowOverlapAtNoCost
Labels may freely overlap other labels, at no cost.
Definition qgis.h:1272
void setPenJoinStyle(Qt::PenJoinStyle style)
Sets the pen join style used to render the line (e.g.
void setPenCapStyle(Qt::PenCapStyle style)
Sets the pen cap style used to render the line (e.g.
static QgsFontManager * fontManager()
Returns the application font manager, which manages available fonts and font installation for the QGI...
A paint effect which blurs a source picture, using a number of different blur methods.
void setBlurUnit(const Qgis::RenderUnit unit)
Sets the units used for the blur level (radius).
@ StackBlur
Stack blur, a fast but low quality blur. Valid blur level values are between 0 - 16.
void setBlurMethod(const BlurMethod method)
Sets the blur method (algorithm) to use for performing the blur.
void setBlurLevel(const double level)
Sets blur level (radius).
A paint effect which consists of a stack of other chained paint effects.
void appendEffect(QgsPaintEffect *effect)
Appends an effect to the end of the stack.
static QString quotedValue(const QVariant &value)
Returns a string representation of a literal value, including appropriate quotations where required.
static QString quotedString(QString text)
Returns a quoted version of a string (in single quotes).
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value, QMetaType::Type fieldType=QMetaType::Type::UnknownType)
Create an expression allowing to evaluate if a field is equal to a value.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes).
QString processFontFamilyName(const QString &name) const
Processes a font family name, applying any matching fontFamilyReplacements() to the name.
static QFont createFont(const QString &family, int pointSize=-1, int weight=-1, bool italic=false)
Creates a font with the specified family.
static bool fontFamilyHasStyle(const QString &family, const QString &style)
Check whether font family on system has specific style.
static QVariant parseJson(const std::string &jsonString)
Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned and ...
void setPlacementFlags(Qgis::LabelLinePlacementFlags flags)
Returns the line placement flags, which dictate how line labels can be placed above or below the line...
void setFactor(double factor)
Sets the obstacle factor, where 1.0 = default, < 1.0 more likely to be covered by labels,...
Contains general settings related to how labels are placed.
void setOverlapHandling(Qgis::LabelOverlapHandling handling)
Sets the technique used to handle overlapping labels.
void setAllowDegradedPlacement(bool allow)
Sets whether labels can be placed in inferior fallback positions if they cannot otherwise be placed.
void setQuadrant(Qgis::LabelQuadrantPosition quadrant)
Sets the quadrant in which to offset labels from the point.
Contains settings related to how the label engine removes candidate label positions and reduces the n...
void setAllowDuplicateRemoval(bool allow)
Sets whether duplicate label removal is permitted for this layer.
void setMinimumDistanceToDuplicateUnit(Qgis::RenderUnit unit)
Sets the unit for the minimum distance to labels with duplicate text.
virtual void setWidth(double width)
Sets the width of the line symbol layer.
void setOffset(double offset)
Sets the line's offset.
void setOffsetUnit(Qgis::RenderUnit unit)
Sets the unit for the line's offset.
Abstract base class for MapBox GL style sources.
QString name() const
Returns the source's name.
QgsMapBoxGlStyleAbstractSource(const QString &name)
Constructor for QgsMapBoxGlStyleAbstractSource.
Context for a MapBox GL style conversion operation.
void setLayerId(const QString &value)
Sets the layer ID of the layer currently being converted.
QStringList warnings() const
Returns a list of warning messages generated during the conversion.
void pushWarning(const QString &warning)
Pushes a warning message generated during the conversion.
QImage spriteImage(const QString &category=QString()) const
Returns the sprite image for a given category to use during conversion, or an invalid image if this i...
double pixelSizeConversionFactor() const
Returns the pixel size conversion factor, used to scale the original pixel sizes when converting styl...
void setTargetUnit(Qgis::RenderUnit targetUnit)
Sets the target unit type.
void setSprites(const QImage &image, const QVariantMap &definitions, const QString &category=QString())
Sets the sprite image and definitions JSON for a given category to use during conversion.
void setPixelSizeConversionFactor(double sizeConversionFactor)
Sets the pixel size conversion factor, used to scale the original pixel sizes when converting styles.
QVariantMap spriteDefinitions(const QString &category=QString()) const
Returns the sprite definitions for a given category to use during conversion.
Qgis::RenderUnit targetUnit() const
Returns the target unit type.
QString layerId() const
Returns the layer ID of the layer currently being converted.
QStringList spriteCategories() const
Returns the list of sprite categories to use during conversion, or an empty list of none is set.
void clearWarnings()
Clears the list of warning messages.
static QgsProperty parseInterpolateByZoom(const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, double multiplier=1, double *defaultNumber=nullptr, QgsMapBoxGlStyleConverter::InterpolationType type=QgsMapBoxGlStyleConverter::InterpolationType::Exponential)
Parses a numeric value which is interpolated by zoom range.
static QString parseColorExpression(const QVariant &colorExpression, QgsMapBoxGlStyleConversionContext &context)
Converts an expression representing a color to a string (can be color string or an expression where a...
static QString parseExpression(const QVariantList &expression, QgsMapBoxGlStyleConversionContext &context, bool colorExpected=false)
Converts a MapBox GL expression to a QGIS expression.
InterpolationType
Interpolation types, for interpolated value conversion.
static QString parsePointStops(double base, const QVariantList &stops, QgsMapBoxGlStyleConversionContext &context, double multiplier=1, QgsMapBoxGlStyleConverter::InterpolationType type=QgsMapBoxGlStyleConverter::InterpolationType::Exponential, double x1=0, double y1=0, double x2=0, double y2=0)
Takes values from stops and uses either scale_linear() or scale_exp() functions to interpolate point/...
PropertyType
Property types, for interpolated value conversion.
@ DashArray
Dash array. Like numeric array, but must be even length array. Odd length arrays are considered as ha...
@ Numeric
Numeric property (e.g. line width, text size).
@ NumericArray
Numeric array for dash arrays or such.
QList< QgsMapBoxGlStyleAbstractSource * > sources()
Returns the list of converted sources.
QList< QgsMapBoxGlStyleRasterSubLayer > rasterSubLayers() const
Returns a list of raster sub layers contained in the style.
static Qt::PenJoinStyle parseJoinStyle(const QString &style)
Converts a value to Qt::PenJoinStyle enum from JSON value.
static QString interpolateExpression(double zoomMin, double zoomMax, QVariant valueMin, QVariant valueMax, double base, double multiplier=1, double x1=0, double y1=0, double x2=1, double y2=1, QgsMapBoxGlStyleConverter::InterpolationType type=QgsMapBoxGlStyleConverter::InterpolationType::Exponential, QgsMapBoxGlStyleConversionContext *contextPtr=nullptr)
Generates an interpolation for values between valueMin and valueMax, scaled between the ranges zoomMi...
static QgsProperty parseInterpolateStringByZoom(const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, const QVariantMap &conversionMap, QString *defaultString=nullptr)
Interpolates a string by zoom.
static QgsProperty parseStepList(const QVariantList &json, PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier=1, int maxOpacity=255, QColor *defaultColor=nullptr, double *defaultNumber=nullptr)
Parses and converts a match function value list.
static bool parseCircleLayer(const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style, QgsMapBoxGlStyleConversionContext &context)
Parses a circle layer.
Result convert(const QVariantMap &style, QgsMapBoxGlStyleConversionContext *context=nullptr)
Converts a JSON style map, and returns the resultant status of the conversion.
static QgsProperty parseInterpolateListByZoom(const QVariantList &json, PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier=1, int maxOpacity=255, QColor *defaultColor=nullptr, double *defaultNumber=nullptr)
Interpolates a list which starts with the interpolate function.
QList< QgsMapLayer * > createSubLayers() const
Returns a list of new map layers corresponding to sublayers of the style, e.g.
@ Success
Conversion was successful.
@ NoLayerList
No layer list was found in JSON input.
QgsMapBoxGlStyleConverter()
Constructor for QgsMapBoxGlStyleConverter.
static QgsProperty parseInterpolateColorByZoom(const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, QColor *defaultColor=nullptr, QgsMapBoxGlStyleConverter::InterpolationType type=QgsMapBoxGlStyleConverter::InterpolationType::Exponential)
Parses a color value which is interpolated by zoom range.
static QImage retrieveSprite(const QString &name, QgsMapBoxGlStyleConversionContext &context, QSize &spriteSize)
Retrieves the sprite image with the specified name, taken from the specified context.
static QString parseLabelStops(const QVariantList &stops, QgsMapBoxGlStyleConversionContext &context)
Parses a list of interpolation stops containing label values.
void parseLayers(const QVariantList &layers, QgsMapBoxGlStyleConversionContext *context=nullptr)
Parse list of layers from JSON.
static QgsProperty parseInterpolateOpacityByZoom(const QVariantMap &json, int maxOpacity, QgsMapBoxGlStyleConversionContext *contextPtr=nullptr, QgsMapBoxGlStyleConverter::InterpolationType type=QgsMapBoxGlStyleConverter::InterpolationType::Exponential)
Interpolates opacity with either scale_linear() or scale_exp() (depending on base value).
static QString retrieveSpriteAsBase64WithProperties(const QVariant &value, QgsMapBoxGlStyleConversionContext &context, QSize &spriteSize, QString &spriteProperty, QString &spriteSizeProperty)
Retrieves the sprite image with the specified name, taken from the specified context as a base64 enco...
std::unique_ptr< QgsVectorTileLabeling > labeling() const
Returns a new instance of a vector tile labeling representing the converted style,...
void parseSources(const QVariantMap &sources, QgsMapBoxGlStyleConversionContext *context=nullptr)
Parse list of sources from JSON.
static QColor parseColor(const QVariant &color, QgsMapBoxGlStyleConversionContext &context)
Parses a color in one of these supported formats:
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 void parseSymbolLayer(const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &rendererStyle, bool &hasRenderer, QgsVectorTileBasicLabelingStyle &labelingStyle, bool &hasLabeling, QgsMapBoxGlStyleConversionContext &context)
Parses a symbol layer as renderer or labeling.
static QString parseOpacityStops(double base, const QVariantList &stops, int maxOpacity, QgsMapBoxGlStyleConversionContext &context, QgsMapBoxGlStyleConverter::InterpolationType type=QgsMapBoxGlStyleConverter::InterpolationType::Exponential, double x1=0, double y1=0, double x2=1, double y2=1)
Takes values from stops and uses either scale_linear() or scale_exp() functions to interpolate alpha ...
static bool parseLineLayer(const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style, QgsMapBoxGlStyleConversionContext &context)
Parses a line layer.
void parseRasterSource(const QVariantMap &source, const QString &name, QgsMapBoxGlStyleConversionContext *context=nullptr)
Parse a raster source from JSON.
std::unique_ptr< QgsVectorTileRenderer > renderer() const
Returns a new instance of a vector tile renderer representing the converted style,...
static void colorAsHslaComponents(const QColor &color, int &hue, int &saturation, int &lightness, int &alpha)
Takes a QColor object and returns HSLA components in required format for QGIS color_hsla() expression...
static Qt::PenCapStyle parseCapStyle(const QString &style)
Converts a value to Qt::PenCapStyle enum from JSON value.
static QString parseStringStops(const QVariantList &stops, QgsMapBoxGlStyleConversionContext &context, const QVariantMap &conversionMap, QString *defaultString=nullptr)
Parses a list of interpolation stops containing string values.
static QgsProperty parseMatchList(const QVariantList &json, PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier=1, int maxOpacity=255, QColor *defaultColor=nullptr, double *defaultNumber=nullptr)
Parses and converts a match function value list.
static QString parseStops(double base, const QVariantList &stops, double multiplier, QgsMapBoxGlStyleConversionContext &context, QgsMapBoxGlStyleConverter::InterpolationType type=QgsMapBoxGlStyleConverter::InterpolationType::Exponential, double x1=0, double y1=0, double x2=1, double y2=1)
Parses a list of interpolation stops.
static QgsProperty parseValueList(const QVariantList &json, PropertyType type, QgsMapBoxGlStyleConversionContext &context, double multiplier=1, int maxOpacity=255, QColor *defaultColor=nullptr, double *defaultNumber=nullptr)
Parses and converts a value list (e.g.
static QgsProperty parseInterpolatePointByZoom(const QVariantMap &json, QgsMapBoxGlStyleConversionContext &context, double multiplier=1, QPointF *defaultPoint=nullptr, QgsMapBoxGlStyleConverter::InterpolationType type=QgsMapBoxGlStyleConverter::InterpolationType::Exponential)
Interpolates a point/offset with either scale_linear() or scale_exp() (depending on base value).
static QString parseArrayStops(const QVariantList &stops, QgsMapBoxGlStyleConversionContext &context, double multiplier=1)
Takes numerical arrays from stops.
Encapsulates a MapBox GL style raster source.
Qgis::MapBoxGlStyleSourceType type() const override
Returns the source type.
QgsMapBoxGlStyleRasterSource(const QString &name)
Constructor for QgsMapBoxGlStyleRasterSource.
QgsRasterLayer * toRasterLayer() const
Returns a new raster layer representing the raster source, or nullptr if the source cannot be represe...
bool setFromJson(const QVariantMap &json, QgsMapBoxGlStyleConversionContext *context) override
Sets the source's state from a json map.
QStringList tiles() const
Returns the list of tile sources.
Encapsulates a MapBox GL style raster sub layer.
QString source() const
Returns the layer's source.
QString id() const
Returns the layer's ID.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the layer's data defined properties.
QgsMapBoxGlStyleRasterSubLayer(const QString &id, const QString &source)
Constructor for QgsMapBoxGlStyleRasterSubLayer, with the given id and source.
Line symbol layer type which draws repeating marker symbols along a line feature.
void setOutputUnit(Qgis::RenderUnit unit) override
Sets the units to use for sizes and widths within the symbol layer.
bool setSubSymbol(QgsSymbol *symbol) override
Sets layer's subsymbol. takes ownership of the passed symbol.
virtual void setSize(double size)
Sets the symbol size.
void setOffsetUnit(Qgis::RenderUnit unit)
Sets the units for the symbol's offset.
void setAngle(double angle)
Sets the rotation angle for the marker.
void setOffset(QPointF offset)
Sets the marker's offset, which is the horizontal and vertical displacement which the rendered marker...
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the symbol's size.
A marker symbol type, for rendering Point and MultiPoint geometries.
void setEnabled(bool enabled)
Sets whether the effect is enabled.
Contains settings for how a map layer will be labeled.
double yOffset
Vertical offset of label.
const QgsLabelObstacleSettings & obstacleSettings() const
Returns the label obstacle settings.
const QgsLabelPlacementSettings & placementSettings() const
Returns the label placement settings.
void setFormat(const QgsTextFormat &format)
Sets the label text formatting settings, e.g., font settings, buffer settings, etc.
double xOffset
Horizontal offset of label.
Qgis::LabelPlacement placement
Label placement mode.
Qgis::LabelMultiLineAlignment multilineAlign
Horizontal alignment of multi-line labels.
int priority
Label priority.
double angleOffset
Label rotation, in degrees clockwise.
const QgsLabelThinningSettings & thinningSettings() const
Returns the label thinning settings.
void setThinningSettings(const QgsLabelThinningSettings &settings)
Sets the label thinning settings.
Qgis::RenderUnit offsetUnits
Units for offsets of label.
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the label's property collection, used for data defined overrides.
bool isExpression
true if this label is made from a expression string, e.g., FieldName || 'mm'
void setPlacementSettings(const QgsLabelPlacementSettings &settings)
Sets the label placement settings.
const QgsLabelLineSettings & lineSettings() const
Returns the label line settings, which contain settings related to how the label engine places and fo...
double dist
Distance from feature to the label.
Qgis::RenderUnit distUnits
Units the distance from feature to the label.
@ LinePlacementOptions
Line placement flags.
@ RemoveDuplicateLabelDistance
Minimum distance from labels for this feature to other labels with duplicate text.
QString fieldName
Name of field (or an expression) to use for label text.
int autoWrapLength
If non-zero, indicates that label text should be automatically wrapped to (ideally) the specified num...
const QgsLabelPointSettings & pointSettings() const
Returns the label point settings, which contain settings related to how the label engine places and f...
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
QVariant value(int key, const QgsExpressionContext &context, const QVariant &defaultValue=QVariant()) const final
Returns the calculated value of the property with the specified key from within the collection.
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
QgsProperty property(int key) const final
Returns a matching property from the collection, if one exists.
A store for object properties.
QString asExpression() const
Returns an expression string representing the state of the property, or an empty string if the proper...
QString expressionString() const
Returns the expression used for the property value.
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
void setExpressionString(const QString &expression)
Sets the expression to use for the property value.
static QgsProperty fromValue(const QVariant &value, bool isActive=true)
Returns a new StaticProperty created from the specified value.
void setActive(bool active)
Sets whether the property is currently active.
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
A fill symbol layer which fills polygons with a repeated raster image.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the unit for the image's width and height.
void setOpacity(double opacity)
Sets the opacity for the raster image used in the fill.
void setImageFilePath(const QString &imagePath)
Sets the path to the raster image used for the fill.
void setWidth(double width)
Sets the width for scaling the image used in the fill.
void setCoordinateMode(Qgis::SymbolCoordinateReference mode)
Set the coordinate mode for fill.
Represents a raster layer.
Line symbol layer type which draws line sections using a raster image file.
void setOutputUnit(Qgis::RenderUnit unit) override
Sets the units to use for sizes and widths within the symbol layer.
Raster marker symbol layer class.
void setOpacity(double opacity)
Set the marker opacity.
void setPath(const QString &path)
Set the marker raster image path.
@ RendererOpacity
Raster renderer global opacity.
Renders polygons using a single fill and stroke color.
void setBrushStyle(Qt::BrushStyle style)
void setOutputUnit(Qgis::RenderUnit unit) override
Sets the units to use for sizes and widths within the symbol layer.
void setStrokeWidth(double strokeWidth)
void setStrokeStyle(Qt::PenStyle strokeStyle)
void setOffsetUnit(Qgis::RenderUnit unit)
Sets the unit for the fill's offset.
void setFillColor(const QColor &color) override
Sets the fill color for the symbol layer.
void setOffset(QPointF offset)
Sets an offset by which polygons will be translated during rendering.
void setStrokeColor(const QColor &strokeColor) override
Sets the stroke color for the symbol layer.
A simple line symbol layer, which renders lines using a line in a variety of styles (e....
void setPenCapStyle(Qt::PenCapStyle style)
Sets the pen cap style used to render the line (e.g.
void setUseCustomDashPattern(bool b)
Sets whether the line uses a custom dash pattern.
void setCustomDashVector(const QVector< qreal > &vector)
Sets the custom dash vector, which is the pattern of alternating drawn/skipped lengths used while ren...
void setOutputUnit(Qgis::RenderUnit unit) override
Sets the units to use for sizes and widths within the symbol layer.
void setPenJoinStyle(Qt::PenJoinStyle style)
Sets the pen join style used to render the line (e.g.
Simple marker symbol layer, consisting of a rendered shape with solid fill color and a stroke.
void setFillColor(const QColor &color) override
Sets the fill color for the symbol layer.
void setStrokeWidthUnit(Qgis::RenderUnit u)
Sets the unit for the width of the marker's stroke.
void setStrokeWidth(double w)
Sets the width of the marker's stroke.
void setStrokeColor(const QColor &color) override
Sets the marker's stroke color.
static QColor parseColor(const QString &colorStr, bool strictEval=false)
Attempts to parse a string as a color using a variety of common formats, including hex codes,...
@ File
Filename, eg for svg files.
@ CustomDash
Custom dash pattern.
@ Name
Name, eg shape name for simple markers.
@ Interval
Line marker interval.
@ LayerEnabled
Whether symbol layer is enabled.
virtual void setColor(const QColor &color)
Sets the "representative" color for the symbol layer.
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the symbol layer's property collection, used for data defined overrides.
void setPlacements(Qgis::MarkerLinePlacements placements)
Sets the placement of the symbols.
Container for settings relating to a text background object.
void setMarkerSymbol(QgsMarkerSymbol *symbol)
Sets the current marker symbol for the background shape.
void setSizeType(SizeType type)
Sets the method used to determine the size of the background shape (e.g., fixed size or buffer around...
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units used for the shape's size.
void setType(ShapeType type)
Sets the type of background shape to draw (e.g., square, ellipse, SVG).
void setEnabled(bool enabled)
Sets whether the text background will be drawn.
void setSize(QSizeF size)
Sets the size of the background shape.
void setColor(const QColor &color)
Sets the color for the buffer.
void setOpacity(double opacity)
Sets the buffer opacity.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units used for the buffer size.
void setEnabled(bool enabled)
Sets whether the text buffer will be drawn.
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the buffer.
void setSize(double size)
Sets the size of the buffer.
Container for all settings relating to text rendering.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setSize(double size)
Sets the size for rendered text.
void setFont(const QFont &font)
Sets the font used for rendering text.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.
void setBackground(const QgsTextBackgroundSettings &backgroundSettings)
Sets the text's background settings.q.
void setNamedStyle(const QString &style)
Sets the named style for the font used for rendering text.
QFont font() const
Returns the font used for rendering text.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
Configuration of a single style within QgsVectorTileBasicLabeling.
void setLayerName(const QString &name)
Sets name of the sub-layer to render (empty layer means that all layers match).
void setMinZoomLevel(int minZoom)
Sets minimum zoom level index (negative number means no limit).
void setFilterExpression(const QString &expr)
Sets filter expression (empty filter means that all features match).
void setMaxZoomLevel(int maxZoom)
Sets maximum zoom level index (negative number means no limit).
void setStyleName(const QString &name)
Sets human readable name of this style.
void setGeometryType(Qgis::GeometryType geomType)
Sets type of the geometry that will be used (point / line / polygon).
void setLabelSettings(const QgsPalLayerSettings &settings)
Sets labeling configuration of this style.
void setEnabled(bool enabled)
Sets whether this style is enabled (used for rendering).
Definition of map rendering of a subset of vector tile data.
void setEnabled(bool enabled)
Sets whether this style is enabled (used for rendering).
void setMinZoomLevel(int minZoom)
Sets minimum zoom level index (negative number means no limit).
void setLayerName(const QString &name)
Sets name of the sub-layer to render (empty layer means that all layers match).
void setFilterExpression(const QString &expr)
Sets filter expression (empty filter means that all features match).
void setSymbol(QgsSymbol *sym)
Sets symbol for rendering. Takes ownership of the symbol.
void setStyleName(const QString &name)
Sets human readable name of this style.
void setMaxZoomLevel(int maxZoom)
Sets maximum zoom level index (negative number means no limit).
void setGeometryType(Qgis::GeometryType geomType)
Sets type of the geometry that will be used (point / line / polygon).
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:7733
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:8100
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:7826
#define QgsDebugError(str)
Definition qgslogger.h:71
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition qgssymbol.h:30