18#include "moc_qgsxyzvectortiledataprovider.cpp"
31#include <QNetworkRequest>
35QString QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_KEY = QStringLiteral(
"xyzvectortiles" );
36QString QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_DESCRIPTION = QObject::tr(
"XYZ Vector Tiles data provider" );
42QgsXyzVectorTileDataProviderBase::QgsXyzVectorTileDataProviderBase(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
51QgsXyzVectorTileDataProviderBase::QgsXyzVectorTileDataProviderBase(
const QgsXyzVectorTileDataProviderBase &other )
54 mAuthCfg = other.mAuthCfg;
55 mHeaders = other.mHeaders;
58bool QgsXyzVectorTileDataProviderBase::supportsAsync()
const
70 QList<QgsVectorTileRawData> rawTiles;
71 rawTiles.reserve( tiles.size() );
74 QMap<QString, QByteArray> data;
76 QgsStringMap::const_iterator it = sources.constBegin();
77 for ( ; it != sources.constEnd(); ++it )
82 const QByteArray rawData = loadFromNetwork(
id, set.
tileMatrix(
id.zoomLevel() ), it.value(), mAuthCfg, mHeaders, feedback, usage );
83 if ( !rawData.isEmpty() )
85 data[it.key()] = rawData;
95 QList<QNetworkRequest> requests;
99 QgsStringMap::const_iterator it = sourcesPaths.constBegin();
101 for ( ; it != sourcesPaths.constEnd(); ++it )
103 QString urlTemplate = it.value();
104 QString layerName = it.key();
106 if ( urlTemplate.contains( QLatin1String(
"{usage}" ) ) )
111 urlTemplate.replace( QLatin1String(
"{usage}" ), QLatin1String(
"view" ) );
114 urlTemplate.replace( QLatin1String(
"{usage}" ), QLatin1String(
"export" ) );
117 urlTemplate.replace( QLatin1String(
"{usage}" ), QString() );
124 QNetworkRequest request( url );
133 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
134 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
136 mHeaders.updateNetworkRequest( request );
153 if ( url.contains( QLatin1String(
"{usage}" ) ) )
158 url.replace( QLatin1String(
"{usage}" ), QLatin1String(
"view" ) );
161 url.replace( QLatin1String(
"{usage}" ), QLatin1String(
"export" ) );
164 url.replace( QLatin1String(
"{usage}" ), QString() );
170 nr.setUrl( QUrl( url ) );
194QgsXyzVectorTileDataProviderMetadata::QgsXyzVectorTileDataProviderMetadata()
195 :
QgsProviderMetadata( QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_KEY, QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_DESCRIPTION )
201 return new QgsXyzVectorTileDataProvider( uri, options, flags );
204QIcon QgsXyzVectorTileDataProviderMetadata::icon()
const
211 return FileBasedUris;
214QVariantMap QgsXyzVectorTileDataProviderMetadata::decodeUri(
const QString &uri )
const
219 QVariantMap uriComponents;
220 uriComponents.insert( QStringLiteral(
"type" ), QStringLiteral(
"xyz" ) );
222 if ( uriComponents[ QStringLiteral(
"type" ) ] == QLatin1String(
"mbtiles" ) ||
223 ( uriComponents[ QStringLiteral(
"type" ) ] == QLatin1String(
"xyz" ) &&
224 !dsUri.
param( QStringLiteral(
"url" ) ).startsWith( QLatin1String(
"http" ) ) ) )
226 uriComponents.insert( QStringLiteral(
"path" ), dsUri.
param( QStringLiteral(
"url" ) ) );
230 uriComponents.insert( QStringLiteral(
"url" ), dsUri.
param( QStringLiteral(
"url" ) ) );
231 if ( dsUri.
hasParam( QStringLiteral(
"urlName" ) ) )
232 uriComponents.insert( QStringLiteral(
"urlName" ), dsUri.
param( QStringLiteral(
"urlName" ) ) );
237 QString url = dsUri.
param( QStringLiteral(
"url_%2" ).arg( i ) );
238 QString urlName = dsUri.
param( QStringLiteral(
"urlName_%2" ).arg( i ) );
239 if ( url.isEmpty() || urlName.isEmpty() )
241 uriComponents.insert( QStringLiteral(
"urlName_%2" ).arg( i ), urlName );
242 uriComponents.insert( QStringLiteral(
"url_%2" ).arg( i ), url );
247 if ( dsUri.
hasParam( QStringLiteral(
"zmin" ) ) )
248 uriComponents.insert( QStringLiteral(
"zmin" ), dsUri.
param( QStringLiteral(
"zmin" ) ) );
249 if ( dsUri.
hasParam( QStringLiteral(
"zmax" ) ) )
250 uriComponents.insert( QStringLiteral(
"zmax" ), dsUri.
param( QStringLiteral(
"zmax" ) ) );
254 if ( dsUri.
hasParam( QStringLiteral(
"styleUrl" ) ) )
255 uriComponents.insert( QStringLiteral(
"styleUrl" ), dsUri.
param( QStringLiteral(
"styleUrl" ) ) );
258 if ( !authcfg.isEmpty() )
259 uriComponents.insert( QStringLiteral(
"authcfg" ), authcfg );
261 return uriComponents;
264QString QgsXyzVectorTileDataProviderMetadata::encodeUri(
const QVariantMap &parts )
const
267 dsUri.
setParam( QStringLiteral(
"type" ), QStringLiteral(
"xyz" ) );
268 dsUri.
setParam( QStringLiteral(
"url" ), parts.value( parts.contains( QStringLiteral(
"path" ) ) ? QStringLiteral(
"path" ) : QStringLiteral(
"url" ) ).toString() );
269 if ( parts.contains( QStringLiteral(
"urlName" ) ) )
270 dsUri.
setParam( QStringLiteral(
"urlName" ), parts[ QStringLiteral(
"urlName" ) ].toString() );
275 QString urlNameKey = QStringLiteral(
"urlName_%2" ).arg( i );
276 QString urlKey = QStringLiteral(
"url_%2" ).arg( i );
278 if ( !parts.contains( urlNameKey ) || !parts.contains( urlKey ) )
280 QString url = dsUri.
param( QStringLiteral(
"url_%2" ).arg( i ) );
281 QString urlName = dsUri.
param( QStringLiteral(
"urlName_%2" ).arg( i ) );
282 if ( url.isEmpty() || urlName.isEmpty() )
285 dsUri.
setParam( urlNameKey, parts[ urlNameKey ].toString() );
286 dsUri.
setParam( urlKey, parts[ urlKey ].toString() );
290 if ( parts.contains( QStringLiteral(
"zmin" ) ) )
291 dsUri.
setParam( QStringLiteral(
"zmin" ), parts[ QStringLiteral(
"zmin" ) ].toString() );
292 if ( parts.contains( QStringLiteral(
"zmax" ) ) )
293 dsUri.
setParam( QStringLiteral(
"zmax" ), parts[ QStringLiteral(
"zmax" ) ].toString() );
297 if ( parts.contains( QStringLiteral(
"styleUrl" ) ) )
298 dsUri.
setParam( QStringLiteral(
"styleUrl" ), parts[ QStringLiteral(
"styleUrl" ) ].toString() );
300 if ( parts.contains( QStringLiteral(
"authcfg" ) ) )
301 dsUri.
setAuthConfigId( parts[ QStringLiteral(
"authcfg" ) ].toString() );
306QString QgsXyzVectorTileDataProviderMetadata::absoluteToRelativeUri(
const QString &uri,
const QgsReadWriteContext &context )
const
311 QString sourcePath = dsUri.
param( QStringLiteral(
"url" ) );
313 const QUrl sourceUrl( sourcePath );
314 if ( sourceUrl.isLocalFile() )
319 dsUri.
setParam( QStringLiteral(
"url" ), QUrl::fromLocalFile( relSrcUrl ).toString() );
326QString QgsXyzVectorTileDataProviderMetadata::relativeToAbsoluteUri(
const QString &uri,
const QgsReadWriteContext &context )
const
331 QString sourcePath = dsUri.
param( QStringLiteral(
"url" ) );
333 const QUrl sourceUrl( sourcePath );
334 if ( sourceUrl.isLocalFile() )
338 dsUri.
setParam( QStringLiteral(
"url" ), QUrl::fromLocalFile( absSrcUrl ).toString() );
345QList<Qgis::LayerType> QgsXyzVectorTileDataProviderMetadata::supportedLayerTypes()
const
355QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
356 : QgsXyzVectorTileDataProviderBase( uri, providerOptions, flags )
361 const QString sourcePath = dsUri.
param( QStringLiteral(
"url" ) );
364 QgsDebugError( QStringLiteral(
"Invalid format of URL for XYZ source: " ) + sourcePath );
370 if ( dsUri.
hasParam( QStringLiteral(
"zmin" ) ) )
371 zMin = dsUri.
param( QStringLiteral(
"zmin" ) ).toInt();
374 if ( dsUri.
hasParam( QStringLiteral(
"zmax" ) ) )
375 zMax = dsUri.
param( QStringLiteral(
"zmax" ) ).toInt();
378 mExtent =
QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 );
383QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider(
const QgsXyzVectorTileDataProvider &other )
384 : QgsXyzVectorTileDataProviderBase( other )
386 mIsValid = other.mIsValid;
387 mExtent = other.mExtent;
388 mMatrixSet = other.mMatrixSet;
396QString QgsXyzVectorTileDataProvider::name()
const
400 return XYZ_DATA_PROVIDER_KEY;
403QString QgsXyzVectorTileDataProvider::description()
const
407 return XYZ_DATA_PROVIDER_DESCRIPTION;
414 return new QgsXyzVectorTileDataProvider( *
this );
417bool QgsXyzVectorTileDataProvider::isValid()
const
424QgsRectangle QgsXyzVectorTileDataProvider::extent()
const
445QString QgsXyzVectorTileDataProvider::sourcePath()
const
451 return dsUri.param( QStringLiteral(
"url" ) );
454QgsStringMap QgsXyzVectorTileDataProvider::sourcePaths()
const
461 QgsStringMap paths = { { dsUri.param( QStringLiteral(
"urlName" ) ), dsUri.param( QStringLiteral(
"url" ) ) } };
466 QString url = dsUri.param( QStringLiteral(
"url_%2" ).arg( i ) );
467 QString urlName = dsUri.param( QStringLiteral(
"urlName_%2" ).arg( i ) );
468 if ( url.isEmpty() || urlName.isEmpty() )
471 paths.insert( urlName, url );
QFlags< DataProviderFlag > DataProviderFlags
Data provider flags.
@ FastExtent2D
Provider's 2D extent retrieval via QgsDataProvider::extent() is always guaranteed to be trivial/fast ...
QFlags< DataProviderReadFlag > DataProviderReadFlags
Flags which control data provider construction.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
RendererUsage
Usage of the renderer.
@ Export
Renderer used for printing or exporting to a file.
@ View
Renderer used for displaying on screen.
@ Unknown
Renderer used for unknown usage.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
bool updateNetworkRequest(QNetworkRequest &request, const QString &authcfg, const QString &dataprovider=QString())
Provider call to update a QNetworkRequest with an authentication config.
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
void setAuthCfg(const QString &authCfg)
Sets the authentication config id which should be used during the request.
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr, RequestFlags requestFlags=QgsBlockingNetworkRequest::RequestFlags())
Performs a "get" operation on the specified request.
@ NoError
No error was encountered.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
This class represents a coordinate reference system (CRS).
Class for storing the component parts of a RDBMS data source URI (e.g.
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
bool hasParam(const QString &key) const
Returns true if a parameter with the specified key exists.
int removeParam(const QString &key)
Removes a generic parameter by key.
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID for the URI.
QgsHttpHeaders httpHeaders() const
Returns http headers.
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const
Tells whether the operation has been canceled already.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
QByteArray content() const
Returns the reply content.
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
The class is used as a container of context for various read/write operations on other objects.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
A rectangle specified with double values.
Defines a set of tile matrices for multiple zoom levels.
QgsTileMatrix tileMatrix(int zoom) const
Returns the tile matrix corresponding to the specified zoom.
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Stores coordinates of a tile in a tile matrix set.
Base class for vector tile layer data providers.
static int DATA_ZOOM
Role to set zoom attribute in the request so it can be retrieved later.
static int DATA_ROW
Role to set row attribute in the request so it can be retrieved later.
static int DATA_SOURCE_ID
Role to set source ID attribute in the request so it can be retrieved later.
static int DATA_COLUMN
Role to set column attribute in the request so it can be retrieved later.
Encapsulates properties of a vector tile matrix set, including tile origins and scaling information.
static QgsVectorTileMatrixSet fromWebMercator(int minimumZoom=0, int maximumZoom=14)
Returns a vector tile structure corresponding to the standard web mercator/GoogleCRS84Quad setup.
Keeps track of raw tile data from one or more sources that need to be decoded.
static bool checkXYZUrlTemplate(const QString &url)
Checks whether the URL template string is correct (contains {x}, {y} / {-y}, {z} placeholders)
static QString formatXYZUrlTemplate(const QString &url, QgsTileXYZ tile, const QgsTileMatrix &tileMatrix)
Returns formatted tile URL string replacing {x}, {y}, {z} placeholders (or {-y} instead of {y} for TM...
QMap< QString, QString > QgsStringMap
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
#define QgsSetRequestInitiatorClass(request, _class)
#define QgsSetRequestInitiatorId(request, str)
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
Setting options for creating vector data providers.