2 WARNING: This code uses internal Qt API - the QZipReader class for reading
3 ZIP files - and things may break if Qt changes the API. For Qt5 this is not
4 a problem as we can "see the future" now and there are no changes in all
5 the supported Qt5 versions up to the last one (5.15). In Qt6 the class
6 might change or even disappear in the future, but this is very unlikely
7 as there were no changes for several years and The Qt Company's policy
8 is: "do not invest any resources into any desktop related stuff unless
9 absolutely necessary". There is an issue (QTBUG-3897) since the year 2009 to
10 include the ZIP reader into the public API, which aptly illustrates the
11 effort The Qt Company is willing to make about anything desktop related...
18 #include <QRegularExpression>
20 #include <private/qzipreader_p.h>
21 #include "common/rectc.h"
24 #define SRTM3_SAMPLES 1201
25 #define SRTM1_SAMPLES 3601
26 #define SRTM05_SAMPLES 7201
28 #define SRTM_SIZE(samples) \
29 ((samples) * (samples) * 2)
31 static qreal
interpolate(qreal dx
, qreal dy
, qreal p0
, qreal p1
, qreal p2
,
34 return p0
* (1 - dx
) * (1 - dy
) + p1
* dx
* (1 - dy
) + p2
* dy
* (1 - dx
)
38 static qreal
value(int col
, int row
, int samples
, const QByteArray
*data
)
40 int pos
= ((samples
- 1 - row
) * samples
+ col
) * 2;
41 qint16 val
= qFromBigEndian(*((const qint16
*)(data
->constData() + pos
)));
43 return (val
== -32768) ? NAN
: val
;
46 static qreal
height(const Coordinates
&c
, const QByteArray
*data
)
50 if (data
->size() == SRTM_SIZE(SRTM3_SAMPLES
))
51 samples
= SRTM3_SAMPLES
;
52 else if (data
->size() == SRTM_SIZE(SRTM1_SAMPLES
))
53 samples
= SRTM1_SAMPLES
;
54 else if (data
->size() == SRTM_SIZE(SRTM05_SAMPLES
))
55 samples
= SRTM05_SAMPLES
;
59 int row
= (int)((c
.lat() - qFloor(c
.lat())) * (samples
- 1));
60 int col
= (int)((c
.lon() - qFloor(c
.lon())) * (samples
- 1));
61 qreal dx
= ((c
.lon() - qFloor(c
.lon())) * (samples
- 1)) - col
;
62 qreal dy
= ((c
.lat() - qFloor(c
.lat())) * (samples
- 1)) - row
;
64 qreal p0
= value(col
, row
, samples
, data
);
65 qreal p1
= value(col
+ 1, row
, samples
, data
);
66 qreal p2
= value(col
, row
+ 1, samples
, data
);
67 qreal p3
= value(col
+ 1, row
+ 1, samples
, data
);
69 return interpolate(dx
, dy
, p0
, p1
, p2
, p3
);
72 QString
DEM::Tile::latStr() const
74 const char ns
= (_lat
>= 0) ? 'N' : 'S';
75 return QString("%1%2").arg(ns
).arg(qAbs(_lat
), 2, 10, QChar('0'));
78 QString
DEM::Tile::lonStr() const
80 const char ew
= (_lon
>= 0) ? 'E' : 'W';
81 return QString("%1%2").arg(ew
).arg(qAbs(_lon
), 3, 10, QChar('0'));
84 QString
DEM::Tile::baseName() const
86 return QString("%1%2.hgt").arg(latStr(), lonStr());
90 DEM::TileCache
DEM::_data
;
92 QString
DEM::fileName(const QString
&baseName
)
94 return QDir(_dir
).absoluteFilePath(baseName
);
97 void DEM::setCacheSize(int size
)
99 _data
.setMaxCost(size
);
102 void DEM::setDir(const QString
&path
)
107 void DEM::clearCache()
112 qreal
DEM::elevation(const Coordinates
&c
)
117 Tile
tile(qFloor(c
.lon()), qFloor(c
.lat()));
119 QByteArray
*ba
= _data
[tile
];
121 QString
bn(tile
.baseName());
122 QString
fn(fileName(bn
));
123 QString
zn(fn
+ ".zip");
125 if (QFileInfo::exists(zn
)) {
126 QZipReader
zip(zn
, QIODevice::ReadOnly
);
127 ba
= new QByteArray(zip
.fileData(bn
));
128 qreal ele
= height(c
, ba
);
129 _data
.insert(tile
, ba
, ba
->size());
133 if (!file
.open(QIODevice::ReadOnly
)) {
134 qWarning("%s: %s", qPrintable(file
.fileName()),
135 qPrintable(file
.errorString()));
136 _data
.insert(tile
, new QByteArray());
139 ba
= new QByteArray(file
.readAll());
140 qreal ele
= height(c
, ba
);
141 _data
.insert(tile
, ba
, ba
->size());
146 return height(c
, ba
);
149 QList
<Area
> DEM::tiles()
152 QFileInfoList
files(dir
.entryInfoList(QDir::Files
| QDir::Readable
));
153 QRegularExpression
re("([NS])([0-9]{2})([EW])([0-9]{3})");
154 QLocale
l(QLocale::system());
157 for (int i
= 0; i
< files
.size(); i
++) {
158 QString
basename(files
.at(i
).baseName());
159 QRegularExpressionMatch
match(re
.match(basename
));
160 if (!match
.hasMatch())
163 int lat
= match
.captured(2).toInt();
164 int lon
= match
.captured(4).toInt();
165 if (match
.captured(1) == "S")
167 if (match
.captured(3) == "W")
170 Area
area(RectC(Coordinates(lon
, lat
+ 1), Coordinates(lon
+ 1, lat
)));
171 area
.setName(basename
);
172 area
.setDescription(files
.at(i
).suffix().toUpper() + ", "
173 + l
.formattedDataSize(files
.at(i
).size()));
182 QDebug
operator<<(QDebug dbg
, const DEM::Tile
&tile
)
184 dbg
.nospace() << "Tile(" << tile
.baseName() << ")";
187 #endif // QT_NO_DEBUG