QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsvectortilelayer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectortilelayer.cpp
3  --------------------------------------
4  Date : March 2020
5  Copyright : (C) 2020 by Martin Dobias
6  Email : wonder dot sk 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 #include "qgsvectortilelayer.h"
17 
18 #include "qgslogger.h"
20 #include "qgsmbtiles.h"
23 #include "qgsvectortilelabeling.h"
24 #include "qgsvectortileloader.h"
25 #include "qgsvectortileutils.h"
27 
28 #include "qgsdatasourceuri.h"
32 #include "qgsjsonutils.h"
33 
34 QgsVectorTileLayer::QgsVectorTileLayer( const QString &uri, const QString &baseName )
35  : QgsMapLayer( QgsMapLayerType::VectorTileLayer, baseName )
36 {
37  mDataSource = uri;
38 
39  setValid( loadDataSource() );
40 
41  // set a default renderer
45 }
46 
47 bool QgsVectorTileLayer::loadDataSource()
48 {
49  QgsDataSourceUri dsUri;
50  dsUri.setEncodedUri( mDataSource );
51 
52  mSourceType = dsUri.param( QStringLiteral( "type" ) );
53  mSourcePath = dsUri.param( QStringLiteral( "url" ) );
54  if ( mSourceType == QLatin1String( "xyz" ) && dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
55  {
56  if ( !setupArcgisVectorTileServiceConnection( mSourcePath, dsUri ) )
57  return false;
58  }
59  else if ( mSourceType == QLatin1String( "xyz" ) )
60  {
61  if ( !QgsVectorTileUtils::checkXYZUrlTemplate( mSourcePath ) )
62  {
63  QgsDebugMsg( QStringLiteral( "Invalid format of URL for XYZ source: " ) + mSourcePath );
64  return false;
65  }
66 
67  // online tiles
68  mSourceMinZoom = 0;
69  mSourceMaxZoom = 14;
70 
71  if ( dsUri.hasParam( QStringLiteral( "zmin" ) ) )
72  mSourceMinZoom = dsUri.param( QStringLiteral( "zmin" ) ).toInt();
73  if ( dsUri.hasParam( QStringLiteral( "zmax" ) ) )
74  mSourceMaxZoom = dsUri.param( QStringLiteral( "zmax" ) ).toInt();
75 
76  setExtent( QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 ) );
77  }
78  else if ( mSourceType == QLatin1String( "mbtiles" ) )
79  {
80  QgsMbTiles reader( mSourcePath );
81  if ( !reader.open() )
82  {
83  QgsDebugMsg( QStringLiteral( "failed to open MBTiles file: " ) + mSourcePath );
84  return false;
85  }
86 
87  QString format = reader.metadataValue( QStringLiteral( "format" ) );
88  if ( format != QLatin1String( "pbf" ) )
89  {
90  QgsDebugMsg( QStringLiteral( "Cannot open MBTiles for vector tiles. Format = " ) + format );
91  return false;
92  }
93 
94  QgsDebugMsgLevel( QStringLiteral( "name: " ) + reader.metadataValue( QStringLiteral( "name" ) ), 2 );
95  bool minZoomOk, maxZoomOk;
96  int minZoom = reader.metadataValue( QStringLiteral( "minzoom" ) ).toInt( &minZoomOk );
97  int maxZoom = reader.metadataValue( QStringLiteral( "maxzoom" ) ).toInt( &maxZoomOk );
98  if ( minZoomOk )
99  mSourceMinZoom = minZoom;
100  if ( maxZoomOk )
101  mSourceMaxZoom = maxZoom;
102  QgsDebugMsgLevel( QStringLiteral( "zoom range: %1 - %2" ).arg( mSourceMinZoom ).arg( mSourceMaxZoom ), 2 );
103 
104  QgsRectangle r = reader.extent();
105  QgsCoordinateTransform ct( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ),
106  QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ), transformContext() );
107  r = ct.transformBoundingBox( r );
108  setExtent( r );
109  }
110  else
111  {
112  QgsDebugMsg( QStringLiteral( "Unknown source type: " ) + mSourceType );
113  return false;
114  }
115 
116  setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ) );
117  return true;
118 }
119 
120 bool QgsVectorTileLayer::setupArcgisVectorTileServiceConnection( const QString &uri, const QgsDataSourceUri &dataSourceUri )
121 {
122  QNetworkRequest request = QNetworkRequest( QUrl( uri ) );
123 
124  QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsVectorTileLayer" ) );
125 
126  QgsBlockingNetworkRequest networkRequest;
127  switch ( networkRequest.get( request ) )
128  {
130  break;
131 
135  return false;
136  }
137 
138  const QgsNetworkReplyContent content = networkRequest.reply();
139  const QByteArray raw = content.content();
140 
141  // Parse data
142  QJsonParseError err;
143  QJsonDocument doc = QJsonDocument::fromJson( raw, &err );
144  if ( doc.isNull() )
145  {
146  return false;
147  }
148  mArcgisLayerConfiguration = doc.object().toVariantMap();
149  if ( mArcgisLayerConfiguration.contains( QStringLiteral( "error" ) ) )
150  {
151  return false;
152  }
153 
154  mArcgisLayerConfiguration.insert( QStringLiteral( "serviceUri" ), uri );
155  mSourcePath = uri + '/' + mArcgisLayerConfiguration.value( QStringLiteral( "tiles" ) ).toList().value( 0 ).toString();
156  if ( !QgsVectorTileUtils::checkXYZUrlTemplate( mSourcePath ) )
157  {
158  QgsDebugMsg( QStringLiteral( "Invalid format of URL for XYZ source: " ) + mSourcePath );
159  return false;
160  }
161 
162  // if hardcoded zoom limits aren't specified, take them from the server
163  if ( !dataSourceUri.hasParam( QStringLiteral( "zmin" ) ) )
164  mSourceMinZoom = 0;
165  else
166  mSourceMinZoom = dataSourceUri.param( QStringLiteral( "zmin" ) ).toInt();
167 
168  if ( !dataSourceUri.hasParam( QStringLiteral( "zmax" ) ) )
169  mSourceMaxZoom = mArcgisLayerConfiguration.value( QStringLiteral( "maxzoom" ) ).toInt();
170  else
171  mSourceMaxZoom = dataSourceUri.param( QStringLiteral( "zmax" ) ).toInt();
172 
173  setExtent( QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 ) );
174 
175  return true;
176 }
177 
179 
180 
182 {
183  QgsVectorTileLayer *layer = new QgsVectorTileLayer( source(), name() );
184  layer->setRenderer( renderer() ? renderer()->clone() : nullptr );
185  return layer;
186 }
187 
189 {
190  return new QgsVectorTileLayerRenderer( this, rendererContext );
191 }
192 
193 bool QgsVectorTileLayer::readXml( const QDomNode &layerNode, QgsReadWriteContext &context )
194 {
195  setValid( loadDataSource() );
196 
197  QString errorMsg;
198  if ( !readSymbology( layerNode, errorMsg, context ) )
199  return false;
200 
201  readStyleManager( layerNode );
202  return true;
203 }
204 
205 bool QgsVectorTileLayer::writeXml( QDomNode &layerNode, QDomDocument &doc, const QgsReadWriteContext &context ) const
206 {
207  QDomElement mapLayerNode = layerNode.toElement();
208  mapLayerNode.setAttribute( QStringLiteral( "type" ), QStringLiteral( "vector-tile" ) );
209 
210  writeStyleManager( layerNode, doc );
211 
212  QString errorMsg;
213  return writeSymbology( layerNode, doc, errorMsg, context );
214 }
215 
216 bool QgsVectorTileLayer::readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories )
217 {
218  QDomElement elem = node.toElement();
219 
220  readCommonStyle( elem, context, categories );
221 
222  const QDomElement elemRenderer = elem.firstChildElement( QStringLiteral( "renderer" ) );
223  if ( elemRenderer.isNull() )
224  {
225  errorMessage = tr( "Missing <renderer> tag" );
226  return false;
227  }
228  const QString rendererType = elemRenderer.attribute( QStringLiteral( "type" ) );
229 
230  if ( categories.testFlag( Symbology ) )
231  {
232  QgsVectorTileRenderer *r = nullptr;
233  if ( rendererType == QLatin1String( "basic" ) )
235  else
236  {
237  errorMessage = tr( "Unknown renderer type: " ) + rendererType;
238  return false;
239  }
240 
241  r->readXml( elemRenderer, context );
242  setRenderer( r );
243  }
244 
245  if ( categories.testFlag( Labeling ) )
246  {
247  setLabeling( nullptr );
248  const QDomElement elemLabeling = elem.firstChildElement( QStringLiteral( "labeling" ) );
249  if ( !elemLabeling.isNull() )
250  {
251  const QString labelingType = elemLabeling.attribute( QStringLiteral( "type" ) );
252  QgsVectorTileLabeling *labeling = nullptr;
253  if ( labelingType == QLatin1String( "basic" ) )
255  else
256  {
257  errorMessage = tr( "Unknown labeling type: " ) + rendererType;
258  }
259 
260  if ( labeling )
261  {
262  labeling->readXml( elemLabeling, context );
264  }
265  }
266  }
267 
268  return true;
269 }
270 
271 bool QgsVectorTileLayer::writeSymbology( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories ) const
272 {
273  Q_UNUSED( errorMessage )
274  QDomElement elem = node.toElement();
275 
276  writeCommonStyle( elem, doc, context, categories );
277 
278  if ( mRenderer )
279  {
280  QDomElement elemRenderer = doc.createElement( QStringLiteral( "renderer" ) );
281  elemRenderer.setAttribute( QStringLiteral( "type" ), mRenderer->type() );
282  if ( categories.testFlag( Symbology ) )
283  {
284  mRenderer->writeXml( elemRenderer, context );
285  }
286  elem.appendChild( elemRenderer );
287  }
288 
289  if ( mLabeling && categories.testFlag( Labeling ) )
290  {
291  QDomElement elemLabeling = doc.createElement( QStringLiteral( "labeling" ) );
292  elemLabeling.setAttribute( QStringLiteral( "type" ), mLabeling->type() );
293  mLabeling->writeXml( elemLabeling, context );
294  elem.appendChild( elemLabeling );
295  }
296 
297  return true;
298 }
299 
301 {
302  Q_UNUSED( transformContext )
303 }
304 
305 QString QgsVectorTileLayer::loadDefaultStyle( bool &resultFlag )
306 {
307  QString error;
308  QStringList warnings;
309  resultFlag = loadDefaultStyle( error, warnings );
310  return error;
311 }
312 
313 bool QgsVectorTileLayer::loadDefaultStyle( QString &error, QStringList &warnings )
314 {
315  QgsDataSourceUri dsUri;
316  dsUri.setEncodedUri( mDataSource );
317 
318  QString styleUrl;
319  if ( !dsUri.param( QStringLiteral( "styleUrl" ) ).isEmpty() )
320  {
321  styleUrl = dsUri.param( QStringLiteral( "styleUrl" ) );
322  }
323  else if ( mSourceType == QLatin1String( "xyz" ) && dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
324  {
325  // for ArcMap VectorTileServices we default to the defaultStyles URL from the layer configuration
326  styleUrl = mArcgisLayerConfiguration.value( QStringLiteral( "serviceUri" ) ).toString()
327  + '/' + mArcgisLayerConfiguration.value( QStringLiteral( "defaultStyles" ) ).toString();
328  }
329 
330  if ( !styleUrl.isEmpty() )
331  {
332  QNetworkRequest request = QNetworkRequest( QUrl( styleUrl ) );
333 
334  QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsVectorTileLayer" ) );
335 
336  QgsBlockingNetworkRequest networkRequest;
337  switch ( networkRequest.get( request ) )
338  {
340  break;
341 
345  error = QObject::tr( "Error retrieving default style" );
346  return false;
347  }
348 
349  const QgsNetworkReplyContent content = networkRequest.reply();
350  const QVariantMap styleDefinition = QgsJsonUtils::parseJson( content.content() ).toMap();
351 
353  // convert automatically from pixel sizes to millimeters, because pixel sizes
354  // are a VERY edge case in QGIS and don't play nice with hidpi map renders or print layouts
356  //assume source uses 96 dpi
357  context.setPixelSizeConversionFactor( 25.4 / 96.0 );
358 
359  if ( styleDefinition.contains( QStringLiteral( "sprite" ) ) )
360  {
361  // retrieve sprite definition
362  QString spriteUriBase;
363  if ( styleDefinition.value( QStringLiteral( "sprite" ) ).toString().startsWith( QLatin1String( "http" ) ) )
364  {
365  spriteUriBase = styleDefinition.value( QStringLiteral( "sprite" ) ).toString();
366  }
367  else
368  {
369  spriteUriBase = styleUrl + '/' + styleDefinition.value( QStringLiteral( "sprite" ) ).toString();
370  }
371 
372  for ( int resolution = 2; resolution > 0; resolution-- )
373  {
374  QNetworkRequest request = QNetworkRequest( QUrl( spriteUriBase + QStringLiteral( "%1.json" ).arg( resolution > 1 ? QStringLiteral( "@%1x" ).arg( resolution ) : QString() ) ) );
375  QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsVectorTileLayer" ) );
376  QgsBlockingNetworkRequest networkRequest;
377  switch ( networkRequest.get( request ) )
378  {
380  {
381  const QgsNetworkReplyContent content = networkRequest.reply();
382  const QVariantMap spriteDefinition = QgsJsonUtils::parseJson( content.content() ).toMap();
383 
384  // retrieve sprite images
385  QNetworkRequest request = QNetworkRequest( QUrl( spriteUriBase + QStringLiteral( "%1.png" ).arg( resolution > 1 ? QStringLiteral( "@%1x" ).arg( resolution ) : QString() ) ) );
386 
387  QgsSetRequestInitiatorClass( request, QStringLiteral( "QgsVectorTileLayer" ) );
388 
389  QgsBlockingNetworkRequest networkRequest;
390  switch ( networkRequest.get( request ) )
391  {
393  {
394  const QgsNetworkReplyContent imageContent = networkRequest.reply();
395  QImage spriteImage( QImage::fromData( imageContent.content() ) );
396  context.setSprites( spriteImage, spriteDefinition );
397  break;
398  }
399 
403  break;
404  }
405 
406  break;
407  }
408 
412  break;
413  }
414 
415  if ( !context.spriteDefinitions().isEmpty() )
416  break;
417  }
418  }
419 
420  QgsMapBoxGlStyleConverter converter;
421  if ( converter.convert( styleDefinition, &context ) != QgsMapBoxGlStyleConverter::Success )
422  {
423  warnings = converter.warnings();
424  error = converter.errorMessage();
425  return false;
426  }
427 
428  setRenderer( converter.renderer() );
429  setLabeling( converter.labeling() );
430  warnings = converter.warnings();
431  return true;
432  }
433  else
434  {
435  bool resultFlag = false;
436  error = QgsMapLayer::loadDefaultStyle( resultFlag );
437  return resultFlag;
438  }
439 }
440 
441 QString QgsVectorTileLayer::loadDefaultMetadata( bool &resultFlag )
442 {
443  QgsDataSourceUri dsUri;
444  dsUri.setEncodedUri( mDataSource );
445  if ( mSourceType == QLatin1String( "xyz" ) && dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
446  {
447  // populate default metadata
449  metadata.setIdentifier( mArcgisLayerConfiguration.value( QStringLiteral( "serviceUri" ) ).toString() );
450  const QString parentIdentifier = mArcgisLayerConfiguration.value( QStringLiteral( "serviceItemId" ) ).toString();
451  if ( !parentIdentifier.isEmpty() )
452  {
453  metadata.setParentIdentifier( parentIdentifier );
454  }
455  metadata.setType( QStringLiteral( "dataset" ) );
456  metadata.setTitle( mArcgisLayerConfiguration.value( QStringLiteral( "name" ) ).toString() );
457  QString copyright = mArcgisLayerConfiguration.value( QStringLiteral( "copyrightText" ) ).toString();
458  if ( !copyright.isEmpty() )
459  metadata.setRights( QStringList() << copyright );
460  metadata.addLink( QgsAbstractMetadataBase::Link( tr( "Source" ), QStringLiteral( "WWW:LINK" ), mArcgisLayerConfiguration.value( QStringLiteral( "serviceUri" ) ).toString() ) );
461 
463 
464  resultFlag = true;
465  return QString();
466  }
467  else
468  {
469  QgsMapLayer::loadDefaultMetadata( resultFlag );
470  resultFlag = true;
471  return QString();
472  }
473 }
474 
475 QString QgsVectorTileLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
476 {
477  QgsDataSourceUri dsUri;
478  dsUri.setEncodedUri( source );
479 
480  QString sourceType = dsUri.param( QStringLiteral( "type" ) );
481  QString sourcePath = dsUri.param( QStringLiteral( "url" ) );
482  if ( sourceType == QLatin1String( "xyz" ) )
483  {
484  QUrl sourceUrl( sourcePath );
485  if ( sourceUrl.isLocalFile() )
486  {
487  // relative path will become "file:./x.txt"
488  QString relSrcUrl = context.pathResolver().writePath( sourceUrl.toLocalFile() );
489  dsUri.removeParam( QStringLiteral( "url" ) ); // needed because setParam() would insert second "url" key
490  dsUri.setParam( QStringLiteral( "url" ), QUrl::fromLocalFile( relSrcUrl ).toString() );
491  return dsUri.encodedUri();
492  }
493  }
494  else if ( sourceType == QLatin1String( "mbtiles" ) )
495  {
497  dsUri.removeParam( QStringLiteral( "url" ) ); // needed because setParam() would insert second "url" key
498  dsUri.setParam( QStringLiteral( "url" ), sourcePath );
499  return dsUri.encodedUri();
500  }
501 
502  return source;
503 }
504 
505 QString QgsVectorTileLayer::decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const
506 {
507  Q_UNUSED( provider )
508 
509  QgsDataSourceUri dsUri;
510  dsUri.setEncodedUri( source );
511 
512  QString sourceType = dsUri.param( QStringLiteral( "type" ) );
513  QString sourcePath = dsUri.param( QStringLiteral( "url" ) );
514  if ( sourceType == QLatin1String( "xyz" ) )
515  {
516  QUrl sourceUrl( sourcePath );
517  if ( sourceUrl.isLocalFile() ) // file-based URL? convert to relative path
518  {
519  QString absSrcUrl = context.pathResolver().readPath( sourceUrl.toLocalFile() );
520  dsUri.removeParam( QStringLiteral( "url" ) ); // needed because setParam() would insert second "url" key
521  dsUri.setParam( QStringLiteral( "url" ), QUrl::fromLocalFile( absSrcUrl ).toString() );
522  return dsUri.encodedUri();
523  }
524  }
525  else if ( sourceType == QLatin1String( "mbtiles" ) )
526  {
528  dsUri.removeParam( QStringLiteral( "url" ) ); // needed because setParam() would insert second "url" key
529  dsUri.setParam( QStringLiteral( "url" ), sourcePath );
530  return dsUri.encodedUri();
531  }
532 
533  return source;
534 }
535 
537 {
538  QgsLayerMetadataFormatter htmlFormatter( metadata() );
539 
540  QString info = QStringLiteral( "<html><head></head>\n<body>\n" );
541 
542  info += QStringLiteral( "<h1>" ) + tr( "Information from provider" ) + QStringLiteral( "</h1>\n<hr>\n" ) %
543  QStringLiteral( "<table class=\"list-view\">\n" ) %
544 
545  // name
546  QStringLiteral( "<tr><td class=\"highlight\">" ) % tr( "Name" ) % QStringLiteral( "</td><td>" ) % name() % QStringLiteral( "</td></tr>\n" );
547 
548  info += QStringLiteral( "<tr><td class=\"highlight\">" ) % tr( "URI" ) % QStringLiteral( "</td><td>" ) % source() % QStringLiteral( "</td></tr>\n" );
549  info += QStringLiteral( "<tr><td class=\"highlight\">" ) % tr( "Source type" ) % QStringLiteral( "</td><td>" ) % sourceType() % QStringLiteral( "</td></tr>\n" );
550 
551  const QString url = sourcePath();
552  info += QStringLiteral( "<tr><td class=\"highlight\">" ) % tr( "Source path" ) % QStringLiteral( "</td><td>%1" ).arg( QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( QUrl( url ).toString(), sourcePath() ) ) + QStringLiteral( "</td></tr>\n" );
553 
554  info += QStringLiteral( "<tr><td class=\"highlight\">" ) % tr( "Zoom levels" ) % QStringLiteral( "</td><td>" ) % QStringLiteral( "%1 - %2" ).arg( sourceMinZoom() ).arg( sourceMaxZoom() ) % QStringLiteral( "</td></tr>\n" );
555  info += QLatin1String( "</table>" );
556 
557  // End Provider section
558  info += QLatin1String( "</table>\n<br><br>" );
559 
560  // Identification section
561  info += QStringLiteral( "<h1>" ) % tr( "Identification" ) % QStringLiteral( "</h1>\n<hr>\n" ) %
562  htmlFormatter.identificationSectionHtml() %
563  QStringLiteral( "<br><br>\n" ) %
564 
565  // extent section
566  QStringLiteral( "<h1>" ) % tr( "Extent" ) % QStringLiteral( "</h1>\n<hr>\n" ) %
567  htmlFormatter.extentSectionHtml( ) %
568  QStringLiteral( "<br><br>\n" ) %
569 
570  // Start the Access section
571  QStringLiteral( "<h1>" ) % tr( "Access" ) % QStringLiteral( "</h1>\n<hr>\n" ) %
572  htmlFormatter.accessSectionHtml( ) %
573  QStringLiteral( "<br><br>\n" ) %
574 
575 
576  // Start the contacts section
577  QStringLiteral( "<h1>" ) % tr( "Contacts" ) % QStringLiteral( "</h1>\n<hr>\n" ) %
578  htmlFormatter.contactsSectionHtml( ) %
579  QStringLiteral( "<br><br>\n" ) %
580 
581  // Start the links section
582  QStringLiteral( "<h1>" ) % tr( "References" ) % QStringLiteral( "</h1>\n<hr>\n" ) %
583  htmlFormatter.linksSectionHtml( ) %
584  QStringLiteral( "<br><br>\n" ) %
585 
586  // Start the history section
587  QStringLiteral( "<h1>" ) % tr( "History" ) % QStringLiteral( "</h1>\n<hr>\n" ) %
588  htmlFormatter.historySectionHtml( ) %
589  QStringLiteral( "<br><br>\n" ) %
590 
591  QStringLiteral( "\n</body>\n</html>\n" );
592 
593  return info;
594 }
595 
597 {
598  QgsTileMatrix tileMatrix = QgsTileMatrix::fromWebMercator( tileID.zoomLevel() );
599  QgsTileRange tileRange( tileID.column(), tileID.column(), tileID.row(), tileID.row() );
600 
601  QgsDataSourceUri dsUri;
602  dsUri.setEncodedUri( mDataSource );
603  const QString authcfg = dsUri.authConfigId();
604  const QString referer = dsUri.param( QStringLiteral( "referer" ) );
605 
606  QList<QgsVectorTileRawData> rawTiles = QgsVectorTileLoader::blockingFetchTileRawData( mSourceType, mSourcePath, tileMatrix, QPointF(), tileRange, authcfg, referer );
607  if ( rawTiles.isEmpty() )
608  return QByteArray();
609  return rawTiles.first().data;
610 }
611 
613 {
614  mRenderer.reset( r );
615  triggerRepaint();
616 }
617 
619 {
620  return mRenderer.get();
621 }
622 
624 {
625  mLabeling.reset( labeling );
626  triggerRepaint();
627 }
628 
630 {
631  return mLabeling.get();
632 }
QgsMapBoxGlStyleConversionContext::setTargetUnit
void setTargetUnit(QgsUnitTypes::RenderUnit targetUnit)
Sets the target unit type.
Definition: qgsmapboxglstyleconverter.cpp:2706
QgsVectorTileLayer::loadDefaultMetadata
QString loadDefaultMetadata(bool &resultFlag) override
Retrieve the default metadata for this layer if one exists (either as a .qmd file on disk or as a rec...
Definition: qgsvectortilelayer.cpp:441
QgsMapLayer::readCommonStyle
void readCommonStyle(const QDomElement &layerElement, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)
Read style data common to all layer types.
Definition: qgsmaplayer.cpp:1622
QgsMapBoxGlStyleConverter::renderer
QgsVectorTileRenderer * renderer() const
Returns a new instance of a vector tile renderer representing the converted style,...
Definition: qgsmapboxglstyleconverter.cpp:2682
QgsVectorTileLayer::QgsVectorTileLayer
QgsVectorTileLayer(const QString &path=QString(), const QString &baseName=QString())
Constructs a new vector tile layer.
Definition: qgsvectortilelayer.cpp:34
QgsDataSourceUri
Class for storing the component parts of a RDBMS data source URI (e.g.
Definition: qgsdatasourceuri.h:36
QgsLayerMetadata::setRights
void setRights(const QStringList &rights)
Sets a list of rights (attribution or copyright strings) associated with the resource.
Definition: qgslayermetadata.cpp:56
QgsVectorTileLayer
Implements a map layer that is dedicated to rendering of vector tiles.
Definition: qgsvectortilelayer.h:84
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:58
QgsVectorTileLayer::sourceType
QString sourceType() const
Returns type of the data source.
Definition: qgsvectortilelayer.h:133
QgsMapLayer::Labeling
@ Labeling
Labeling.
Definition: qgsmaplayer.h:165
QgsTileXYZ
Stores coordinates of a tile in a tile matrix set.
Definition: qgstiles.h:33
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsTileRange
Range of tiles in a tile matrix to be rendered.
Definition: qgstiles.h:66
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsLayerMetadataFormatter::historySectionHtml
QString historySectionHtml() const
Formats the "History" section according to a metadata object.
Definition: qgslayermetadataformatter.cpp:230
QgsLayerMetadataFormatter
Class for metadata formatter.
Definition: qgslayermetadataformatter.h:34
qgsblockingnetworkrequest.h
qgsvectortilelabeling.h
QgsLayerMetadata
A structured metadata store for a map layer.
Definition: qgslayermetadata.h:57
QgsMapLayerType
QgsMapLayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:68
QgsVectorTileLayer::setRenderer
void setRenderer(QgsVectorTileRenderer *r)
Sets renderer for the map layer.
Definition: qgsvectortilelayer.cpp:612
QgsBlockingNetworkRequest::reply
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get() or post() request has been made.
Definition: qgsblockingnetworkrequest.h:117
QgsVectorTileBasicRenderer
The default vector tile renderer implementation.
Definition: qgsvectortilebasicrenderer.h:128
QgsVectorTileLoader::blockingFetchTileRawData
static QList< QgsVectorTileRawData > blockingFetchTileRawData(const QString &sourceType, const QString &sourcePath, const QgsTileMatrix &tileMatrix, const QPointF &viewCenter, const QgsTileRange &range, const QString &authid, const QString &referer)
Returns raw tile data for the specified range of tiles. Blocks the caller until all tiles are fetched...
Definition: qgsvectortileloader.cpp:159
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsVectorTileBasicLabeling
Basic labeling configuration for vector tile layers.
Definition: qgsvectortilebasiclabeling.h:108
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
QgsAbstractMetadataBase::addLink
void addLink(const QgsAbstractMetadataBase::Link &link)
Adds an individual link to the existing links.
Definition: qgsabstractmetadatabase.cpp:158
QgsVectorTileLayer::~QgsVectorTileLayer
~QgsVectorTileLayer() override
QgsLayerMetadataFormatter::contactsSectionHtml
QString contactsSectionHtml() const
Formats the "Contacts" section according to a metadata object.
Definition: qgslayermetadataformatter.cpp:51
QgsVectorTileLayerRenderer
This class provides map rendering functionality for vector tile layers.
Definition: qgsvectortilelayerrenderer.h:42
QgsAbstractMetadataBase::setTitle
void setTitle(const QString &title)
Sets the human readable title (name) of the resource, typically displayed in search results.
Definition: qgsabstractmetadatabase.cpp:56
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsMapLayer::setMetadata
virtual void setMetadata(const QgsLayerMetadata &metadata)
Sets the layer's metadata store.
Definition: qgsmaplayer.cpp:1836
QgsMapLayer::setCrs
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer's spatial reference system.
Definition: qgsmaplayer.cpp:771
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsVectorTileLayer::clone
QgsVectorTileLayer * clone() const override
Returns a new instance equivalent to this one except for the id which is still unique.
Definition: qgsvectortilelayer.cpp:181
QgsSetRequestInitiatorClass
#define QgsSetRequestInitiatorClass(request, _class)
Definition: qgsnetworkaccessmanager.h:41
QgsMapBoxGlStyleConverter::errorMessage
QString errorMessage() const
Returns a descriptive error message if an error was encountered during the style conversion,...
Definition: qgsmapboxglstyleconverter.h:226
QgsBlockingNetworkRequest::ServerExceptionError
@ ServerExceptionError
An exception was raised by the server.
Definition: qgsblockingnetworkrequest.h:57
qgsmapboxglstyleconverter.h
QgsMapLayerRenderer
Base class for utility classes that encapsulate information necessary for rendering of map layers.
Definition: qgsmaplayerrenderer.h:51
qgsmbtiles.h
QgsMapBoxGlStyleConverter::convert
Result convert(const QVariantMap &style, QgsMapBoxGlStyleConversionContext *context=nullptr)
Converts a JSON style map, and returns the resultant status of the conversion.
Definition: qgsmapboxglstyleconverter.cpp:42
QgsLayerMetadataFormatter::linksSectionHtml
QString linksSectionHtml() const
Formats the "Links" section according to a metadata object.
Definition: qgslayermetadataformatter.cpp:256
QgsVectorTileLayer::encodedSource
QString encodedSource(const QString &source, const QgsReadWriteContext &context) const FINAL
Called by writeLayerXML(), used by derived classes to encode provider's specific data source to proje...
Definition: qgsvectortilelayer.cpp:475
QgsMapBoxGlStyleConverter::Success
@ Success
Conversion was successful.
Definition: qgsmapboxglstyleconverter.h:190
qgsvectortilelayer.h
QgsDataSourceUri::param
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
Definition: qgsdatasourceuri.cpp:823
QgsMapBoxGlStyleConversionContext
Context for a MapBox GL style conversion operation.
Definition: qgsmapboxglstyleconverter.h:37
QgsMapLayer::triggerRepaint
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
Definition: qgsmaplayer.cpp:1826
QgsVectorTileLayer::sourcePath
QString sourcePath() const
Returns URL/path of the data source (syntax different to each data source type)
Definition: qgsvectortilelayer.h:135
QgsPathResolver::writePath
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
Definition: qgspathresolver.cpp:192
QgsTileMatrix
Defines a matrix of tiles for a single zoom level: it is defined by its size (width * height) and map...
Definition: qgstiles.h:103
QgsMbTiles
Utility class for reading and writing MBTiles files (which are SQLite3 databases).
Definition: qgsmbtiles.h:39
QgsVectorTileLayer::createMapRenderer
QgsMapLayerRenderer * createMapRenderer(QgsRenderContext &rendererContext) override
Returns new instance of QgsMapLayerRenderer that will be used for rendering of given context.
Definition: qgsvectortilelayer.cpp:188
QgsVectorTileUtils::checkXYZUrlTemplate
static bool checkXYZUrlTemplate(const QString &url)
Checks whether the URL template string is correct (contains {x}, {y} / {-y}, {z} placeholders)
Definition: qgsvectortileutils.cpp:141
QgsMapLayer::setExtent
virtual void setExtent(const QgsRectangle &rect)
Sets the extent.
Definition: qgsmaplayer.cpp:1858
qgsdatasourceuri.h
QgsVectorTileRenderer
Abstract base class for all vector tile renderer implementations.
Definition: qgsvectortilerenderer.h:89
QgsAbstractMetadataBase::setParentIdentifier
void setParentIdentifier(const QString &parentIdentifier)
Sets a reference, URI, URL or some other mechanism to identify the parent resource that this resource...
Definition: qgsabstractmetadatabase.cpp:36
QgsMapLayer::readStyleManager
void readStyleManager(const QDomNode &layerNode)
Read style manager's configuration (if any). To be called by subclasses.
Definition: qgsmaplayer.cpp:639
QgsMapLayer::writeCommonStyle
void writeCommonStyle(QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const
Write style data common to all layer types.
Definition: qgsmaplayer.cpp:540
QgsVectorTileRenderer::readXml
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)=0
Reads renderer's properties from given XML element.
QgsVectorTileLayer::renderer
QgsVectorTileRenderer * renderer() const
Returns currently assigned renderer.
Definition: qgsvectortilelayer.cpp:618
qgsnetworkaccessmanager.h
QgsAbstractMetadataBase::setIdentifier
void setIdentifier(const QString &identifier)
Sets the reference, URI, URL or some other mechanism to identify the resource.
Definition: qgsabstractmetadatabase.cpp:26
QgsMapBoxGlStyleConversionContext::setSprites
void setSprites(const QImage &image, const QVariantMap &definitions)
Sets the sprite image and definitions JSON to use during conversion.
Definition: qgsmapboxglstyleconverter.cpp:2731
QgsDataSourceUri::setParam
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
Definition: qgsdatasourceuri.cpp:778
QgsMapLayer::loadDefaultStyle
virtual QString loadDefaultStyle(bool &resultFlag)
Retrieve the default style for this layer if one exists (either as a .qml file on disk or as a record...
Definition: qgsmaplayer.cpp:862
QgsBlockingNetworkRequest::NoError
@ NoError
No error was encountered.
Definition: qgsblockingnetworkrequest.h:54
QgsMapBoxGlStyleConversionContext::spriteDefinitions
QVariantMap spriteDefinitions() const
Returns the sprite definitions to use during conversion.
Definition: qgsmapboxglstyleconverter.cpp:2726
QgsVectorTileLayer::loadDefaultStyle
QString loadDefaultStyle(bool &resultFlag) override
Retrieve the default style for this layer if one exists (either as a .qml file on disk or as a record...
Definition: qgsvectortilelayer.cpp:305
QgsVectorTileLayer::setLabeling
void setLabeling(QgsVectorTileLabeling *labeling)
Sets labeling for the map layer.
Definition: qgsvectortilelayer.cpp:623
QgsMapLayer::mDataSource
QString mDataSource
Data source description string, varies by layer type.
Definition: qgsmaplayer.h:1538
QgsDataSourceUri::removeParam
int removeParam(const QString &key)
Removes a generic parameter by key.
Definition: qgsdatasourceuri.cpp:802
QgsVectorTileLayer::writeXml
bool writeXml(QDomNode &layerNode, QDomDocument &doc, const QgsReadWriteContext &context) const override
Called by writeLayerXML(), used by children to write state specific to them to project files.
Definition: qgsvectortilelayer.cpp:205
QgsTileXYZ::zoomLevel
int zoomLevel() const
Returns tile's zoom level (Z)
Definition: qgstiles.h:46
QgsMapBoxGlStyleConverter::labeling
QgsVectorTileLabeling * labeling() const
Returns a new instance of a vector tile labeling representing the converted style,...
Definition: qgsmapboxglstyleconverter.cpp:2687
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
qgsvectortileloader.h
QgsDataSourceUri::hasParam
bool hasParam(const QString &key) const
Returns true if a parameter with the specified key exists.
Definition: qgsdatasourceuri.cpp:849
QgsBlockingNetworkRequest::TimeoutError
@ TimeoutError
Timeout was reached before a reply was received.
Definition: qgsblockingnetworkrequest.h:56
QgsMapBoxGlStyleConversionContext::setPixelSizeConversionFactor
void setPixelSizeConversionFactor(double sizeConversionFactor)
Sets the pixel size conversion factor, used to scale the original pixel sizes when converting styles.
Definition: qgsmapboxglstyleconverter.cpp:2716
QgsVectorTileLayer::sourceMaxZoom
int sourceMaxZoom() const
Returns maximum zoom level at which source has any valid tiles (negative = unconstrained)
Definition: qgsvectortilelayer.h:140
QgsPathResolver::readPath
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
Definition: qgspathresolver.cpp:35
QgsMapLayer::source
QString source() const
Returns the source for the layer.
Definition: qgsmaplayer.cpp:192
QgsBlockingNetworkRequest::get
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "get" operation on the specified request.
Definition: qgsblockingnetworkrequest.cpp:57
QgsMapLayer::writeStyleManager
void writeStyleManager(QDomNode &layerNode, QDomDocument &doc) const
Write style manager's configuration (if exists). To be called by subclasses.
Definition: qgsmaplayer.cpp:648
QgsDataSourceUri::encodedUri
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
Definition: qgsdatasourceuri.cpp:610
QgsMapLayer::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the layer data provider coordinate transform context or a default transform context if the la...
Definition: qgsmaplayer.cpp:785
QgsDataSourceUri::setEncodedUri
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
Definition: qgsdatasourceuri.cpp:630
QgsVectorTileLayer::readSymbology
bool readSymbology(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) override
Read the symbology for the current layer from the DOM node supplied.
Definition: qgsvectortilelayer.cpp:216
QgsVectorTileLayer::labeling
QgsVectorTileLabeling * labeling() const
Returns currently assigned labeling.
Definition: qgsvectortilelayer.cpp:629
QgsTileXYZ::row
int row() const
Returns tile's row index (Y)
Definition: qgstiles.h:44
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
qgsvectortilelayerrenderer.h
QgsMapLayer::Symbology
@ Symbology
Symbology.
Definition: qgsmaplayer.h:163
QgsMapLayer::loadDefaultMetadata
virtual QString loadDefaultMetadata(bool &resultFlag)
Retrieve the default metadata for this layer if one exists (either as a .qmd file on disk or as a rec...
Definition: qgsmaplayer.cpp:852
qgslayermetadataformatter.h
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:86
qgsvectortilebasicrenderer.h
QgsVectorTileLayer::getRawTile
QByteArray getRawTile(QgsTileXYZ tileID)
Fetches raw tile data for the give tile coordinates.
Definition: qgsvectortilelayer.cpp:596
QgsVectorTileLayer::decodedSource
QString decodedSource(const QString &source, const QString &provider, const QgsReadWriteContext &context) const FINAL
Called by readLayerXML(), used by derived classes to decode provider's specific data source from proj...
Definition: qgsvectortilelayer.cpp:505
QgsNetworkReplyContent::content
QByteArray content() const
Returns the reply content.
Definition: qgsnetworkreply.h:158
QgsNetworkReplyContent
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
Definition: qgsnetworkreply.h:29
QgsTileMatrix::fromWebMercator
static QgsTileMatrix fromWebMercator(int mZoomLevel)
Returns a tile matrix for the usual web mercator.
Definition: qgstiles.cpp:20
QgsVectorTileLayer::setTransformContext
void setTransformContext(const QgsCoordinateTransformContext &transformContext) override
Sets the coordinate transform context to transformContext.
Definition: qgsvectortilelayer.cpp:300
QgsVectorTileLabeling
Base class for labeling configuration classes for vector tile layers.
Definition: qgsvectortilelabeling.h:71
QgsVectorTileLayer::sourceMinZoom
int sourceMinZoom() const
Returns minimum zoom level at which source has any valid tiles (negative = unconstrained)
Definition: qgsvectortilelayer.h:138
QgsAbstractMetadataBase::setType
void setType(const QString &type)
Sets the type (nature) of the resource.
Definition: qgsabstractmetadatabase.cpp:46
QgsVectorTileLayer::readXml
bool readXml(const QDomNode &layerNode, QgsReadWriteContext &context) override
Called by readLayerXML(), used by children to read state specific to them from project files.
Definition: qgsvectortilelayer.cpp:193
QgsVectorTileBasicRenderer::simpleStyleWithRandomColors
static QList< QgsVectorTileBasicRendererStyle > simpleStyleWithRandomColors()
Returns a list of styles to render all layers, using random colors.
Definition: qgsvectortilebasicrenderer.cpp:275
QgsMapBoxGlStyleConverter::warnings
QStringList warnings() const
Returns a list of user-friendly warnings generated during the conversion, e.g.
Definition: qgsmapboxglstyleconverter.h:234
QgsDataSourceUri::authConfigId
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
Definition: qgsdatasourceuri.cpp:254
QgsBlockingNetworkRequest::NetworkError
@ NetworkError
A network error occurred.
Definition: qgsblockingnetworkrequest.h:55
qgsjsonutils.h
qgslogger.h
QgsLayerMetadataFormatter::extentSectionHtml
QString extentSectionHtml(const bool showSpatialExtent=true) const
Formats the "Extents" section according to a metadata object (extent and temporal).
Definition: qgslayermetadataformatter.cpp:111
qgsvectortileutils.h
QgsMapLayer::setValid
void setValid(bool valid)
Sets whether layer is valid or not.
Definition: qgsmaplayer.cpp:1775
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:53
qgsvectortilebasiclabeling.h
QgsLayerMetadataFormatter::accessSectionHtml
QString accessSectionHtml() const
Formats the "Access" section according to a metadata object.
Definition: qgslayermetadataformatter.cpp:27
QgsVectorTileLayer::htmlMetadata
QString htmlMetadata() const override
Obtain a formatted HTML string containing assorted metadata for this layer.
Definition: qgsvectortilelayer.cpp:536
QgsVectorTileLabeling::readXml
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)=0
Reads labeling properties from given XML element.
QgsBlockingNetworkRequest
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
Definition: qgsblockingnetworkrequest.h:47
QgsMapLayer::metadata
QgsLayerMetadata metadata
Definition: qgsmaplayer.h:88
QgsLayerMetadataFormatter::identificationSectionHtml
QString identificationSectionHtml() const
Formats the "Identification" section according to a metadata object.
Definition: qgslayermetadataformatter.cpp:181
QgsMapBoxGlStyleConverter
Handles conversion of MapBox GL styles to QGIS vector tile renderers and labeling settings.
Definition: qgsmapboxglstyleconverter.h:172
QgsVectorTileLayer::writeSymbology
bool writeSymbology(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const override
Write the style for the layer into the docment provided.
Definition: qgsvectortilelayer.cpp:271
QgsReadWriteContext::pathResolver
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
Definition: qgsreadwritecontext.cpp:47
QgsTileXYZ::column
int column() const
Returns tile's column index (X)
Definition: qgstiles.h:42
QgsMapLayer::error
virtual QgsError error() const
Gets current status error.
Definition: qgsmaplayer.cpp:1733
QgsJsonUtils::parseJson
static QVariant parseJson(const std::string &jsonString)
Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned.
Definition: qgsjsonutils.cpp:455