KOSMIndoorMap

xmlwriter.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "xmlwriter.h"
8#include "datatypes.h"
9#include "element.h"
10
11#include <QXmlStreamWriter>
12
13using namespace Qt::Literals::StringLiterals;
14using namespace OSM;
15
16template <typename T>
17static void writeTags(QXmlStreamWriter &writer, const T &elem)
18{
19 for (const auto &tag : elem.tags) {
20 writer.writeStartElement(QStringLiteral("tag"));
21 writer.writeAttribute(QStringLiteral("k"), QString::fromUtf8(tag.key.name()));
22 writer.writeAttribute(QStringLiteral("v"), QString::fromUtf8(tag.value));
23 writer.writeEndElement();
24 }
25}
26
27void XmlWriter::writeToIODevice(const DataSet &dataSet, QIODevice *out)
28{
29 QXmlStreamWriter writer(out);
30 writer.setAutoFormatting(true);
31 writer.setAutoFormattingIndent(-1);
32
33 writer.writeStartDocument();
34 // using \123 instead of 'S' here prevents reuse lint from interpreting the following string as a license header for this file
35 writer.writeComment(QStringLiteral("\n \123PDX-FileCopyrightText: OpenStreetMap contributors\n \123PDX-License-Identifier: ODbL-1.0\n"));
36 writer.writeStartElement(QStringLiteral("osm"));
37 writer.writeAttribute(QStringLiteral("version"), QStringLiteral("0.6"));
38 writer.writeAttribute(QStringLiteral("generator"), QStringLiteral("KOSM"));
39
41 OSM::for_each(dataSet, [&bbox](auto elem) { bbox = OSM::unite(bbox, elem.boundingBox()); });
42 writer.writeStartElement(QStringLiteral("bounds"));
43 writer.writeAttribute(QStringLiteral("minlat"), QString::number(bbox.min.latF(), 'f', 10));;
44 writer.writeAttribute(QStringLiteral("minlon"), QString::number(bbox.min.lonF(), 'f', 10));;
45 writer.writeAttribute(QStringLiteral("maxlat"), QString::number(bbox.max.latF(), 'f', 10));;
46 writer.writeAttribute(QStringLiteral("maxlon"), QString::number(bbox.max.lonF(), 'f', 10));;
47 writer.writeEndElement();
48
49 for (const auto &node : dataSet.nodes) {
50 writer.writeStartElement(QStringLiteral("node"));
51 writer.writeAttribute(QStringLiteral("id"), QString::number(node.id));
52 writer.writeAttribute(QStringLiteral("lat"), QString::number(node.coordinate.latF(), 'f', 10));
53 writer.writeAttribute(QStringLiteral("lon"), QString::number(node.coordinate.lonF(), 'f', 10));
54 writeTags(writer, node);
55 writer.writeEndElement();
56 }
57
58 for (const auto &way : dataSet.ways) {
59 writer.writeStartElement(QStringLiteral("way"));
60 writer.writeAttribute(QStringLiteral("id"), QString::number(way.id));
61 for (const auto &nd : way.nodes) {
62 writer.writeStartElement(QStringLiteral("nd"));
63 writer.writeAttribute(QStringLiteral("ref"), QString::number(nd));
64 writer.writeEndElement();
65 }
66 writeTags(writer, way);
67 writer.writeEndElement();
68 }
69
70 for (const auto &rel : dataSet.relations) {
71 writer.writeStartElement(QStringLiteral("relation"));
72 writer.writeAttribute(QStringLiteral("id"), QString::number(rel.id));
73 for (const auto &mem : rel.members) {
74 writer.writeStartElement(QStringLiteral("member"));
75 writer.writeAttribute(u"type"_s, QLatin1StringView(OSM::typeName(mem.type())));
76 writer.writeAttribute(QStringLiteral("ref"), QString::number(mem.id));
77 if (!mem.role().isNull()) {
78 writer.writeAttribute(QStringLiteral("role"), QString::fromUtf8(mem.role().name()));
79 }
80 writer.writeEndElement();
81 }
82 writeTags(writer, rel);
83 writer.writeEndElement();
84 }
85
86 writer.writeEndElement();
87 writer.writeEndDocument();
88}
Bounding box, ie.
Definition datatypes.h:95
A set of nodes, ways and relations.
Definition datatypes.h:346
Low-level types and functions to work with raw OSM data as efficiently as possible.
KOSM_EXPORT const char * typeName(Type type)
Element type name.
Definition datatypes.cpp:11
QString fromUtf8(QByteArrayView str)
QString number(double n, char format, int precision)
void setAutoFormattingIndent(int spacesOrTabs)
void setAutoFormatting(bool enable)
void writeAttribute(QAnyStringView namespaceUri, QAnyStringView name, QAnyStringView value)
void writeComment(QAnyStringView text)
void writeEndDocument()
void writeEndElement()
void writeStartDocument()
void writeStartElement(QAnyStringView namespaceUri, QAnyStringView name)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:06:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.