Do not show local DEM tiles with unsupported file names
[GPXSee.git] / src / common / demloader.cpp
bloba460a7364b3fbd87fb38aeba41e566facd5d2271
1 #include <QtMath>
2 #include <QFileInfo>
3 #include "common/rectc.h"
4 #include "demloader.h"
7 static QList<DEM::Tile> tiles(const RectC &rect)
9 QList<DEM::Tile> list;
11 if (rect.isNull())
12 return list;
14 for (int i = qFloor(rect.top()); i >= qFloor(rect.bottom()); i--)
15 for (int j = qFloor(rect.left()); j <= qFloor(rect.right()); j++)
16 list.append(DEM::Tile(j, i));
18 return list;
21 static bool isZip(const QUrl &url)
23 QFileInfo fi(url.fileName());
24 return (fi.suffix().toLower() == "zip");
28 DEMLoader::DEMLoader(const QString &dir, QObject *parent)
29 : QObject(parent), _dir(dir)
31 _downloader = new Downloader(this);
32 connect(_downloader, &Downloader::finished, this, &DEMLoader::finished);
35 int DEMLoader::numTiles(const RectC &rect) const
37 QList<DEM::Tile> tl(tiles(rect));
38 int cnt = 0;
40 for (int i = 0; i < tl.size(); i++) {
41 const DEM::Tile &t = tl.at(i);
42 QString fn(tileFile(t));
43 QString zn(fn + ".zip");
45 if (!(QFileInfo::exists(zn) || QFileInfo::exists(fn)))
46 cnt++;
49 return cnt;
52 bool DEMLoader::loadTiles(const RectC &rect)
54 QList<DEM::Tile> tl(tiles(rect));
55 QList<Download> dl;
57 /* Create the user DEM dir only when a download is requested as it will
58 override the global DEM dir. */
59 if (!_dir.mkpath(_dir.absolutePath())) {
60 qWarning("%s: %s", qPrintable(_dir.canonicalPath()),
61 "Error creating DEM directory");
62 return false;
64 DEM::setDir(_dir.path());
66 for (int i = 0; i < tl.size(); i++) {
67 const DEM::Tile &t = tl.at(i);
68 QString fn(tileFile(t));
69 QString zn(fn + ".zip");
71 if (!(QFileInfo::exists(zn) || QFileInfo::exists(fn))) {
72 QUrl url(tileUrl(t));
73 dl.append(Download(url, isZip(url) ? zn : fn));
76 if (dl.size() > DEM_DOWNLOAD_LIMIT) {
77 qWarning("DEM download limit (%d) exceeded.", DEM_DOWNLOAD_LIMIT);
78 return false;
82 return _downloader->get(dl, _headers);
85 bool DEMLoader::checkTiles(const RectC &rect) const
87 QList<DEM::Tile> tl(tiles(rect));
89 for (int i = 0; i < tl.size(); i++) {
90 const DEM::Tile &t = tl.at(i);
91 QString fn(tileFile(t));
92 QString zn(fn + ".zip");
94 if (!(QFileInfo::exists(zn) || QFileInfo::exists(fn)))
95 return false;
98 return true;
101 QUrl DEMLoader::tileUrl(const DEM::Tile &tile) const
103 QString url(_url);
105 url.replace("$lon", tile.lonStr());
106 url.replace("$lat", tile.latStr());
108 return QUrl(url);
111 QString DEMLoader::tileFile(const DEM::Tile &tile) const
113 return _dir.absoluteFilePath(tile.fileName());
116 void DEMLoader::setAuthorization(const Authorization &authorization)
118 QList<HTTPHeader> headers;
119 if (!authorization.isNull())
120 headers.append(authorization.header());
121 _headers = headers;