Marble

KmlOsmPlacemarkDataTagWriter.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2015 Marius-Valeriu Stanciu <stanciumarius94@gmail.com>
4//
5
6#include "KmlOsmPlacemarkDataTagWriter.h"
7
8#include "GeoDataFeature.h"
9#include "GeoDataLinearRing.h"
10#include "GeoDataPlacemark.h"
11#include "GeoDataPolygon.h"
12#include "GeoDataSchemaData.h"
13#include "GeoWriter.h"
14#include "KmlElementDictionary.h"
15#include "osm/OsmPlacemarkData.h"
16
17namespace Marble
18{
19
21{
22 const auto placemark = geodata_cast<GeoDataPlacemark>(feature);
23
24 if (!placemark) {
25 return false;
26 }
27
28 const OsmPlacemarkData &osmData = placemark->osmData();
29 if (osmData.isNull()) {
30 return true;
31 }
32
33 writer.writeStartElement(QString::fromUtf8(kml::kmlTag_ExtendedData));
34
35 // We declare the "mx" namespace for the custom osmPlacemarkData XML schema
36 writer.writeNamespace(QString::fromUtf8(kml::kmlTag_nameSpaceMx), "mx");
37
38 KmlOsmPlacemarkDataTagWriter::writeOsmData(placemark->geometry(), osmData, writer);
39
40 writer.writeEndElement();
41 return true;
42}
43
44bool KmlOsmPlacemarkDataTagWriter::writeOsmData(const GeoDataGeometry *geometry, const OsmPlacemarkData &osmData, GeoWriter &writer)
45{
46 writer.writeStartElement(QString::fromUtf8(kml::kmlTag_nameSpaceMx), QString::fromUtf8(kml::kmlTag_OsmPlacemarkData));
47
48 // Writing the attributes
49 writer.writeAttribute("id", QString::number(osmData.id()));
50 writer.writeOptionalAttribute(QStringLiteral("changeset"), osmData.changeset());
51 writer.writeOptionalAttribute(QStringLiteral("timestamp"), osmData.timestamp());
52 writer.writeOptionalAttribute(QStringLiteral("uid"), osmData.uid());
53 writer.writeOptionalAttribute(QStringLiteral("user"), osmData.user());
54 writer.writeOptionalAttribute(QStringLiteral("version"), osmData.version());
55 writer.writeOptionalAttribute(QStringLiteral("visible"), osmData.isVisible());
56 writer.writeOptionalAttribute(QStringLiteral("action"), osmData.action());
57
58 // Writing the tags
60 QHash<QString, QString>::const_iterator tagsEnd = osmData.tagsEnd();
61 for (; tagsIt != tagsEnd; ++tagsIt) {
62 writer.writeStartElement(QString::fromUtf8(kml::kmlTag_nameSpaceMx), "tag");
63 writer.writeAttribute("k", tagsIt.key());
64 writer.writeAttribute("v", tagsIt.value());
65 writer.writeEndElement();
66 }
67 if (geometry) {
68 // Ways
69 if (const auto lineString = dynamic_cast<const GeoDataLineString *>(geometry)) {
70 int ndIndex = 0;
71
72 // Writing the component nodes
73 QList<GeoDataCoordinates>::const_iterator nodeIt = lineString->begin();
74 QList<GeoDataCoordinates>::const_iterator nodeEnd = lineString->end();
75 for (; nodeIt != nodeEnd; ++nodeIt) {
76 const OsmPlacemarkData &nodeOsmData = osmData.nodeReference(*nodeIt);
77 writer.writeStartElement(QString::fromUtf8(kml::kmlTag_nameSpaceMx), "nd");
78 writer.writeAttribute("index", QString::number(ndIndex++));
79 writeOsmData(nullptr, nodeOsmData, writer);
80 writer.writeEndElement();
81 }
82 }
83 // Polygons
84 else if (const auto polygon = geodata_cast<GeoDataPolygon>(geometry)) {
85 int memberIndex = -1;
86
87 // Writing the outerBoundary osmData
88 const GeoDataLinearRing &outerRing = polygon->outerBoundary();
89 const OsmPlacemarkData &outerRingOsmData = osmData.memberReference(memberIndex);
90 writer.writeStartElement(QString::fromUtf8(kml::kmlTag_nameSpaceMx), QString::fromUtf8(kml::kmlTag_member));
91 writer.writeAttribute("index", QString::number(memberIndex));
92 writeOsmData(&outerRing, outerRingOsmData, writer);
93 writer.writeEndElement();
94
95 // Writing the innerBoundaries
96 for (const GeoDataLinearRing &innerRing : polygon->innerBoundaries()) {
97 const OsmPlacemarkData &innerRingOsmData = osmData.memberReference(++memberIndex);
98 writer.writeStartElement(QString::fromUtf8(kml::kmlTag_nameSpaceMx), QString::fromUtf8(kml::kmlTag_member));
99 writer.writeAttribute("index", QString::number(memberIndex));
100 writeOsmData(&innerRing, innerRingOsmData, writer);
101 writer.writeEndElement();
102 }
103 }
104 }
105 writer.writeEndElement();
106 return true;
107}
108
109}
A base class for all geodata features.
A base class for all geodata features.
Standard Marble way of writing XML This class is intended to be a standardised way of writing XML for...
Definition GeoWriter.h:26
void writeOptionalAttribute(const QString &key, const QString &value, const QString &defaultValue=QString())
writeOptionalAttribute Convenience method to write k=v attributes if value is not equal to defaultVal...
Definition GeoWriter.cpp:96
static bool write(const GeoDataFeature *feature, GeoWriter &writer)
write function writes a custom XML schema made to store OsmPlacemarkData in a valid KML context
This class is used to encapsulate the osm data fields kept within a placemark's extendedData.
OsmPlacemarkData & nodeReference(const GeoDataCoordinates &coordinates)
this function returns the osmData associated with a nd
OsmPlacemarkData & memberReference(int key)
this function returns the osmData associated with a member boundary's index -1 represents the outer b...
bool isNull() const
isNull returns false if the osmData is loaded from a source or true if its just default constructed
QHash< QString, QString >::const_iterator tagsBegin() const
iterators for the tags hash.
Binds a QML item to a specific geodetic location in screen coordinates.
T * geodata_cast(GeoDataObject *node)
Returns the given node cast to type T if the node was instantiated as type T; otherwise returns 0.
QString fromUtf8(QByteArrayView str)
QString number(double n, char format, int precision)
void writeAttribute(QAnyStringView namespaceUri, QAnyStringView name, QAnyStringView value)
void writeEndElement()
void writeNamespace(QAnyStringView namespaceUri, QAnyStringView prefix)
void writeStartElement(QAnyStringView namespaceUri, QAnyStringView name)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:37:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.