QGIS API Documentation 4.5.0-Master (9185df287ca)
Loading...
Searching...
No Matches
qgstilecache.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstilecache.h
3 --------------------------------------
4 Date : September 2016
5 Copyright : (C) 2016 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgstilecache.h"
17
18#include "qgsapplication.h"
20
21#include <QAbstractNetworkCache>
22#include <QImage>
23#include <QUrl>
24
25// Default cache size is 256 tiles (considering tiles 256x256)
26// TODO: make this configurable!
27QCache<QUrl, QImage> QgsTileCache::sTileCache( 256 );
28QMutex QgsTileCache::sTileCacheMutex;
29
30
31void QgsTileCache::insertTile( const QUrl &url, const QImage &image )
32{
33 const QMutexLocker locker( &sTileCacheMutex );
34 // The cache default size is 256 tiles, but since this is also used for caching tiles
35 // from cascading WMS servers which can become quite large, we use the number of pixels
36 // in the image as multiple of a "standard" tile of 256x256 to calculate a cost for the cache.
37 // This way, the cache will be able to store more smaller tiles and less larger tiles
38 // using approximately the same amount of memory.
39 sTileCache.insert( url, new QImage( image ), std::max( static_cast<qsizetype>( 1 ), static_cast<qsizetype>( image.width() * image.height() / ( 256 * 256 ) ) ) );
40}
41
42bool QgsTileCache::tile( const QUrl &url, QImage &image )
43{
44 QNetworkRequest req( url );
45 //Preprocessing might alter the url, so we need to make sure we store/retrieve the url after preprocessing
47 const QUrl adjUrl = req.url();
48
49 const QMutexLocker locker( &sTileCacheMutex );
50 bool success = false;
51 if ( QImage *i = sTileCache.object( adjUrl ) )
52 {
53 image = *i;
54 success = true;
55 }
56 else if ( QgsNetworkAccessManager::instance()->cache()->metaData( adjUrl ).isValid() )
57 {
58 if ( QIODevice *data = QgsNetworkAccessManager::instance()->cache()->data( adjUrl ) )
59 {
60 const QByteArray imageData = data->readAll();
61 delete data;
62
63 image = QImage::fromData( imageData );
64
65 // cache it as well (mutex is already locked)
66 // Check for null because it could be a redirect (see: https://github.com/qgis/QGIS/issues/24336 )
67 if ( !image.isNull() )
68 {
69 sTileCache.insert( adjUrl, new QImage( image ) );
70 success = true;
71 }
72 }
73 }
74 return success;
75}
76
78{
79 const QMutexLocker locker( &sTileCacheMutex );
80 return sTileCache.totalCost();
81}
82
84{
85 const QMutexLocker locker( &sTileCacheMutex );
86 return sTileCache.maxCost();
87}
void preprocessRequest(QNetworkRequest *req) const
Preprocesses request.
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
static bool tile(const QUrl &url, QImage &image)
Try to access a tile and load it into "image" argument.
static int totalCost()
how many tiles are stored in the in-memory cache
static void insertTile(const QUrl &url, const QImage &image)
Add a tile image with given URL to the cache.
static int maxCost()
how many tiles can be stored in the in-memory cache