Marble

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

KDE's Doxygen guidelines are available online.