QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
47 bool QgsVectorTileLayer::loadDataSource()
52 mSourceType = dsUri.
param( QStringLiteral(
"type" ) );
53 mSourcePath = dsUri.
param( QStringLiteral(
"url" ) );
54 if ( mSourceType == QLatin1String(
"xyz" ) && dsUri.
param( QStringLiteral(
"serviceType" ) ) == QLatin1String(
"arcgis" ) )
56 if ( !setupArcgisVectorTileServiceConnection( mSourcePath, dsUri ) )
59 else if ( mSourceType == QLatin1String(
"xyz" ) )
63 QgsDebugMsg( QStringLiteral(
"Invalid format of URL for XYZ source: " ) + mSourcePath );
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();
76 setExtent(
QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 ) );
78 else if ( mSourceType == QLatin1String(
"mbtiles" ) )
83 QgsDebugMsg( QStringLiteral(
"failed to open MBTiles file: " ) + mSourcePath );
87 QString format = reader.metadataValue( QStringLiteral(
"format" ) );
88 if ( format != QLatin1String(
"pbf" ) )
90 QgsDebugMsg( QStringLiteral(
"Cannot open MBTiles for vector tiles. Format = " ) + format );
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 );
99 mSourceMinZoom = minZoom;
101 mSourceMaxZoom = maxZoom;
102 QgsDebugMsgLevel( QStringLiteral(
"zoom range: %1 - %2" ).arg( mSourceMinZoom ).arg( mSourceMaxZoom ), 2 );
107 r = ct.transformBoundingBox( r );
112 QgsDebugMsg( QStringLiteral(
"Unknown source type: " ) + mSourceType );
120 bool QgsVectorTileLayer::setupArcgisVectorTileServiceConnection(
const QString &uri,
const QgsDataSourceUri &dataSourceUri )
122 QNetworkRequest request = QNetworkRequest( QUrl( uri ) );
127 switch ( networkRequest.
get( request ) )
139 const QByteArray raw = content.
content();
143 QJsonDocument doc = QJsonDocument::fromJson( raw, &err );
148 mArcgisLayerConfiguration = doc.object().toVariantMap();
149 if ( mArcgisLayerConfiguration.contains( QStringLiteral(
"error" ) ) )
154 mArcgisLayerConfiguration.insert( QStringLiteral(
"serviceUri" ), uri );
155 mSourcePath = uri +
'/' + mArcgisLayerConfiguration.value( QStringLiteral(
"tiles" ) ).toList().value( 0 ).toString();
158 QgsDebugMsg( QStringLiteral(
"Invalid format of URL for XYZ source: " ) + mSourcePath );
163 if ( !dataSourceUri.
hasParam( QStringLiteral(
"zmin" ) ) )
166 mSourceMinZoom = dataSourceUri.
param( QStringLiteral(
"zmin" ) ).toInt();
168 if ( !dataSourceUri.
hasParam( QStringLiteral(
"zmax" ) ) )
169 mSourceMaxZoom = mArcgisLayerConfiguration.value( QStringLiteral(
"maxzoom" ) ).toInt();
171 mSourceMaxZoom = dataSourceUri.
param( QStringLiteral(
"zmax" ) ).toInt();
173 setExtent(
QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 ) );
207 QDomElement mapLayerNode = layerNode.toElement();
208 mapLayerNode.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"vector-tile" ) );
218 QDomElement elem = node.toElement();
222 const QDomElement elemRenderer = elem.firstChildElement( QStringLiteral(
"renderer" ) );
223 if ( elemRenderer.isNull() )
225 errorMessage = tr(
"Missing <renderer> tag" );
228 const QString rendererType = elemRenderer.attribute( QStringLiteral(
"type" ) );
233 if ( rendererType == QLatin1String(
"basic" ) )
237 errorMessage = tr(
"Unknown renderer type: " ) + rendererType;
241 r->
readXml( elemRenderer, context );
245 if ( categories.testFlag(
Labeling ) )
248 const QDomElement elemLabeling = elem.firstChildElement( QStringLiteral(
"labeling" ) );
249 if ( !elemLabeling.isNull() )
251 const QString labelingType = elemLabeling.attribute( QStringLiteral(
"type" ) );
253 if ( labelingType == QLatin1String(
"basic" ) )
257 errorMessage = tr(
"Unknown labeling type: " ) + rendererType;
273 Q_UNUSED( errorMessage )
274 QDomElement elem = node.toElement();
280 QDomElement elemRenderer = doc.createElement( QStringLiteral(
"renderer" ) );
281 elemRenderer.setAttribute( QStringLiteral(
"type" ), mRenderer->type() );
284 mRenderer->writeXml( elemRenderer, context );
286 elem.appendChild( elemRenderer );
289 if ( mLabeling && categories.testFlag(
Labeling ) )
291 QDomElement elemLabeling = doc.createElement( QStringLiteral(
"labeling" ) );
292 elemLabeling.setAttribute( QStringLiteral(
"type" ), mLabeling->type() );
293 mLabeling->writeXml( elemLabeling, context );
294 elem.appendChild( elemLabeling );
308 QStringList warnings;
319 if ( !dsUri.
param( QStringLiteral(
"styleUrl" ) ).isEmpty() )
321 styleUrl = dsUri.
param( QStringLiteral(
"styleUrl" ) );
323 else if ( mSourceType == QLatin1String(
"xyz" ) && dsUri.
param( QStringLiteral(
"serviceType" ) ) == QLatin1String(
"arcgis" ) )
326 styleUrl = mArcgisLayerConfiguration.value( QStringLiteral(
"serviceUri" ) ).toString()
327 +
'/' + mArcgisLayerConfiguration.value( QStringLiteral(
"defaultStyles" ) ).toString();
330 if ( !styleUrl.isEmpty() )
332 QNetworkRequest request = QNetworkRequest( QUrl( styleUrl ) );
337 switch ( networkRequest.
get( request ) )
345 error = QObject::tr(
"Error retrieving default style" );
359 if ( styleDefinition.contains( QStringLiteral(
"sprite" ) ) )
362 QString spriteUriBase;
363 if ( styleDefinition.value( QStringLiteral(
"sprite" ) ).toString().startsWith( QLatin1String(
"http" ) ) )
365 spriteUriBase = styleDefinition.value( QStringLiteral(
"sprite" ) ).toString();
369 spriteUriBase = styleUrl +
'/' + styleDefinition.value( QStringLiteral(
"sprite" ) ).toString();
372 for (
int resolution = 2; resolution > 0; resolution-- )
374 QNetworkRequest request = QNetworkRequest( QUrl( spriteUriBase + QStringLiteral(
"%1.json" ).arg( resolution > 1 ? QStringLiteral(
"@%1x" ).arg( resolution ) : QString() ) ) );
377 switch ( networkRequest.
get( request ) )
385 QNetworkRequest request = QNetworkRequest( QUrl( spriteUriBase + QStringLiteral(
"%1.png" ).arg( resolution > 1 ? QStringLiteral(
"@%1x" ).arg( resolution ) : QString() ) ) );
390 switch ( networkRequest.
get( request ) )
395 QImage spriteImage( QImage::fromData( imageContent.
content() ) );
396 context.
setSprites( spriteImage, spriteDefinition );
435 bool resultFlag =
false;
445 if ( mSourceType == QLatin1String(
"xyz" ) && dsUri.
param( QStringLiteral(
"serviceType" ) ) == QLatin1String(
"arcgis" ) )
449 metadata.
setIdentifier( mArcgisLayerConfiguration.value( QStringLiteral(
"serviceUri" ) ).toString() );
450 const QString parentIdentifier = mArcgisLayerConfiguration.value( QStringLiteral(
"serviceItemId" ) ).toString();
451 if ( !parentIdentifier.isEmpty() )
456 metadata.
setTitle( mArcgisLayerConfiguration.value( QStringLiteral(
"name" ) ).toString() );
457 QString copyright = mArcgisLayerConfiguration.value( QStringLiteral(
"copyrightText" ) ).toString();
458 if ( !copyright.isEmpty() )
485 if ( sourceUrl.isLocalFile() )
490 dsUri.
setParam( QStringLiteral(
"url" ), QUrl::fromLocalFile( relSrcUrl ).toString() );
494 else if (
sourceType == QLatin1String(
"mbtiles" ) )
517 if ( sourceUrl.isLocalFile() )
521 dsUri.
setParam( QStringLiteral(
"url" ), QUrl::fromLocalFile( absSrcUrl ).toString() );
525 else if (
sourceType == QLatin1String(
"mbtiles" ) )
540 QString info = QStringLiteral(
"<html><head></head>\n<body>\n" );
542 info += QStringLiteral(
"<h1>" ) + tr(
"Information from provider" ) + QStringLiteral(
"</h1>\n<hr>\n" ) %
543 QStringLiteral(
"<table class=\"list-view\">\n" ) %
546 QStringLiteral(
"<tr><td class=\"highlight\">" ) % tr(
"Name" ) % QStringLiteral(
"</td><td>" ) %
name() % QStringLiteral(
"</td></tr>\n" );
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" );
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" );
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>" );
558 info += QLatin1String(
"</table>\n<br><br>" );
561 info += QStringLiteral(
"<h1>" ) % tr(
"Identification" ) % QStringLiteral(
"</h1>\n<hr>\n" ) %
563 QStringLiteral(
"<br><br>\n" ) %
566 QStringLiteral(
"<h1>" ) % tr(
"Extent" ) % QStringLiteral(
"</h1>\n<hr>\n" ) %
568 QStringLiteral(
"<br><br>\n" ) %
571 QStringLiteral(
"<h1>" ) % tr(
"Access" ) % QStringLiteral(
"</h1>\n<hr>\n" ) %
573 QStringLiteral(
"<br><br>\n" ) %
577 QStringLiteral(
"<h1>" ) % tr(
"Contacts" ) % QStringLiteral(
"</h1>\n<hr>\n" ) %
579 QStringLiteral(
"<br><br>\n" ) %
582 QStringLiteral(
"<h1>" ) % tr(
"References" ) % QStringLiteral(
"</h1>\n<hr>\n" ) %
584 QStringLiteral(
"<br><br>\n" ) %
587 QStringLiteral(
"<h1>" ) % tr(
"History" ) % QStringLiteral(
"</h1>\n<hr>\n" ) %
589 QStringLiteral(
"<br><br>\n" ) %
591 QStringLiteral(
"\n</body>\n</html>\n" );
604 const QString referer = dsUri.
param( QStringLiteral(
"referer" ) );
607 if ( rawTiles.isEmpty() )
609 return rawTiles.first().data;
614 mRenderer.reset( r );
620 return mRenderer.get();
631 return mLabeling.get();
void setTargetUnit(QgsUnitTypes::RenderUnit targetUnit)
Sets the target unit type.
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...
void readCommonStyle(const QDomElement &layerElement, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)
Read style data common to all layer types.
QgsVectorTileRenderer * renderer() const
Returns a new instance of a vector tile renderer representing the converted style,...
QgsVectorTileLayer(const QString &path=QString(), const QString &baseName=QString())
Constructs a new vector tile layer.
Class for storing the component parts of a RDBMS data source URI (e.g.
Implements a map layer that is dedicated to rendering of vector tiles.
Contains information about the context in which a coordinate transform is executed.
QString sourceType() const
Returns type of the data source.
Stores coordinates of a tile in a tile matrix set.
The class is used as a container of context for various read/write operations on other objects.
Range of tiles in a tile matrix to be rendered.
#define QgsDebugMsgLevel(str, level)
QgsMapLayerType
Types of layers that can be added to a map.
void setRenderer(QgsVectorTileRenderer *r)
Sets renderer for the map layer.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get() or post() request has been made.
The default vector tile renderer implementation.
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...
Contains information about the context of a rendering operation.
Basic labeling configuration for vector tile layers.
@ RenderMillimeters
Millimeters.
~QgsVectorTileLayer() override
This class provides map rendering functionality for vector tile layers.
virtual void setMetadata(const QgsLayerMetadata &metadata)
Sets the layer's metadata store.
void setCrs(const QgsCoordinateReferenceSystem &srs, bool emitSignal=true)
Sets layer's spatial reference system.
A rectangle specified with double values.
QgsVectorTileLayer * clone() const override
Returns a new instance equivalent to this one except for the id which is still unique.
#define QgsSetRequestInitiatorClass(request, _class)
QString errorMessage() const
Returns a descriptive error message if an error was encountered during the style conversion,...
@ ServerExceptionError
An exception was raised by the server.
Base class for utility classes that encapsulate information necessary for rendering of map layers.
Result convert(const QVariantMap &style, QgsMapBoxGlStyleConversionContext *context=nullptr)
Converts a JSON style map, and returns the resultant status of the conversion.
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...
@ Success
Conversion was successful.
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
Context for a MapBox GL style conversion operation.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted.
QString sourcePath() const
Returns URL/path of the data source (syntax different to each data source type)
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
Defines a matrix of tiles for a single zoom level: it is defined by its size (width * height) and map...
Utility class for reading and writing MBTiles files (which are SQLite3 databases).
QgsMapLayerRenderer * createMapRenderer(QgsRenderContext &rendererContext) override
Returns new instance of QgsMapLayerRenderer that will be used for rendering of given context.
static bool checkXYZUrlTemplate(const QString &url)
Checks whether the URL template string is correct (contains {x}, {y} / {-y}, {z} placeholders)
virtual void setExtent(const QgsRectangle &rect)
Sets the extent.
Abstract base class for all vector tile renderer implementations.
void readStyleManager(const QDomNode &layerNode)
Read style manager's configuration (if any). To be called by subclasses.
void writeCommonStyle(QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const
Write style data common to all layer types.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)=0
Reads renderer's properties from given XML element.
QgsVectorTileRenderer * renderer() const
Returns currently assigned renderer.
void setSprites(const QImage &image, const QVariantMap &definitions)
Sets the sprite image and definitions JSON to use during conversion.
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
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...
@ NoError
No error was encountered.
QVariantMap spriteDefinitions() const
Returns the sprite definitions to use during conversion.
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...
void setLabeling(QgsVectorTileLabeling *labeling)
Sets labeling for the map layer.
QString mDataSource
Data source description string, varies by layer type.
int removeParam(const QString &key)
Removes a generic parameter by key.
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.
int zoomLevel() const
Returns tile's zoom level (Z)
QgsVectorTileLabeling * labeling() const
Returns a new instance of a vector tile labeling representing the converted style,...
This class represents a coordinate reference system (CRS).
bool hasParam(const QString &key) const
Returns true if a parameter with the specified key exists.
@ TimeoutError
Timeout was reached before a reply was received.
void setPixelSizeConversionFactor(double sizeConversionFactor)
Sets the pixel size conversion factor, used to scale the original pixel sizes when converting styles.
int sourceMaxZoom() const
Returns maximum zoom level at which source has any valid tiles (negative = unconstrained)
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
QString source() const
Returns the source for the layer.
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "get" operation on the specified request.
void writeStyleManager(QDomNode &layerNode, QDomDocument &doc) const
Write style manager's configuration (if exists). To be called by subclasses.
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
QgsCoordinateTransformContext transformContext() const
Returns the layer data provider coordinate transform context or a default transform context if the la...
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
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.
QgsVectorTileLabeling * labeling() const
Returns currently assigned labeling.
int row() const
Returns tile's row index (Y)
Base class for all map layer types.
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...
QByteArray getRawTile(QgsTileXYZ tileID)
Fetches raw tile data for the give tile coordinates.
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...
QByteArray content() const
Returns the reply content.
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
static QgsTileMatrix fromWebMercator(int mZoomLevel)
Returns a tile matrix for the usual web mercator.
void setTransformContext(const QgsCoordinateTransformContext &transformContext) override
Sets the coordinate transform context to transformContext.
Base class for labeling configuration classes for vector tile layers.
int sourceMinZoom() const
Returns minimum zoom level at which source has any valid tiles (negative = unconstrained)
bool readXml(const QDomNode &layerNode, QgsReadWriteContext &context) override
Called by readLayerXML(), used by children to read state specific to them from project files.
static QList< QgsVectorTileBasicRendererStyle > simpleStyleWithRandomColors()
Returns a list of styles to render all layers, using random colors.
QStringList warnings() const
Returns a list of user-friendly warnings generated during the conversion, e.g.
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
@ NetworkError
A network error occurred.
void setValid(bool valid)
Sets whether layer is valid or not.
QString htmlMetadata() const override
Obtain a formatted HTML string containing assorted metadata for this layer.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)=0
Reads labeling properties from given XML element.
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
QgsLayerMetadata metadata
Handles conversion of MapBox GL styles to QGIS vector tile renderers and labeling settings.
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.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
int column() const
Returns tile's column index (X)
virtual QgsError error() const
Gets current status error.
static QVariant parseJson(const std::string &jsonString)
Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned.