2019-01-06 18:45:09 +01:00
|
|
|
#include <QtEndian>
|
2019-10-31 17:47:58 +01:00
|
|
|
#include <QtMath>
|
2019-01-17 00:47:44 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
2019-01-06 18:45:09 +01:00
|
|
|
#include "common/coordinates.h"
|
|
|
|
#include "dem.h"
|
|
|
|
|
2019-01-22 23:00:02 +01:00
|
|
|
|
2019-01-06 18:45:09 +01:00
|
|
|
#define SRTM3_SAMPLES 1201
|
|
|
|
#define SRTM1_SAMPLES 3601
|
|
|
|
|
|
|
|
#define SRTM_SIZE(samples) \
|
|
|
|
((samples) * (samples) * 2)
|
|
|
|
|
2019-01-06 19:59:58 +01:00
|
|
|
static qreal interpolate(qreal dx, qreal dy, qreal p0, qreal p1, qreal p2,
|
|
|
|
qreal p3)
|
|
|
|
{
|
|
|
|
return p0 * (1 - dx) * (1 - dy) + p1 * dx * (1 - dy) + p2 * dy * (1 - dx)
|
|
|
|
+ p3 * dx * dy;
|
|
|
|
}
|
|
|
|
|
2019-01-22 23:00:02 +01:00
|
|
|
static qreal value(int col, int row, int samples, const QByteArray *data)
|
2019-01-06 19:59:58 +01:00
|
|
|
{
|
|
|
|
int pos = ((samples - 1 - row) * samples + col) * 2;
|
2019-01-22 23:00:02 +01:00
|
|
|
qint16 val = qFromBigEndian(*((const qint16*)(data->constData() + pos)));
|
2019-01-06 19:59:58 +01:00
|
|
|
|
|
|
|
return (val == -32768) ? NAN : val;
|
|
|
|
}
|
|
|
|
|
2019-01-22 23:00:02 +01:00
|
|
|
static qreal height(const Coordinates &c, const QByteArray *data)
|
2019-01-06 18:45:09 +01:00
|
|
|
{
|
|
|
|
int samples;
|
|
|
|
|
2019-01-22 23:00:02 +01:00
|
|
|
if (data->size() == SRTM_SIZE(SRTM3_SAMPLES))
|
2019-01-06 18:45:09 +01:00
|
|
|
samples = SRTM3_SAMPLES;
|
2019-01-22 23:00:02 +01:00
|
|
|
else if (data->size() == SRTM_SIZE(SRTM1_SAMPLES))
|
2019-01-06 18:45:09 +01:00
|
|
|
samples = SRTM1_SAMPLES;
|
|
|
|
else
|
|
|
|
return NAN;
|
|
|
|
|
2019-10-31 17:47:58 +01:00
|
|
|
int row = (int)((c.lat() - qFloor(c.lat())) * (samples - 1));
|
|
|
|
int col = (int)((c.lon() - qFloor(c.lon())) * (samples - 1));
|
|
|
|
qreal dx = ((c.lon() - qFloor(c.lon())) * (samples - 1)) - col;
|
|
|
|
qreal dy = ((c.lat() - qFloor(c.lat())) * (samples - 1)) - row;
|
2019-01-06 18:45:09 +01:00
|
|
|
|
2019-01-06 19:59:58 +01:00
|
|
|
qreal p0 = value(col, row, samples, data);
|
|
|
|
qreal p1 = value(col + 1, row, samples, data);
|
|
|
|
qreal p2 = value(col, row + 1, samples, data);
|
|
|
|
qreal p3 = value(col + 1, row + 1, samples, data);
|
2019-01-06 18:45:09 +01:00
|
|
|
|
2019-01-06 19:59:58 +01:00
|
|
|
return interpolate(dx, dy, p0, p1, p2, p3);
|
2019-01-06 18:45:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-17 00:47:44 +01:00
|
|
|
|
|
|
|
QString DEM::_dir;
|
2019-01-22 23:00:02 +01:00
|
|
|
QCache<DEM::Key, QByteArray> DEM::_data;
|
2019-01-17 00:47:44 +01:00
|
|
|
|
|
|
|
QString DEM::fileName(const Key &key)
|
2019-01-06 18:45:09 +01:00
|
|
|
{
|
2019-01-22 23:00:02 +01:00
|
|
|
const char ns = (key.lat() >= 0) ? 'N' : 'S';
|
|
|
|
const char ew = (key.lon() >= 0) ? 'E' : 'W';
|
2019-01-06 18:45:09 +01:00
|
|
|
|
|
|
|
QString basename = QString("%1%2%3%4.hgt").arg(ns)
|
2019-01-22 23:00:02 +01:00
|
|
|
.arg(qAbs(key.lat()), 2, 10, QChar('0')).arg(ew)
|
|
|
|
.arg(qAbs(key.lon()), 3, 10, QChar('0'));
|
2019-01-07 22:07:03 +01:00
|
|
|
return QDir(_dir).absoluteFilePath(basename);
|
2019-01-06 18:45:09 +01:00
|
|
|
}
|
|
|
|
|
2019-01-17 00:47:44 +01:00
|
|
|
void DEM::setDir(const QString &path)
|
|
|
|
{
|
|
|
|
_dir = path;
|
|
|
|
}
|
|
|
|
|
2019-01-06 18:45:09 +01:00
|
|
|
qreal DEM::elevation(const Coordinates &c)
|
|
|
|
{
|
2019-01-07 22:07:03 +01:00
|
|
|
if (_dir.isEmpty())
|
|
|
|
return NAN;
|
|
|
|
|
2019-10-31 17:47:58 +01:00
|
|
|
Key k(qFloor(c.lon()), qFloor(c.lat()));
|
2019-01-06 18:45:09 +01:00
|
|
|
|
2019-01-22 23:00:02 +01:00
|
|
|
QByteArray *ba = _data[k];
|
|
|
|
if (!ba) {
|
2019-01-06 18:45:09 +01:00
|
|
|
QFile file(fileName(k));
|
2019-01-06 21:15:49 +01:00
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2019-01-07 22:07:03 +01:00
|
|
|
qWarning("%s: %s", qPrintable(file.fileName()),
|
|
|
|
qPrintable(file.errorString()));
|
2019-01-22 23:00:02 +01:00
|
|
|
_data.insert(k, new QByteArray());
|
2019-01-06 18:45:09 +01:00
|
|
|
return NAN;
|
2019-01-06 21:15:49 +01:00
|
|
|
} else {
|
2019-01-22 23:00:02 +01:00
|
|
|
ba = new QByteArray(file.readAll());
|
2019-03-05 20:43:05 +01:00
|
|
|
qreal ele = height(c, ba);
|
2019-01-22 23:00:02 +01:00
|
|
|
_data.insert(k, ba);
|
2019-03-05 20:43:05 +01:00
|
|
|
return ele;
|
2019-01-06 18:45:09 +01:00
|
|
|
}
|
|
|
|
} else
|
2019-01-22 23:00:02 +01:00
|
|
|
return height(c, ba);
|
2019-01-06 18:45:09 +01:00
|
|
|
}
|