32#include <QNetworkRequest>
34#include "moc_qgsxyzvectortiledataprovider.cpp"
38QString QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_KEY = QStringLiteral(
"xyzvectortiles" );
39QString QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_DESCRIPTION = QObject::tr(
"XYZ Vector Tiles data provider" );
45QgsXyzVectorTileDataProviderBase::QgsXyzVectorTileDataProviderBase(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
54QgsXyzVectorTileDataProviderBase::QgsXyzVectorTileDataProviderBase(
const QgsXyzVectorTileDataProviderBase &other )
57 mAuthCfg = other.mAuthCfg;
58 mHeaders = other.mHeaders;
61bool QgsXyzVectorTileDataProviderBase::supportsAsync()
const
73 QList<QgsVectorTileRawData> rawTiles;
74 rawTiles.reserve( tiles.size() );
77 QMap<QString, QByteArray> data;
79 QgsStringMap::const_iterator it = sources.constBegin();
80 for ( ; it != sources.constEnd(); ++it )
85 const QByteArray rawData = loadFromNetwork(
id, set.
tileMatrix(
id.zoomLevel() ), it.value(), mAuthCfg, mHeaders, feedback, usage );
86 if ( !rawData.isEmpty() )
88 data[it.key()] = rawData;
98 QList<QNetworkRequest> requests;
102 QgsStringMap::const_iterator it = sourcesPaths.constBegin();
104 for ( ; it != sourcesPaths.constEnd(); ++it )
106 QString urlTemplate = it.value();
107 QString layerName = it.key();
109 if ( urlTemplate.contains( QLatin1String(
"{usage}" ) ) )
114 urlTemplate.replace( QLatin1String(
"{usage}" ), QLatin1String(
"view" ) );
117 urlTemplate.replace( QLatin1String(
"{usage}" ), QLatin1String(
"export" ) );
120 urlTemplate.replace( QLatin1String(
"{usage}" ), QString() );
127 QNetworkRequest request( url );
136 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
137 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
139 mHeaders.updateNetworkRequest( request );
156 if ( url.contains( QLatin1String(
"{usage}" ) ) )
161 url.replace( QLatin1String(
"{usage}" ), QLatin1String(
"view" ) );
164 url.replace( QLatin1String(
"{usage}" ), QLatin1String(
"export" ) );
167 url.replace( QLatin1String(
"{usage}" ), QString() );
173 nr.setUrl( QUrl( url ) );
197QgsXyzVectorTileDataProviderMetadata::QgsXyzVectorTileDataProviderMetadata()
198 :
QgsProviderMetadata( QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_KEY, QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_DESCRIPTION )
204 return new QgsXyzVectorTileDataProvider( uri, options, flags );
207QIcon QgsXyzVectorTileDataProviderMetadata::icon()
const
214 return FileBasedUris;
217QVariantMap QgsXyzVectorTileDataProviderMetadata::decodeUri(
const QString &uri )
const
222 QVariantMap uriComponents;
223 uriComponents.insert( QStringLiteral(
"type" ), QStringLiteral(
"xyz" ) );
225 if ( uriComponents[ QStringLiteral(
"type" ) ] == QLatin1String(
"mbtiles" ) ||
226 ( uriComponents[ QStringLiteral(
"type" ) ] == QLatin1String(
"xyz" ) &&
227 !dsUri.
param( QStringLiteral(
"url" ) ).startsWith( QLatin1String(
"http" ) ) ) )
229 uriComponents.insert( QStringLiteral(
"path" ), dsUri.
param( QStringLiteral(
"url" ) ) );
233 uriComponents.insert( QStringLiteral(
"url" ), dsUri.
param( QStringLiteral(
"url" ) ) );
234 if ( dsUri.
hasParam( QStringLiteral(
"urlName" ) ) )
235 uriComponents.insert( QStringLiteral(
"urlName" ), dsUri.
param( QStringLiteral(
"urlName" ) ) );
240 QString url = dsUri.
param( QStringLiteral(
"url_%2" ).arg( i ) );
241 QString urlName = dsUri.
param( QStringLiteral(
"urlName_%2" ).arg( i ) );
242 if ( url.isEmpty() || urlName.isEmpty() )
244 uriComponents.insert( QStringLiteral(
"urlName_%2" ).arg( i ), urlName );
245 uriComponents.insert( QStringLiteral(
"url_%2" ).arg( i ), url );
250 if ( dsUri.
hasParam( QStringLiteral(
"zmin" ) ) )
251 uriComponents.insert( QStringLiteral(
"zmin" ), dsUri.
param( QStringLiteral(
"zmin" ) ) );
252 if ( dsUri.
hasParam( QStringLiteral(
"zmax" ) ) )
253 uriComponents.insert( QStringLiteral(
"zmax" ), dsUri.
param( QStringLiteral(
"zmax" ) ) );
257 if ( dsUri.
hasParam( QStringLiteral(
"styleUrl" ) ) )
258 uriComponents.insert( QStringLiteral(
"styleUrl" ), dsUri.
param( QStringLiteral(
"styleUrl" ) ) );
261 if ( !authcfg.isEmpty() )
262 uriComponents.insert( QStringLiteral(
"authcfg" ), authcfg );
264 return uriComponents;
267QString QgsXyzVectorTileDataProviderMetadata::encodeUri(
const QVariantMap &parts )
const
270 dsUri.
setParam( QStringLiteral(
"type" ), QStringLiteral(
"xyz" ) );
271 dsUri.
setParam( QStringLiteral(
"url" ), parts.value( parts.contains( QStringLiteral(
"path" ) ) ? QStringLiteral(
"path" ) : QStringLiteral(
"url" ) ).toString() );
272 if ( parts.contains( QStringLiteral(
"urlName" ) ) )
273 dsUri.
setParam( QStringLiteral(
"urlName" ), parts[ QStringLiteral(
"urlName" ) ].toString() );
278 QString urlNameKey = QStringLiteral(
"urlName_%2" ).arg( i );
279 QString urlKey = QStringLiteral(
"url_%2" ).arg( i );
281 if ( !parts.contains( urlNameKey ) || !parts.contains( urlKey ) )
283 QString url = dsUri.
param( QStringLiteral(
"url_%2" ).arg( i ) );
284 QString urlName = dsUri.
param( QStringLiteral(
"urlName_%2" ).arg( i ) );
285 if ( url.isEmpty() || urlName.isEmpty() )
288 dsUri.
setParam( urlNameKey, parts[ urlNameKey ].toString() );
289 dsUri.
setParam( urlKey, parts[ urlKey ].toString() );
293 if ( parts.contains( QStringLiteral(
"zmin" ) ) )
294 dsUri.
setParam( QStringLiteral(
"zmin" ), parts[ QStringLiteral(
"zmin" ) ].toString() );
295 if ( parts.contains( QStringLiteral(
"zmax" ) ) )
296 dsUri.
setParam( QStringLiteral(
"zmax" ), parts[ QStringLiteral(
"zmax" ) ].toString() );
300 if ( parts.contains( QStringLiteral(
"styleUrl" ) ) )
301 dsUri.
setParam( QStringLiteral(
"styleUrl" ), parts[ QStringLiteral(
"styleUrl" ) ].toString() );
303 if ( parts.contains( QStringLiteral(
"authcfg" ) ) )
304 dsUri.
setAuthConfigId( parts[ QStringLiteral(
"authcfg" ) ].toString() );
309QString QgsXyzVectorTileDataProviderMetadata::absoluteToRelativeUri(
const QString &uri,
const QgsReadWriteContext &context )
const
314 QString sourcePath = dsUri.
param( QStringLiteral(
"url" ) );
316 const QUrl sourceUrl( sourcePath );
317 if ( sourceUrl.isLocalFile() )
322 dsUri.
setParam( QStringLiteral(
"url" ), QUrl::fromLocalFile( relSrcUrl ).toString( QUrl::DecodeReserved ) );
329QString QgsXyzVectorTileDataProviderMetadata::relativeToAbsoluteUri(
const QString &uri,
const QgsReadWriteContext &context )
const
334 QString sourcePath = dsUri.
param( QStringLiteral(
"url" ) );
336 const QUrl sourceUrl( sourcePath );
337 if ( sourceUrl.isLocalFile() )
341 dsUri.
setParam( QStringLiteral(
"url" ), QUrl::fromLocalFile( absSrcUrl ).toString( QUrl::DecodeReserved ) );
348QList<Qgis::LayerType> QgsXyzVectorTileDataProviderMetadata::supportedLayerTypes()
const
358QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
359 : QgsXyzVectorTileDataProviderBase( uri, providerOptions, flags )
364 const QString sourcePath = dsUri.
param( QStringLiteral(
"url" ) );
367 QgsDebugError( QStringLiteral(
"Invalid format of URL for XYZ source: " ) + sourcePath );
373 if ( dsUri.
hasParam( QStringLiteral(
"zmin" ) ) )
374 zMin = dsUri.
param( QStringLiteral(
"zmin" ) ).toInt();
377 if ( dsUri.
hasParam( QStringLiteral(
"zmax" ) ) )
378 zMax = dsUri.
param( QStringLiteral(
"zmax" ) ).toInt();
381 mExtent =
QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 );
386QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider(
const QgsXyzVectorTileDataProvider &other )
387 : QgsXyzVectorTileDataProviderBase( other )
389 mIsValid = other.mIsValid;
390 mExtent = other.mExtent;
391 mMatrixSet = other.mMatrixSet;
399QString QgsXyzVectorTileDataProvider::name()
const
403 return XYZ_DATA_PROVIDER_KEY;
406QString QgsXyzVectorTileDataProvider::description()
const
410 return XYZ_DATA_PROVIDER_DESCRIPTION;
417 return new QgsXyzVectorTileDataProvider( *
this );
420bool QgsXyzVectorTileDataProvider::isValid()
const
427QgsRectangle QgsXyzVectorTileDataProvider::extent()
const
448QString QgsXyzVectorTileDataProvider::sourcePath()
const
454 return dsUri.param( QStringLiteral(
"url" ) );
457QgsStringMap QgsXyzVectorTileDataProvider::sourcePaths()
const
464 QgsStringMap paths = { { dsUri.param( QStringLiteral(
"urlName" ) ), dsUri.param( QStringLiteral(
"url" ) ) } };
469 QString url = dsUri.param( QStringLiteral(
"url_%2" ).arg( i ) );
470 QString urlName = dsUri.param( QStringLiteral(
"urlName_%2" ).arg( i ) );
471 if ( url.isEmpty() || urlName.isEmpty() )
474 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.
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...
Represents a coordinate reference system (CRS).
Stores the component parts of a 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, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
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.
A container for the context for various read/write operations on 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}...
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.