-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcachereader.cpp
248 lines (204 loc) · 6.72 KB
/
cachereader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* -*- coding: utf-8-unix -*-
*
* File: src/cachereader.cpp
*
* Copyright (C) 2021 Jukka Sirkka
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cachereader.h"
#include <QCryptographicHash>
#include <QStandardPaths>
#include <QFile>
#include <QDate>
#include <QDataStream>
#include "platform.h"
const GeoProjection* CacheReader::geoprojection() const {
return m_proj;
}
CacheReader::CacheReader()
: ChartFileReader("cache")
, m_proj(GeoProjection::CreateProjection("SimpleMercator"))
{}
CacheReader::~CacheReader() {
delete m_proj;
}
GeoProjection* CacheReader::configuredProjection(const QString &path) const {
auto id = CacheId(path);
const auto base = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
const auto cachePath = QString("%1/%2/%3").arg(base).arg(Platform::base_app_name()).arg(QString(id));
QFile file(cachePath);
if (!file.open(QFile::ReadOnly)) {
throw ChartFileError(QString("Cannot open %1 for reading").arg(cachePath));
}
QDataStream stream(&file);
stream.setVersion(QDataStream::Qt_5_6);
stream.setByteOrder(QDataStream::LittleEndian);
QByteArray magic(8, '0');
stream.readRawData(magic.data(), 8);
if (magic != id) {
throw ChartFileError(QString("%1 is not a proper cached chart file").arg(cachePath));
}
stream.setFloatingPointPrecision(QDataStream::DoublePrecision);
// header
double lng;
stream >> lng;
double lat;
stream >> lat;
auto ref = WGS84Point::fromLL(lng , lat);
file.close();
auto gp = GeoProjection::CreateProjection(m_proj->className());
gp->setReference(ref);
return gp;
}
S57ChartOutline CacheReader::readOutline(const QString &path, const GeoProjection*) const {
auto id = CacheId(path);
const auto base = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
const auto cachePath = QString("%1/%2/%3").arg(base).arg(Platform::base_app_name()).arg(QString(id));
QFile file(cachePath);
if (!file.open(QFile::ReadOnly)) {
throw ChartFileError(QString("Cannot open %1 for reading").arg(cachePath));
}
QDataStream stream(&file);
stream.setVersion(QDataStream::Qt_5_6);
stream.setByteOrder(QDataStream::LittleEndian);
QByteArray magic(8, '0');
stream.readRawData(magic.data(), 8);
if (magic != id) {
throw ChartFileError(QString("%1 is not a proper cached chart file").arg(cachePath));
}
stream.setFloatingPointPrecision(QDataStream::DoublePrecision);
// header
double lng;
double lat;
stream >> lng >> lat;
const auto ref = WGS84Point::fromLL(lng , lat);
// extent
// stream >> lng >> lat;
// const auto sw = WGS84Point::fromLL(lng , lat);
// stream >> lng >> lat;
// const auto ne = WGS84Point::fromLL(lng , lat);
// cov/nocov
// auto decodeCov = [&stream, &lng, &lat] (WGS84Polygon& tgt) {
// int Ncov;
// stream >> Ncov;
// for (int nc = 0; nc < Ncov; nc++) {
// int Npol;
// stream >> Npol;
// WGS84PointVector pol;
// for (int n = 0; n < Npol; n++) {
// stream >> lng >> lat;
// pol << WGS84Point::fromLL(lng , lat);
// }
// tgt.append(pol);
// }
// };
// WGS84Polygon cov;
// decodeCov(cov);
// WGS84Polygon nocov;
// decodeCov(nocov);
file.close();
// Only reference point needed
return S57ChartOutline(WGS84Point(),
WGS84Point(),
WGS84Polygon(),
WGS84Polygon(),
ref,
QSizeF(1., 1.),
1,
QDate(),
QDate());
}
void CacheReader::readChart(GL::VertexVector& vertices,
GL::IndexVector& indices,
S57::ObjectVector& objects,
const QString& path,
const GeoProjection*) const {
auto id = CacheId(path);
const auto base = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
const auto cachePath = QString("%1/%2/%3").arg(base).arg(Platform::base_app_name()).arg(QString(id));
QFile file(cachePath);
if (!file.open(QFile::ReadOnly)) {
throw ChartFileError(QString("Cannot open %1 for reading").arg(cachePath));
}
QDataStream stream(&file);
stream.setVersion(QDataStream::Qt_5_6);
stream.setByteOrder(QDataStream::LittleEndian);
QByteArray magic(8, '0');
stream.readRawData(magic.data(), 8);
if (magic != id) {
throw ChartFileError(QString("%1 is not a proper cached chart file").arg(cachePath));
}
stream.setFloatingPointPrecision(QDataStream::DoublePrecision);
// header
double dummy;
stream >> dummy >> dummy; // ref
// extent
// stream >> dummy >> dummy;
// stream >> dummy >> dummy;
// cov & nocov
// auto decodeCov = [&stream, &dummy] () {
// int Ncov;
// stream >> Ncov;
// for (int nc = 0; nc < Ncov; nc++) {
// int Npol;
// stream >> Npol;
// for (int n = 0; n < Npol; n++) {
// stream >> dummy >> dummy;
// }
// }
// };
// decodeCov();
// decodeCov();
stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
int Nc;
GLfloat v;
// vertices
stream >> Nc;
for (int n = 0; n < Nc; n++) {
stream >> v;
vertices << v;
stream >> v;
vertices << v;
}
// indices
int Ni;
stream >> Ni;
GLuint k;
for (int n = 0; n < Ni; n++) {
stream >> k;
indices << k;
}
// objects
int No;
stream >> No;
for (int n = 0; n < No; n++) {
objects.append(S57::Object::Decode(stream));
}
file.close();
if (file.open(QFile::ReadWrite)) { // update mtime - saner cache mgmt
QDataStream stream2(&file);
stream2.setVersion(QDataStream::Qt_5_6);
stream2.setByteOrder(QDataStream::LittleEndian);
// rewrite magic
stream2.writeRawData(id.constData(), 8);
file.close();
}
}
QByteArray CacheReader::CacheId(const QString& path) {
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(path.toUtf8());
// convert sha1 to base36 form and return first 8 bytes for use as string
return QByteArray::number(*reinterpret_cast<const quint64*>(hash.result().constData()), 36).left(8);
}