30#include <QNetworkRequest>
34QString QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_KEY = QStringLiteral(
"xyzvectortiles" );
35QString QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_DESCRIPTION = QObject::tr(
"XYZ Vector Tiles data provider" );
41QgsXyzVectorTileDataProviderBase::QgsXyzVectorTileDataProviderBase(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
50QgsXyzVectorTileDataProviderBase::QgsXyzVectorTileDataProviderBase(
const QgsXyzVectorTileDataProviderBase &other )
53 mAuthCfg = other.mAuthCfg;
54 mHeaders = other.mHeaders;
57bool QgsXyzVectorTileDataProviderBase::supportsAsync()
const
69 QList<QgsVectorTileRawData> rawTiles;
70 rawTiles.reserve( tiles.size() );
73 QMap<QString, QByteArray> data;
75 QgsStringMap::const_iterator it = sources.constBegin();
76 for ( ; it != sources.constEnd(); ++it )
81 const QByteArray rawData = loadFromNetwork(
id, set.
tileMatrix(
id.zoomLevel() ), it.value(), mAuthCfg, mHeaders, feedback, usage );
82 if ( !rawData.isEmpty() )
84 data[it.key()] = rawData;
94 QList<QNetworkRequest> requests;
98 QgsStringMap::const_iterator it = sourcesPaths.constBegin();
100 for ( ; it != sourcesPaths.constEnd(); ++it )
102 QString urlTemplate = it.value();
103 QString layerName = it.key();
105 if ( urlTemplate.contains( QLatin1String(
"{usage}" ) ) )
110 urlTemplate.replace( QLatin1String(
"{usage}" ), QLatin1String(
"view" ) );
113 urlTemplate.replace( QLatin1String(
"{usage}" ), QLatin1String(
"export" ) );
116 urlTemplate.replace( QLatin1String(
"{usage}" ), QString() );
123 QNetworkRequest request( url );
132 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
133 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
135 mHeaders.updateNetworkRequest( request );
152 if ( url.contains( QLatin1String(
"{usage}" ) ) )
157 url.replace( QLatin1String(
"{usage}" ), QLatin1String(
"view" ) );
160 url.replace( QLatin1String(
"{usage}" ), QLatin1String(
"export" ) );
163 url.replace( QLatin1String(
"{usage}" ), QString() );
169 nr.setUrl( QUrl( url ) );
193QgsXyzVectorTileDataProviderMetadata::QgsXyzVectorTileDataProviderMetadata()
194 :
QgsProviderMetadata( QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_KEY, QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_DESCRIPTION )
200 return new QgsXyzVectorTileDataProvider( uri, options, flags );
203QIcon QgsXyzVectorTileDataProviderMetadata::icon()
const
210 return FileBasedUris;
213QVariantMap QgsXyzVectorTileDataProviderMetadata::decodeUri(
const QString &uri )
const
218 QVariantMap uriComponents;
219 uriComponents.insert( QStringLiteral(
"type" ), QStringLiteral(
"xyz" ) );
221 if ( uriComponents[ QStringLiteral(
"type" ) ] == QLatin1String(
"mbtiles" ) ||
222 ( uriComponents[ QStringLiteral(
"type" ) ] == QLatin1String(
"xyz" ) &&
223 !dsUri.
param( QStringLiteral(
"url" ) ).startsWith( QLatin1String(
"http" ) ) ) )
225 uriComponents.insert( QStringLiteral(
"path" ), dsUri.
param( QStringLiteral(
"url" ) ) );
229 uriComponents.insert( QStringLiteral(
"url" ), dsUri.
param( QStringLiteral(
"url" ) ) );
230 if ( dsUri.
hasParam( QStringLiteral(
"urlName" ) ) )
231 uriComponents.insert( QStringLiteral(
"urlName" ), dsUri.
param( QStringLiteral(
"urlName" ) ) );
236 QString url = dsUri.
param( QStringLiteral(
"url_%2" ).arg( i ) );
237 QString urlName = dsUri.
param( QStringLiteral(
"urlName_%2" ).arg( i ) );
238 if ( url.isEmpty() || urlName.isEmpty() )
240 uriComponents.insert( QStringLiteral(
"urlName_%2" ).arg( i ), urlName );
241 uriComponents.insert( QStringLiteral(
"url_%2" ).arg( i ), url );
246 if ( dsUri.
hasParam( QStringLiteral(
"zmin" ) ) )
247 uriComponents.insert( QStringLiteral(
"zmin" ), dsUri.
param( QStringLiteral(
"zmin" ) ) );
248 if ( dsUri.
hasParam( QStringLiteral(
"zmax" ) ) )
249 uriComponents.insert( QStringLiteral(
"zmax" ), dsUri.
param( QStringLiteral(
"zmax" ) ) );
253 if ( dsUri.
hasParam( QStringLiteral(
"styleUrl" ) ) )
254 uriComponents.insert( QStringLiteral(
"styleUrl" ), dsUri.
param( QStringLiteral(
"styleUrl" ) ) );
257 if ( !authcfg.isEmpty() )
258 uriComponents.insert( QStringLiteral(
"authcfg" ), authcfg );
260 return uriComponents;
263QString QgsXyzVectorTileDataProviderMetadata::encodeUri(
const QVariantMap &parts )
const
266 dsUri.
setParam( QStringLiteral(
"type" ), QStringLiteral(
"xyz" ) );
267 dsUri.
setParam( QStringLiteral(
"url" ), parts.value( parts.contains( QStringLiteral(
"path" ) ) ? QStringLiteral(
"path" ) : QStringLiteral(
"url" ) ).toString() );
268 if ( parts.contains( QStringLiteral(
"urlName" ) ) )
269 dsUri.
setParam( QStringLiteral(
"urlName" ), parts[ QStringLiteral(
"urlName" ) ].toString() );
274 QString urlNameKey = QStringLiteral(
"urlName_%2" ).arg( i );
275 QString urlKey = QStringLiteral(
"url_%2" ).arg( i );
277 if ( !parts.contains( urlNameKey ) || !parts.contains( urlKey ) )
279 QString url = dsUri.
param( QStringLiteral(
"url_%2" ).arg( i ) );
280 QString urlName = dsUri.
param( QStringLiteral(
"urlName_%2" ).arg( i ) );
281 if ( url.isEmpty() || urlName.isEmpty() )
284 dsUri.
setParam( urlNameKey, parts[ urlNameKey ].toString() );
285 dsUri.
setParam( urlKey, parts[ urlKey ].toString() );
289 if ( parts.contains( QStringLiteral(
"zmin" ) ) )
290 dsUri.
setParam( QStringLiteral(
"zmin" ), parts[ QStringLiteral(
"zmin" ) ].toString() );
291 if ( parts.contains( QStringLiteral(
"zmax" ) ) )
292 dsUri.
setParam( QStringLiteral(
"zmax" ), parts[ QStringLiteral(
"zmax" ) ].toString() );
296 if ( parts.contains( QStringLiteral(
"styleUrl" ) ) )
297 dsUri.
setParam( QStringLiteral(
"styleUrl" ), parts[ QStringLiteral(
"styleUrl" ) ].toString() );
299 if ( parts.contains( QStringLiteral(
"authcfg" ) ) )
300 dsUri.
setAuthConfigId( parts[ QStringLiteral(
"authcfg" ) ].toString() );
305QString QgsXyzVectorTileDataProviderMetadata::absoluteToRelativeUri(
const QString &uri,
const QgsReadWriteContext &context )
const
310 QString sourcePath = dsUri.
param( QStringLiteral(
"url" ) );
312 const QUrl sourceUrl( sourcePath );
313 if ( sourceUrl.isLocalFile() )
318 dsUri.
setParam( QStringLiteral(
"url" ), QUrl::fromLocalFile( relSrcUrl ).toString() );
325QString QgsXyzVectorTileDataProviderMetadata::relativeToAbsoluteUri(
const QString &uri,
const QgsReadWriteContext &context )
const
330 QString sourcePath = dsUri.
param( QStringLiteral(
"url" ) );
332 const QUrl sourceUrl( sourcePath );
333 if ( sourceUrl.isLocalFile() )
337 dsUri.
setParam( QStringLiteral(
"url" ), QUrl::fromLocalFile( absSrcUrl ).toString() );
344QList<Qgis::LayerType> QgsXyzVectorTileDataProviderMetadata::supportedLayerTypes()
const
354QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
355 : QgsXyzVectorTileDataProviderBase( uri, providerOptions, flags )
360 const QString sourcePath = dsUri.
param( QStringLiteral(
"url" ) );
363 QgsDebugError( QStringLiteral(
"Invalid format of URL for XYZ source: " ) + sourcePath );
369 if ( dsUri.
hasParam( QStringLiteral(
"zmin" ) ) )
370 zMin = dsUri.
param( QStringLiteral(
"zmin" ) ).toInt();
373 if ( dsUri.
hasParam( QStringLiteral(
"zmax" ) ) )
374 zMax = dsUri.
param( QStringLiteral(
"zmax" ) ).toInt();
377 mExtent =
QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 );
382QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider(
const QgsXyzVectorTileDataProvider &other )
383 : QgsXyzVectorTileDataProviderBase( other )
385 mIsValid = other.mIsValid;
386 mExtent = other.mExtent;
387 mMatrixSet = other.mMatrixSet;
395QString QgsXyzVectorTileDataProvider::name()
const
399 return XYZ_DATA_PROVIDER_KEY;
402QString QgsXyzVectorTileDataProvider::description()
const
406 return XYZ_DATA_PROVIDER_DESCRIPTION;
413 return new QgsXyzVectorTileDataProvider( *
this );
416bool QgsXyzVectorTileDataProvider::isValid()
const
423QgsRectangle QgsXyzVectorTileDataProvider::extent()
const
444QString QgsXyzVectorTileDataProvider::sourcePath()
const
450 return dsUri.param( QStringLiteral(
"url" ) );
453QgsStringMap QgsXyzVectorTileDataProvider::sourcePaths()
const
460 QgsStringMap paths = { { dsUri.param( QStringLiteral(
"urlName" ) ), dsUri.param( QStringLiteral(
"url" ) ) } };
465 QString url = dsUri.param( QStringLiteral(
"url_%2" ).arg( i ) );
466 QString urlName = dsUri.param( QStringLiteral(
"urlName_%2" ).arg( i ) );
467 if ( url.isEmpty() || urlName.isEmpty() )
470 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.