QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
34 : mEventLoop( new QEventLoop )
35 , mFeedback( feedback )
41 connect( feedback, &
QgsFeedback::canceled,
this, &QgsVectorTileLoader::canceled, Qt::QueuedConnection );
54 loadFromNetworkAsync(
id, tileMatrix, uri );
62 if ( !mReplies.isEmpty() )
74 QgsDebugMsgLevel( QStringLiteral(
"downloadBlocking - not staring event loop - canceled" ), 2 );
78 QgsDebugMsgLevel( QStringLiteral(
"Starting event loop with %1 requests" ).arg( mReplies.count() ), 2 );
80 mEventLoop->exec( QEventLoop::ExcludeUserInputEvents );
84 Q_ASSERT( mReplies.isEmpty() );
87 void QgsVectorTileLoader::loadFromNetworkAsync(
const QgsTileXYZ &
id,
const QgsTileMatrix &tileMatrix,
const QString &requestUrl )
90 QNetworkRequest request( url );
94 request.setAttribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 1 ),
id.column() );
95 request.setAttribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 2 ),
id.row() );
96 request.setAttribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 3 ),
id.zoomLevel() );
98 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
99 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
113 void QgsVectorTileLoader::tileReplyFinished()
117 int reqX = reply->
request().attribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 1 ) ).toInt();
118 int reqY = reply->
request().attribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 2 ) ).toInt();
119 int reqZ = reply->
request().attribute(
static_cast<QNetworkRequest::Attribute
>( QNetworkRequest::User + 3 ) ).toInt();
122 if ( reply->
error() == QNetworkReply::NoError )
126 QgsDebugMsgLevel( QStringLiteral(
"Tile download successful: " ) + tileID.toString(), 2 );
127 QByteArray rawData = reply->
data();
128 mReplies.removeOne( reply );
129 reply->deleteLater();
135 if ( reply->
error() == QNetworkReply::ContentAccessDenied )
137 if ( reply->
data().isEmpty() )
138 mError = tr(
"Access denied" );
140 mError = tr(
"Access denied: %1" ).arg( QString( reply->
data() ) );
144 mReplies.removeOne( reply );
145 reply->deleteLater();
150 if ( mReplies.isEmpty() )
153 QMetaObject::invokeMethod( mEventLoop.get(),
"quit", Qt::QueuedConnection );
157 void QgsVectorTileLoader::canceled()
159 QgsDebugMsgLevel( QStringLiteral(
"Canceling %1 pending requests" ).arg( mReplies.count() ), 2 );
160 qDeleteAll( mReplies );
177 QList<QgsVectorTileRawData> rawTiles;
179 std::unique_ptr< QgsMbTiles > mbReader;
180 std::unique_ptr< QgsVtpkTiles > vtpkReader;
181 bool isUrl = ( sourceType == QLatin1String(
"xyz" ) );
182 if ( sourceType == QLatin1String(
"vtpk" ) )
184 vtpkReader = std::make_unique< QgsVtpkTiles >( sourcePath );
189 mbReader = std::make_unique< QgsMbTiles >( sourcePath );
190 bool res = mbReader->open();
201 if ( tiles.size() < 10000 )
204 rawTiles.reserve( tiles.size() );
205 for (
QgsTileXYZ id : std::as_const( tiles ) )
210 QByteArray rawData = isUrl ?
loadFromNetwork(
id, tileMatrix, sourcePath, authid, headers, feedback )
212 if ( !rawData.isEmpty() )
224 nr.setUrl( QUrl( url ) );
234 QgsDebugMsg( QStringLiteral(
"Request failed: " ) + url );
246 int rowTMS = pow( 2,
id.zoomLevel() ) -
id.row() - 1;
247 QByteArray gzippedTileData = mbTileReader.
tileData(
id.zoomLevel(),
id.column(), rowTMS );
248 if ( gzippedTileData.isEmpty() )
259 QgsDebugMsg( QStringLiteral(
"Failed to decompress tile " ) +
id.toString() );
263 QgsDebugMsgLevel( QStringLiteral(
"Tile blob size %1 -> uncompressed size %2" ).arg( gzippedTileData.size() ).arg( data.size() ), 2 );
269 QByteArray tileData = vtpkTileReader.
tileData(
id.zoomLevel(),
id.column(),
id.row() );
270 if ( tileData.isEmpty() )
Stores coordinates of a tile in a tile matrix set. Tile matrix is identified by the zoomLevel(),...
void finished()
Emitted when the reply has finished (either with a success or with a failure)
Range of tiles in a tile matrix to be rendered. The selection is rectangular, given by start/end row ...
#define QgsDebugMsgLevel(str, level)
static QgsTileDownloadManager * tileDownloadManager()
Returns the application's tile download manager, used for download of map tiles when rendering.
void canceled()
Internal routines can connect to this signal if they use event loop.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
Utility class for reading and writing ESRI VTPK files.
QByteArray tileData(int z, int x, int y)
Returns the raw tile data for the matching tile.
static void sortTilesByDistanceFromCenter(QVector< QgsTileXYZ > &tiles, QPointF center)
Orders tile requests according to the distance from view center (given in tile matrix coords)
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
#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 the zoom level of the tile matrix.
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
Utility class for reading and writing MBTiles files (which are SQLite3 databases).
QString errorString() const
Returns error string (only valid when already finished)
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 loadFromMBTiles(const QgsTileXYZ &id, QgsMbTiles &mbTileReader, QgsFeedback *feedback=nullptr)
Returns raw tile data for a single tile loaded from MBTiles file.
static QList< QgsVectorTileRawData > blockingFetchTileRawData(const QString &sourceType, const QString &sourcePath, const QgsTileMatrix &tileMatrix, const QPointF &viewCenter, const QgsTileRange &range, const QString &authid, const QgsHttpHeaders &headers, QgsFeedback *feedback=nullptr)
Returns raw tile data for the specified range of tiles. Blocks the caller until all tiles are fetched...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
CORE_EXPORT bool decodeGzip(const QByteArray &bytesIn, QByteArray &bytesOut)
Decodes gzip byte stream, returns true on success.
void setAuthCfg(const QString &authCfg)
Sets the authentication config id which should be used during the request.
QNetworkRequest request() const
Returns the original request for this reply object.
QString error() const
Returns a eventual error that occurred during loading, void if no error.
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).
@ NoError
No error was encountered.
QByteArray data() const
Returns binary data returned in the reply (only valid when already finished)
#define QgsSetRequestInitiatorId(request, str)
QByteArray tileData(int z, int x, int y) const
Returns raw tile data for given tile.
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 QByteArray loadFromNetwork(const QgsTileXYZ &id, const QgsTileMatrix &tileMatrix, const QString &requestUrl, const QString &authid, const QgsHttpHeaders &headers, QgsFeedback *feedback=nullptr)
Returns raw tile data for a single tile, doing a HTTP request. Block the caller until tile data are d...
static QByteArray loadFromVtpk(const QgsTileXYZ &id, QgsVtpkTiles &vtpkTileReader)
Returns raw tile data for a single tile loaded from VTPK file.
QByteArray content() const
Returns the reply content.
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
QgsVectorTileLoader(const QString &uri, const QgsTileMatrix &tileMatrix, const QgsTileRange &range, const QPointF &viewCenter, const QString &authid, const QgsHttpHeaders &headers, QgsFeedback *feedback)
Constructs tile loader for doing asynchronous requests and starts network requests.
QgsTileDownloadManagerReply * get(const QNetworkRequest &request)
Starts a request.
QNetworkReply::NetworkError error() const
Returns error code (only valid when already finished)
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
Reply object for tile download manager requests returned from calls to QgsTileDownloadManager::get().
static QVector< QgsTileXYZ > tilesInRange(QgsTileRange range, int zoomLevel)
Returns a list of tiles in the given tile range.