QGIS API Documentation
3.16.0-Hannover (43b64b13f3)
|
Go to the documentation of this file.
30 : mEventLoop( new QEventLoop )
31 , mFeedback( feedback )
37 connect( feedback, &
QgsFeedback::canceled,
this, &QgsVectorTileLoader::canceled, Qt::QueuedConnection );
48 for (
QgsTileXYZ id : qgis::as_const( tiles ) )
50 loadFromNetworkAsync(
id, tileMatrix, uri );
58 if ( !mReplies.isEmpty() )
70 QgsDebugMsgLevel( QStringLiteral(
"downloadBlocking - not staring event loop - canceled" ), 2 );
74 QgsDebugMsgLevel( QStringLiteral(
"Starting event loop with %1 requests" ).arg( mReplies.count() ), 2 );
76 mEventLoop->exec( QEventLoop::ExcludeUserInputEvents );
80 Q_ASSERT( mReplies.isEmpty() );
83 void QgsVectorTileLoader::loadFromNetworkAsync(
const QgsTileXYZ &
id,
const QgsTileMatrix &tileMatrix,
const QString &requestUrl )
86 QNetworkRequest request( url );
90 request.setAttribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 1 ),
id.column() );
91 request.setAttribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 2 ),
id.row() );
92 request.setAttribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 3 ),
id.zoomLevel() );
94 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
95 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
97 if ( !mReferer.isEmpty() )
98 request.setRawHeader(
"Referer", mReferer.toUtf8() );
106 connect( reply, &QNetworkReply::finished,
this, &QgsVectorTileLoader::tileReplyFinished );
111 void QgsVectorTileLoader::tileReplyFinished()
113 QNetworkReply *reply = qobject_cast<QNetworkReply *>( sender() );
115 int reqX = reply->request().attribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 1 ) ).toInt();
116 int reqY = reply->request().attribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 2 ) ).toInt();
117 int reqZ = reply->request().attribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 3 ) ).toInt();
120 if ( reply->error() == QNetworkReply::NoError )
124 QgsDebugMsgLevel( QStringLiteral(
"Tile download successful: " ) + tileID.toString(), 2 );
125 QByteArray rawData = reply->readAll();
126 mReplies.removeOne( reply );
127 reply->deleteLater();
133 QgsDebugMsg( QStringLiteral(
"Tile download failed! " ) + reply->errorString() );
134 mReplies.removeOne( reply );
135 reply->deleteLater();
140 if ( mReplies.isEmpty() )
143 QMetaObject::invokeMethod( mEventLoop.get(),
"quit", Qt::QueuedConnection );
147 void QgsVectorTileLoader::canceled()
149 QgsDebugMsgLevel( QStringLiteral(
"Canceling %1 pending requests" ).arg( mReplies.count() ), 2 );
150 const QList<QNetworkReply *> replies = mReplies;
151 for ( QNetworkReply *reply : replies )
161 QList<QgsVectorTileRawData> rawTiles;
164 bool isUrl = ( sourceType == QLatin1String(
"xyz" ) );
167 bool res = mbReader.
open();
174 for (
QgsTileXYZ id : qgis::as_const( tiles ) )
177 if ( !rawData.isEmpty() )
189 nr.setUrl( QUrl( url ) );
191 if ( !referer.isEmpty() )
192 nr.setRawHeader(
"Referer", referer.toUtf8() );
200 QgsDebugMsg( QStringLiteral(
"Request failed: " ) + url );
212 int rowTMS = pow( 2,
id.zoomLevel() ) -
id.row() - 1;
213 QByteArray gzippedTileData = mbTileReader.
tileData(
id.zoomLevel(),
id.column(), rowTMS );
214 if ( gzippedTileData.isEmpty() )
216 QgsDebugMsg( QStringLiteral(
"Failed to get tile " ) +
id.toString() );
223 QgsDebugMsg( QStringLiteral(
"Failed to decompress tile " ) +
id.toString() );
227 QgsDebugMsgLevel( QStringLiteral(
"Tile blob size %1 -> uncompressed size %2" ).arg( gzippedTileData.size() ).arg( data.size() ), 2 );
Stores coordinates of a tile in a tile matrix set.
Range of tiles in a tile matrix to be rendered.
#define QgsDebugMsgLevel(str, level)
void canceled()
Internal routines can connect to this signal if they use event loop.
static bool decodeGzip(const QByteArray &bytesIn, QByteArray &bytesOut)
Decodes gzip byte stream, returns true on success. Useful for reading vector tiles.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get() or post() request has been made.
bool open()
Tries to open the file, returns true on success.
static QList< QgsVectorTileRawData > blockingFetchTileRawData(const QString &sourceType, const QString &sourcePath, const QgsTileMatrix &tileMatrix, const QPointF &viewCenter, const QgsTileRange &range, const QString &authid, const QString &referer)
Returns raw tile data for the specified range of tiles. Blocks the caller until all tiles are fetched...
QByteArray tileData(int z, int x, int y)
Returns raw tile data for given tile.
#define QgsSetRequestInitiatorClass(request, _class)
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
void downloadBlocking()
Blocks the caller until all asynchronous requests are finished (with a success or a failure)
int zoomLevel() const
Returns zoom level of the tile matrix.
static QVector< QgsTileXYZ > tilesInRange(const QgsTileRange &range, int zoomLevel)
Returns a list of tiles in the given tile range.
Defines a matrix of tiles for a single zoom level: it is defined by its size (width * height) and map...
Utility class for reading and writing MBTiles files (which are SQLite3 databases).
void tileRequestFinished(const QgsVectorTileRawData &rawTile)
Emitted when a tile request has finished. If a tile request has failed, the returned raw tile byte ar...
static QByteArray loadFromNetwork(const QgsTileXYZ &id, const QgsTileMatrix &tileMatrix, const QString &requestUrl, const QString &authid, const QString &referer)
Returns raw tile data for a single tile, doing a HTTP request. Block the caller until tile data are d...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
static void sortTilesByDistanceFromCenter(QVector< QgsTileXYZ > &tiles, const QPointF ¢er)
Orders tile requests according to the distance from view center (given in tile matrix coords)
void setAuthCfg(const QString &authCfg)
Sets the authentication config id which should be used during the request.
@ NoError
No error was encountered.
#define QgsSetRequestInitiatorId(request, str)
static QByteArray loadFromMBTiles(const QgsTileXYZ &id, QgsMbTiles &mbTileReader)
Returns raw tile data for a single tile loaded from MBTiles file.
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
QgsVectorTileLoader(const QString &uri, const QgsTileMatrix &tileMatrix, const QgsTileRange &range, const QPointF &viewCenter, const QString &authid, const QString &referer, QgsFeedback *feedback)
Constructs tile loader for doing asynchronous requests and starts network requests.
bool isCanceled() const
Tells whether the operation has been canceled already.
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "get" operation on the specified request.
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...
Keeps track of raw tile data that need to be decoded.
bool updateNetworkRequest(QNetworkRequest &request, const QString &authcfg, const QString &dataprovider=QString())
Provider call to update a QNetworkRequest with an authentication config.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QByteArray content() const
Returns the reply content.
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...