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 );
155QByteArray QgsXyzVectorTileDataProviderBase::loadFromNetwork(
161 if ( url.contains(
"{usage}"_L1 ) )
166 url.replace(
"{usage}"_L1,
"view"_L1 );
169 url.replace(
"{usage}"_L1,
"export"_L1 );
172 url.replace(
"{usage}"_L1, QString() );
178 nr.setUrl( QUrl( url ) );
202QgsXyzVectorTileDataProviderMetadata::QgsXyzVectorTileDataProviderMetadata()
203 :
QgsProviderMetadata( QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_KEY, QgsXyzVectorTileDataProvider::XYZ_DATA_PROVIDER_DESCRIPTION )
208 return new QgsXyzVectorTileDataProvider( uri, options, flags );
211QIcon QgsXyzVectorTileDataProviderMetadata::icon()
const
218 return FileBasedUris;
221QVariantMap QgsXyzVectorTileDataProviderMetadata::decodeUri(
const QString &uri )
const
226 QVariantMap uriComponents;
227 uriComponents.insert( u
"type"_s, u
"xyz"_s );
229 if ( uriComponents[u
"type"_s] ==
"mbtiles"_L1 || ( uriComponents[u
"type"_s] ==
"xyz"_L1 && !dsUri.
param( u
"url"_s ).startsWith(
"http"_L1 ) ) )
231 uriComponents.insert( u
"path"_s, dsUri.
param( u
"url"_s ) );
235 uriComponents.insert( u
"url"_s, dsUri.
param( u
"url"_s ) );
236 if ( dsUri.
hasParam( u
"urlName"_s ) )
237 uriComponents.insert( u
"urlName"_s, dsUri.
param( u
"urlName"_s ) );
242 QString url = dsUri.
param( u
"url_%2"_s.arg( i ) );
243 QString urlName = dsUri.
param( u
"urlName_%2"_s.arg( i ) );
244 if ( url.isEmpty() || urlName.isEmpty() )
246 uriComponents.insert( u
"urlName_%2"_s.arg( i ), urlName );
247 uriComponents.insert( u
"url_%2"_s.arg( i ), url );
253 uriComponents.insert( u
"zmin"_s, dsUri.
param( u
"zmin"_s ) );
255 uriComponents.insert( u
"zmax"_s, dsUri.
param( u
"zmax"_s ) );
259 if ( dsUri.
hasParam( u
"styleUrl"_s ) )
260 uriComponents.insert( u
"styleUrl"_s, dsUri.
param( u
"styleUrl"_s ) );
263 if ( !authcfg.isEmpty() )
264 uriComponents.insert( u
"authcfg"_s, authcfg );
266 return uriComponents;
269QString QgsXyzVectorTileDataProviderMetadata::encodeUri(
const QVariantMap &parts )
const
272 dsUri.
setParam( u
"type"_s, u
"xyz"_s );
273 dsUri.
setParam( u
"url"_s, parts.value( parts.contains( u
"path"_s ) ? u
"path"_s : u
"url"_s ).toString() );
274 if ( parts.contains( u
"urlName"_s ) )
275 dsUri.
setParam( u
"urlName"_s, parts[u
"urlName"_s].toString() );
280 QString urlNameKey = u
"urlName_%2"_s.arg( i );
281 QString urlKey = u
"url_%2"_s.arg( i );
283 if ( !parts.contains( urlNameKey ) || !parts.contains( urlKey ) )
285 QString url = dsUri.
param( u
"url_%2"_s.arg( i ) );
286 QString urlName = dsUri.
param( u
"urlName_%2"_s.arg( i ) );
287 if ( url.isEmpty() || urlName.isEmpty() )
290 dsUri.
setParam( urlNameKey, parts[urlNameKey].toString() );
291 dsUri.
setParam( urlKey, parts[urlKey].toString() );
295 if ( parts.contains( u
"zmin"_s ) )
296 dsUri.
setParam( u
"zmin"_s, parts[u
"zmin"_s].toString() );
297 if ( parts.contains( u
"zmax"_s ) )
298 dsUri.
setParam( u
"zmax"_s, parts[u
"zmax"_s].toString() );
302 if ( parts.contains( u
"styleUrl"_s ) )
303 dsUri.
setParam( u
"styleUrl"_s, parts[u
"styleUrl"_s].toString() );
305 if ( parts.contains( u
"authcfg"_s ) )
311QString QgsXyzVectorTileDataProviderMetadata::absoluteToRelativeUri(
const QString &uri,
const QgsReadWriteContext &context )
const
316 QString sourcePath = dsUri.
param( u
"url"_s );
318 const QUrl sourceUrl( sourcePath );
319 if ( sourceUrl.isLocalFile() )
324 dsUri.
setParam( u
"url"_s, QUrl::fromLocalFile( relSrcUrl ).toString( QUrl::DecodeReserved ) );
331QString QgsXyzVectorTileDataProviderMetadata::relativeToAbsoluteUri(
const QString &uri,
const QgsReadWriteContext &context )
const
336 QString sourcePath = dsUri.
param( u
"url"_s );
338 const QUrl sourceUrl( sourcePath );
339 if ( sourceUrl.isLocalFile() )
343 dsUri.
setParam( u
"url"_s, QUrl::fromLocalFile( absSrcUrl ).toString( QUrl::DecodeReserved ) );
350QList<Qgis::LayerType> QgsXyzVectorTileDataProviderMetadata::supportedLayerTypes()
const
360QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
361 : QgsXyzVectorTileDataProviderBase( uri, providerOptions, flags )
366 const QString sourcePath = dsUri.
param( u
"url"_s );
369 QgsDebugError( u
"Invalid format of URL for XYZ source: "_s + sourcePath );
376 zMin = dsUri.
param( u
"zmin"_s ).toInt();
380 zMax = dsUri.
param( u
"zmax"_s ).toInt();
383 mExtent =
QgsRectangle( -20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892 );
388QgsXyzVectorTileDataProvider::QgsXyzVectorTileDataProvider(
const QgsXyzVectorTileDataProvider &other )
389 : QgsXyzVectorTileDataProviderBase( other )
391 mIsValid = other.mIsValid;
392 mExtent = other.mExtent;
393 mMatrixSet = other.mMatrixSet;
401QString QgsXyzVectorTileDataProvider::name()
const
405 return XYZ_DATA_PROVIDER_KEY;
408QString QgsXyzVectorTileDataProvider::description()
const
412 return XYZ_DATA_PROVIDER_DESCRIPTION;
419 return new QgsXyzVectorTileDataProvider( *
this );
422bool QgsXyzVectorTileDataProvider::isValid()
const
429QgsRectangle QgsXyzVectorTileDataProvider::extent()
const
450QString QgsXyzVectorTileDataProvider::sourcePath()
const
456 return dsUri.param( u
"url"_s );
459QgsStringMap QgsXyzVectorTileDataProvider::sourcePaths()
const
466 QgsStringMap paths = { { dsUri.param( u
"urlName"_s ), dsUri.param( u
"url"_s ) } };
471 QString url = dsUri.param( u
"url_%2"_s.arg( i ) );
472 QString urlName = dsUri.param( u
"urlName_%2"_s.arg( i ) );
473 if ( url.isEmpty() || urlName.isEmpty() )
476 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(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
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.