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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2023-05-15
* Description : geolocation engine based on Marble.
* (c) 2007-2022 Marble Team
* https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
*
* SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "O5mWriter.h"
// Qt includes
#include <QDataStream>
#include <QBuffer>
// Local includes
#include "GeoDataDocument.h"
#include "GeoDataLineString.h"
#include "GeoDataLinearRing.h"
#include "GeoDataPlacemark.h"
#include "GeoDataRelation.h"
#include "GeoDataPolygon.h"
#include "GeoDataBuilding.h"
#include "GeoDataMultiGeometry.h"
#include "GeoWriter.h"
#include "OsmPlacemarkData.h"
namespace Marble
{
QSet<QString> O5mWriter::m_blacklistedTags;
bool O5mWriter::write(QIODevice* device, const GeoDataDocument& document)
{
if (!device || !device->isWritable())
{
return false;
}
OsmConverter converter;
converter.read(&document);
QDataStream stream(device);
writeHeader(stream);
writeNodes(converter.nodes(), stream);
writeWays(converter.ways(), stream);
writeRelations(converter.relations(), stream);
writeTrailer(stream);
return true;
}
void O5mWriter::writeHeader(QDataStream& stream) const
{
stream << qint8(0xff); // o5m file start indicator
stream << qint8(0xe0); // o5m header block indicator
stream << qint8(0x04) << qint8(0x6f) << qint8(0x35) << qint8(0x6d) << qint8(0x32); // o5m header
}
void O5mWriter::writeNodes(const OsmConverter::Nodes& nodes, QDataStream& stream) const
{
if (nodes.empty())
{
return;
}
stream << qint8(0xff); // reset delta encoding counters
StringTable stringTable;
qint64 lastId = 0;
double lastLon = 0.0;
double lastLat = 0.0;
QByteArray bufferData;
QBuffer buffer(&bufferData);
for (auto const& node : nodes)
{
if (node.second.id() == lastId)
{
continue;
}
stream << qint8(0x10); // node section start indicator
bufferData.clear();
buffer.open(QIODevice::WriteOnly);
QDataStream bufferStream(&buffer);
OsmPlacemarkData const& osmData = node.second;
qint64 idDiff = osmData.id() - lastId;
writeSigned(idDiff, bufferStream);
writeVersion(osmData, bufferStream);
GeoDataCoordinates const& coordinates = node.first;
double const lon = coordinates.longitude(GeoDataCoordinates::Degree);
double const lat = coordinates.latitude(GeoDataCoordinates::Degree);
writeSigned(deltaTo(lon, lastLon), bufferStream);
writeSigned(deltaTo(lat, lastLat), bufferStream);
writeTags(osmData, stringTable, bufferStream);
buffer.close();
writeUnsigned(bufferData.size(), stream);
stream.writeRawData(bufferData.constData(), bufferData.size());
lastId = osmData.id();
lastLon = lon;
lastLat = lat;
}
}
void O5mWriter::writeWays(const OsmConverter::Ways& ways, QDataStream& stream) const
{
if (ways.empty())
{
return;
}
stream << qint8(0xff); // reset delta encoding counters
StringTable stringTable;
qint64 lastId = 0;
qint64 lastReferenceId = 0;
QByteArray bufferData;
QBuffer buffer(&bufferData);
QByteArray referencesBufferData;
QBuffer referencesBuffer(&referencesBufferData);
for (auto const& way : ways)
{
Q_ASSERT(way.first);
if (way.second.id() == lastId)
{
continue;
}
stream << qint8(0x11); // way start indicator
OsmPlacemarkData const& osmData = way.second;
bufferData.clear();
buffer.open(QIODevice::WriteOnly);
QDataStream bufferStream(&buffer);
qint64 idDiff = osmData.id() - lastId;
writeSigned(idDiff, bufferStream);
lastId = osmData.id();
writeVersion(osmData, bufferStream);
referencesBufferData.clear();
referencesBuffer.open(QIODevice::WriteOnly);
QDataStream referencesStream(&referencesBuffer);
writeReferences(*way.first, lastReferenceId, osmData, referencesStream);
referencesBuffer.close();
writeUnsigned(referencesBufferData.size(), bufferStream);
bufferStream.writeRawData(referencesBufferData.constData(), referencesBufferData.size());
writeTags(osmData, stringTable, bufferStream);
buffer.close();
writeUnsigned(bufferData.size(), stream);
stream.writeRawData(bufferData.constData(), bufferData.size());
}
}
void O5mWriter::writeRelations(const OsmConverter::Relations& relations, QDataStream& stream) const
{
if (relations.empty())
{
return;
}
stream << qint8(0xff); // reset delta encoding counters
StringTable stringTable;
qint64 lastId = 0;
qint64 lastReferenceId[3] = { 0, 0, 0 };
QByteArray bufferData;
QBuffer buffer(&bufferData);
QByteArray referencesBufferData;
QBuffer referencesBuffer(&referencesBufferData);
for (auto const& relation : relations)
{
if (relation.second.id() == lastId)
{
continue;
}
stream << qint8(0x12); // relation start indicator
OsmPlacemarkData const& osmData = relation.second;
bufferData.clear();
buffer.open(QIODevice::WriteOnly);
QDataStream bufferStream(&buffer);
qint64 idDiff = osmData.id() - lastId;
writeSigned(idDiff, bufferStream);
lastId = osmData.id();
writeVersion(osmData, bufferStream);
referencesBufferData.clear();
referencesBuffer.open(QIODevice::WriteOnly);
QDataStream referencesStream(&referencesBuffer);
if (const auto placemark = geodata_cast<GeoDataPlacemark>(relation.first))
{
if (const auto building = geodata_cast<GeoDataBuilding>(placemark->geometry()))
{
auto polygon = geodata_cast<GeoDataPolygon>(& static_cast<const GeoDataMultiGeometry*>(building->multiGeometry())->at(0));
Q_ASSERT(polygon);
writeMultipolygonMembers(*polygon, lastReferenceId, osmData, stringTable, referencesStream);
}
else
{
auto polygon = geodata_cast<GeoDataPolygon>(placemark->geometry());
Q_ASSERT(polygon);
writeMultipolygonMembers(*polygon, lastReferenceId, osmData, stringTable, referencesStream);
}
}
else if (const auto placemark = geodata_cast<GeoDataRelation>(relation.first))
{
writeRelationMembers(placemark, lastReferenceId, osmData, stringTable, referencesStream);
}
else
{
Q_ASSERT(false);
}
referencesBuffer.close();
writeUnsigned(referencesBufferData.size(), bufferStream);
bufferStream.writeRawData(referencesBufferData.constData(), referencesBufferData.size());
writeTags(osmData, stringTable, bufferStream);
buffer.close();
writeUnsigned(bufferData.size(), stream);
stream.writeRawData(bufferData.constData(), bufferData.size());
}
}
void O5mWriter::writeTrailer(QDataStream& stream) const
{
stream << qint8(0xfe); // o5m file end indicator
}
void O5mWriter::writeMultipolygonMembers(const GeoDataPolygon& polygon, qint64(&lastId)[3], const OsmPlacemarkData& osmData, StringTable& stringTable, QDataStream& stream) const
{
qint64 id = osmData.memberReference(-1).id();
qint64 idDiff = id - lastId[(int)OsmType::Way];<--- Shadowed declaration
writeSigned(idDiff, stream);
lastId[(int)OsmType::Way] = id;
writeStringPair(StringPair(QString::fromUtf8("1outer"), QString()), stringTable, stream); // type=way, role=outer
for (int index = 0; index < polygon.innerBoundaries().size(); ++index)
{
id = osmData.memberReference(index).id();
qint64 idDiff = id - lastId[(int)OsmType::Way];<--- Shadow variable
writeSigned(idDiff, stream);
writeStringPair(StringPair(QString::fromUtf8("1inner"), QString()), stringTable, stream); // type=way, role=inner
lastId[(int)OsmType::Way] = id;
}
}
void O5mWriter::writeRelationMembers(const GeoDataRelation* relation, qint64(&lastId)[3], const OsmPlacemarkData& osmData, O5mWriter::StringTable& stringTable, QDataStream& stream) const
{
Q_UNUSED(relation);
for (auto iter = osmData.relationReferencesBegin(), end = osmData.relationReferencesEnd(); iter != end; ++iter)
{
qint64 id = iter.key().id;
qint64 idDiff = id - lastId[(int)iter.key().type];
writeSigned(idDiff, stream);
const QString key = QLatin1Char('0' + (int)iter.key().type) + iter.value();
writeStringPair(StringPair(key, QString()), stringTable, stream);
lastId[(int)iter.key().type] = id;
}
}
void O5mWriter::writeReferences(const GeoDataLineString& lineString, qint64& lastId, const OsmPlacemarkData& osmData, QDataStream& stream) const
{
QVector<GeoDataCoordinates>::const_iterator it = lineString.constBegin();
QVector<GeoDataCoordinates>::ConstIterator const end = lineString.constEnd();
for (; it != end; ++it)
{
qint64 id = osmData.nodeReference(*it).id();
qint64 idDiff = id - lastId;
writeSigned(idDiff, stream);
lastId = id;
}
if (!lineString.isEmpty() && lineString.isClosed())
{
auto const startId = osmData.nodeReference(lineString.first()).id();
auto const endId = osmData.nodeReference(lineString.last()).id();
if (startId != endId)
{
qint64 idDiff = startId - lastId;
writeSigned(idDiff, stream);
lastId = startId;
}
}
}
void O5mWriter::writeVersion(const OsmPlacemarkData&, QDataStream& stream) const
{
stream << qint8(0x00); // no version information
/** @todo implement */
}
void O5mWriter::writeTags(const OsmPlacemarkData& osmData, StringTable& stringTable, QDataStream& stream) const
{
if (m_blacklistedTags.isEmpty())
{
m_blacklistedTags << QStringLiteral("mx:version");
m_blacklistedTags << QStringLiteral("mx:changeset");
m_blacklistedTags << QStringLiteral("mx:uid");
m_blacklistedTags << QStringLiteral("mx:visible");
m_blacklistedTags << QStringLiteral("mx:user");
m_blacklistedTags << QStringLiteral("mx:timestamp");
m_blacklistedTags << QStringLiteral("mx:action");
}
for (auto iter = osmData.tagsBegin(), end = osmData.tagsEnd(); iter != end; ++iter)
{
if (!m_blacklistedTags.contains(iter.key()))
{
writeStringPair(StringPair(iter.key(), iter.value()), stringTable, stream);
}
}
}
void O5mWriter::writeStringPair(const StringPair& pair, StringTable& stringTable, QDataStream& stream) const
{
Q_ASSERT(stringTable.size() <= 15000);
auto const iter = stringTable.constFind(pair);
if (iter == stringTable.cend())
{
m_stringPairBuffer.clear();
m_stringPairBuffer.push_back(char(0x00));
m_stringPairBuffer.push_back(pair.first.toUtf8());
if (!pair.second.isEmpty())
{
m_stringPairBuffer.push_back(char(0x00));
m_stringPairBuffer.push_back(pair.second.toUtf8());
}
m_stringPairBuffer.push_back(char(0x00));
stream.writeRawData(m_stringPairBuffer.constData(), m_stringPairBuffer.size());
bool const tooLong = (m_stringPairBuffer.size() - (pair.second.isEmpty() ? 2 : 3)) > 250;
bool const tableFull = stringTable.size() > 15000;
Q_ASSERT(!tableFull);
if (!tooLong && !tableFull)
{
/* When the table is full, old values could be re-used.
* See o5m spec. This is only relevant for large files and would
* need some kind of string popularity to be effective though. */
stringTable.insert(pair, stringTable.size());
}
}
else
{
auto const reference = stringTable.size() - iter.value();
Q_ASSERT(reference > 0);
Q_ASSERT(reference <= stringTable.size());
writeUnsigned(reference, stream);
}
}
void O5mWriter::writeSigned(qint64 value, QDataStream& stream) const
{
bool const negative = value < 0;
if (negative)
{
value = -value - 1;
}
quint8 word = (value >> 6) > 0 ? (1 << 7) : 0;
word |= ((value << 1) & 0x7e);
if (negative)
{
word |= 0x01;
}
value >>= 6;
stream << word;
while (value > 0)
{
word = ((value >> 7) > 0 ? 0x80 : 0x00) | (value & 0x7f);
stream << word;
value >>= 7;
}
}
void O5mWriter::writeUnsigned(quint32 value, QDataStream& stream) const
{
do
{
quint8 word = ((value >> 7) > 0 ? 0x80 : 0x00) | (value & 0x7f);
stream << word;
value >>= 7;
}
while (value > 0);
}
qint32 O5mWriter::deltaTo(double value, double previous) const
{
double const diff = value - previous;
return qRound(diff * 1e7);
}
MARBLE_ADD_WRITER(O5mWriter, QString::fromUtf8("o5m"))
} // namespace Marble
|