2021-08-30 20:31:33 +02:00
|
|
|
#include <QtMath>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include "common/rectc.h"
|
|
|
|
#include "demloader.h"
|
|
|
|
|
|
|
|
|
2021-09-01 13:08:34 +02:00
|
|
|
#define DOWNLOAD_LIMIT 16
|
2021-08-30 20:31:33 +02:00
|
|
|
|
|
|
|
static QList<DEM::Tile> tiles(const RectC &rect)
|
|
|
|
{
|
|
|
|
QList<DEM::Tile> list;
|
|
|
|
|
2021-09-21 22:07:22 +02:00
|
|
|
if (rect.isNull())
|
2021-08-30 20:31:33 +02:00
|
|
|
return list;
|
|
|
|
|
|
|
|
for (int i = qFloor(rect.top()); i >= qFloor(rect.bottom()); i--)
|
|
|
|
for (int j = qFloor(rect.left()); j <= qFloor(rect.right()); j++)
|
|
|
|
list.append(DEM::Tile(j, i));
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isZip(const QUrl &url)
|
|
|
|
{
|
|
|
|
QFileInfo fi(url.fileName());
|
|
|
|
return (fi.suffix().toLower() == "zip");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DEMLoader::DEMLoader(const QString &dir, QObject *parent)
|
|
|
|
: QObject(parent), _dir(dir)
|
|
|
|
{
|
|
|
|
_downloader = new Downloader(this);
|
|
|
|
connect(_downloader, &Downloader::finished, this, &DEMLoader::finished);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DEMLoader::loadTiles(const RectC &rect)
|
|
|
|
{
|
|
|
|
QList<DEM::Tile> tl(tiles(rect));
|
|
|
|
QList<Download> dl;
|
|
|
|
|
|
|
|
/* Create the user DEM dir only when a download is requested as it will
|
|
|
|
override the global DEM dir. */
|
2021-09-01 13:08:34 +02:00
|
|
|
if (!_dir.mkpath(_dir.absolutePath())) {
|
|
|
|
qWarning("%s: %s", qPrintable(_dir.canonicalPath()),
|
|
|
|
"Error creating DEM directory");
|
2021-08-30 20:31:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-01 13:08:34 +02:00
|
|
|
DEM::setDir(_dir.path());
|
2021-08-30 20:31:33 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < tl.size(); i++) {
|
|
|
|
const DEM::Tile &t = tl.at(i);
|
|
|
|
QString fn(tileFile(t));
|
|
|
|
QString zn(fn + ".zip");
|
|
|
|
|
2021-09-01 20:14:06 +02:00
|
|
|
if (!(QFileInfo::exists(zn) || QFileInfo::exists(fn))) {
|
2021-08-30 20:31:33 +02:00
|
|
|
QUrl url(tileUrl(t));
|
|
|
|
dl.append(Download(url, isZip(url) ? zn : fn));
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:02:24 +02:00
|
|
|
if (dl.size() > DOWNLOAD_LIMIT) {
|
|
|
|
qWarning("DEM download limit exceeded.");
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-30 20:31:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return _downloader->get(dl, _authorization);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DEMLoader::checkTiles(const RectC &rect) const
|
|
|
|
{
|
|
|
|
QList<DEM::Tile> tl(tiles(rect));
|
|
|
|
|
|
|
|
for (int i = 0; i < tl.size(); i++) {
|
|
|
|
const DEM::Tile &t = tl.at(i);
|
|
|
|
QString fn(tileFile(t));
|
|
|
|
QString zn(fn + ".zip");
|
|
|
|
|
2021-09-01 20:14:06 +02:00
|
|
|
if (!(QFileInfo::exists(zn) || QFileInfo::exists(fn)))
|
2021-08-30 20:31:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl DEMLoader::tileUrl(const DEM::Tile &tile) const
|
|
|
|
{
|
|
|
|
QString url(_url);
|
|
|
|
|
|
|
|
url.replace("$lon", tile.lonStr());
|
|
|
|
url.replace("$lat", tile.latStr());
|
|
|
|
|
|
|
|
return QUrl(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DEMLoader::tileFile(const DEM::Tile &tile) const
|
|
|
|
{
|
2021-09-01 13:08:34 +02:00
|
|
|
return _dir.absoluteFilePath(tile.baseName());
|
2021-08-30 20:31:33 +02:00
|
|
|
}
|