Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can offline tiles be loaded #35

Open
kunalkhosla93 opened this issue Sep 4, 2023 · 1 comment
Open

Can offline tiles be loaded #35

kunalkhosla93 opened this issue Sep 4, 2023 · 1 comment

Comments

@kunalkhosla93
Copy link

Hi,

Is it possible to load offline tiles from a directory using this? if so how?

@raptorswing
Copy link
Owner

Hi. Sorry for the slow response. It is possible, but might require a bit of effort. The code supports different "Tile Source" implementations. These can have their own implementations for loading tiles (e.g. HTTP requests, drawing on the fly, or loading from disk). There isn't one built-in for loading from disk, but the base class that they all inherit from does have the ability to store downloaded/rendered tiles to a disk cache and then load from there instead of hitting the network or rendering the tiles again.

See, for example, parts of the caching implementation in MapTileSource:

QImage *MapTileSource::fromDiskCache(const QString &cacheID)
{
//Figure out x,y,z based on the cacheID
quint32 x,y,z;
if (!MapTileSource::cacheID2xyz(cacheID,&x,&y,&z))
return 0;
//See if we've got it in the cache
const QString path = this->getDiskCacheFile(x,y,z);
QFile fp(path);
if (!fp.exists())
return 0;
//Figure out when the tile we're loading from cache was supposed to expire
QDateTime expireTime = this->getTileExpirationTime(cacheID);
//If the cached tile is older than we would like, throw it out
if (QDateTime::currentDateTimeUtc().secsTo(expireTime) <= 0)
{
if (!QFile::remove(path))
qWarning() << "Failed to remove old cache file" << path;
return 0;
}
if (!fp.open(QFile::ReadOnly))
{
qWarning() << "Failed to open" << QFileInfo(fp.fileName()).baseName() << "from cache";
return 0;
}
QByteArray data;
quint64 counter = 0;
while (data.length() < fp.size())
{
data += fp.read(20480);
if (++counter >= MAX_DISK_CACHE_READ_ATTEMPTS)
{
qWarning() << "Reading cache file" << fp.fileName() << "took too long. Aborting.";
return 0;
}
}
QImage * image = new QImage();
if (!image->loadFromData(data))
{
delete image;
return 0;
}
return image;
}

You could conceivably implement a tile source that reuses this caching functionality, or maybe do something else. In either case, you'll probably want to implement a child class of MapTileSource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants