32#include <QNetworkRequest>
35#include "moc_qgsxyzvectortiledataprovider.cpp"
37using namespace Qt::StringLiterals;
41QString QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_KEY = u
"xyzvectortiles"_s;
42QString QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_DESCRIPTION = QObject::tr(
"XYZ Vector Tiles data provider" );
48QgsXyzVectorTileDataProviderBase::QgsXyzVectorTileDataProviderBase(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
57QgsXyzVectorTileDataProviderBase::QgsXyzVectorTileDataProviderBase(
const QgsXyzVectorTileDataProviderBase &other )
60 mAuthCfg = other.mAuthCfg;
61 mHeaders = other.mHeaders;
64bool QgsXyzVectorTileDataProviderBase::supportsAsync()
const
76 QList<QgsVectorTileRawData> rawTiles;
77 rawTiles.reserve( tiles.size() );
80 QMap<QString, QByteArray> data;
82 QgsStringMap::const_iterator it = sources.constBegin();
83 for ( ; it != sources.constEnd(); ++it )
88 const QByteArray rawData = loadFromNetwork(
id, set.
tileMatrix(
id.zoomLevel() ), it.value(), mAuthCfg, mHeaders, feedback, usage );
89 if ( !rawData.isEmpty() )
91 data[it.key()] = rawData;
101 QList<QNetworkRequest> requests;
105 QgsStringMap::const_iterator it = sourcesPaths.constBegin();
107 for ( ; it != sourcesPaths.constEnd(); ++it )
109 QString urlTemplate = it.value();
110 QString layerName = it.key();
112 if ( urlTemplate.contains(
"{usage}"_L1 ) )
117 urlTemplate.replace(
"{usage}"_L1,
"view"_L1 );
120 urlTemplate.replace(
"{usage}"_L1,
"export"_L1 );
123 urlTemplate.replace(
"{usage}"_L1, QString() );
130 QNetworkRequest request( url );
139 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
140 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
142 mHeaders.updateNetworkRequest( request );
159 if ( url.contains(
"{usage}"_L1 ) )
164 url.replace(
"{usage}"_L1,
"view"_L1 );
167 url.replace(
"{usage}"_L1,
"export"_L1 );
170 url.replace(
"{usage}"_L1, QString() );
176 nr.setUrl( QUrl( url ) );
200QgsXyzVectorTileDataProviderMetadata::QgsXyzVectorTileDataProviderMetadata()
201 :
QgsProviderMetadata( QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_KEY, QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_DESCRIPTION )
207 return new QgsXyzVectorTileDataProvider( uri, options, flags );
210QIcon QgsXyzVectorTileDataProviderMetadata::icon()
const
217 return FileBasedUris;
220QVariantMap QgsXyzVectorTileDataProviderMetadata::decodeUri(
const QString &uri )
const
225 QVariantMap uriComponents;
226 uriComponents.insert( u
"type"_s, u
"xyz"_s );
228 if ( uriComponents[ u
"type"_s ] ==
"mbtiles"_L1 ||
229 ( uriComponents[ u
"type"_s ] ==
"xyz"_L1 &&
230 !dsUri.
param( u
"url"_s ).startsWith(
"http"_L1 ) ) )
232 uriComponents.insert( u
"path"_s, dsUri.
param( u
"url"_s ) );
236 uriComponents.insert( u
"url"_s, dsUri.
param( u
"url"_s ) );
237 if ( dsUri.
hasParam( u
"urlName"_s ) )
238 uriComponents.insert( u
"urlName"_s, dsUri.
param( u
"urlName"_s ) );
243 QString url = dsUri.
param( u
"url_%2"_s.arg( i ) );
244 QString urlName = dsUri.
param( u
"urlName_%2"_s.arg( i ) );
245 if ( url.isEmpty() || urlName.isEmpty() )
247 uriComponents.insert( u
"urlName_%2"_s.arg( i ), urlName );
248 uriComponents.insert( u
"url_%2"_s.arg( i ), url );
254 uriComponents.insert( u
"zmin"_s, dsUri.
param( u
"zmin"_s ) );
256 uriComponents.insert( u
"zmax"_s, dsUri.
param( u
"zmax"_s ) );
260 if ( dsUri.
hasParam( u
"styleUrl"_s ) )
261 uriComponents.insert( u
"styleUrl"_s, dsUri.
param( u
"styleUrl"_s ) );
264 if ( !authcfg.isEmpty() )
265 uriComponents.insert( u
"authcfg"_s, authcfg );
267 return uriComponents;
270QString QgsXyzVectorTileDataProviderMetadata::encodeUri(
const QVariantMap &parts )
const
273 dsUri.
setParam( u
"type"_s, u
"xyz"_s );
274 dsUri.
setParam( u
"url"_s, parts.value( parts.contains( u
"path"_s ) ? u
"path"_s : u
"url"_s ).toString() );
275 if ( parts.contains( u
"urlName"_s ) )
276 dsUri.
setParam( u
"urlName"_s, parts[ u
"urlName"_s ].toString() );
281 QString urlNameKey = u
"urlName_%2"_s.arg( i );
282 QString urlKey = u
"url_%2"_s.arg( i );
284 if ( !parts.contains( urlNameKey ) || !parts.contains( urlKey ) )
286 QString url = dsUri.
param( u
"url_%2"_s.arg( i ) );
287 QString urlName = dsUri.
param( u
"urlName_%2"_s.arg( i ) );
288 if ( url.isEmpty() || urlName.isEmpty() )
291 dsUri.
setParam( urlNameKey, parts[ urlNameKey ].toString() );
292 dsUri.
setParam( urlKey, parts[ urlKey ].toString() );
296 if ( parts.contains( u
"zmin"_s ) )
297 dsUri.
setParam( u
"zmin"_s, parts[ u
"zmin"_s ].toString() );
298 if ( parts.contains( u
"zmax"_s ) )
299 dsUri.
setParam( u
"zmax"_s, parts[ u
"zmax"_s ].toString() );
303 if ( parts.contains( u
"styleUrl"_s ) )
304 dsUri.
setParam( u
"styleUrl"_s, parts[ u
"styleUrl"_s ].toString() );
306 if ( parts.contains( u
"authcfg"_s ) )
312QString QgsXyzVectorTileDataProviderMetadata::absoluteToRelativeUri(
const QString &uri,
const QgsReadWriteContext &context )
const
317 QString sourcePath = dsUri.
param( u
"url"_s );
319 const QUrl sourceUrl( sourcePath );
320 if ( sourceUrl.isLocalFile() )
325 dsUri.
setParam( u
"url"_s, QUrl::fromLocalFile( relSrcUrl ).toString( QUrl::DecodeReserved ) );
332QString QgsXyzVectorTileDataProviderMetadata::relativeToAbsoluteUri(
const QString &uri,
const QgsReadWriteContext &context )
const
337 QString sourcePath = dsUri.
param( u
"url"_s );
339 const QUrl sourceUrl( sourcePath );
340 if ( sourceUrl.isLocalFile() )
344 dsUri.
setParam( u
"url"_s, QUrl::fromLocalFile( absSrcUrl ).toString( QUrl::DecodeReserved ) );
351QList<Qgis::LayerType> QgsXyzVectorTileDataProviderMetadata::supportedLayerTypes()
const
361QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
362 : QgsXyzVectorTileDataProviderBase( uri, providerOptions, flags )
367 const QString sourcePath = dsUri.
param( u
"url"_s );
370 QgsDebugError( u
"Invalid format of URL for XYZ source: "_s + sourcePath );
377 zMin = dsUri.
param( u
"zmin"_s ).toInt();
381 zMax = dsUri.
param( u
"zmax"_s ).toInt();
384 mExtent =
QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 );
389QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider(
const QgsXyzVectorTileDataProvider &other )
390 : QgsXyzVectorTileDataProviderBase( other )
392 mIsValid = other.mIsValid;
393 mExtent = other.mExtent;
394 mMatrixSet = other.mMatrixSet;
402QString QgsXyzVectorTileDataProvider::name()
const
406 return XYZ_DATA_PROVIDER_KEY;
409QString QgsXyzVectorTileDataProvider::description()
const
413 return XYZ_DATA_PROVIDER_DESCRIPTION;
420 return new QgsXyzVectorTileDataProvider( *
this );
423bool QgsXyzVectorTileDataProvider::isValid()
const
430QgsRectangle QgsXyzVectorTileDataProvider::extent()
const
451QString QgsXyzVectorTileDataProvider::sourcePath()
const
457 return dsUri.param( u
"url"_s );
460QgsStringMap QgsXyzVectorTileDataProvider::sourcePaths()
const
467 QgsStringMap paths = { { dsUri.param( u
"urlName"_s ), dsUri.param( u
"url"_s ) } };
472 QString url = dsUri.param( u
"url_%2"_s.arg( i ) );
473 QString urlName = dsUri.param( u
"urlName_%2"_s.arg( i ) );
474 if ( url.isEmpty() || urlName.isEmpty() )
477 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.