Marble

OsmPlacemarkData.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2015 Marius-Valeriu Stanciu <stanciumarius94@gmail.com>
4//
5
6// Self
7#include "osm/OsmPlacemarkData.h"
8
9// Marble
10#include "GeoDataExtendedData.h"
11
12#include <QXmlStreamAttributes>
13
14namespace Marble
15{
16
17inline uint qHash(Marble::OsmIdentifier ident, uint seed)
18{
19 return ::qHash(ident.id, seed) ^ ::qHash((int)ident.type, seed);
20}
21
22OsmPlacemarkData::OsmPlacemarkData()
23 : m_id(0)
24 , m_href(std::make_shared<OsmPlacemarkDataHashRef>())
25{
26 // nothing to do
27}
28
29OsmPlacemarkData::~OsmPlacemarkData() = default;
30
31OsmPlacemarkDataHashRef *OsmPlacemarkData::hRef() const
32{
33 return m_href.get();
34}
35
36qint64 OsmPlacemarkData::id() const
37{
38 return m_id;
39}
40
41qint64 OsmPlacemarkData::oid() const
42{
43 auto const value = m_tags.value(QStringLiteral("mx:oid")).toLong();
44 return value > 0 ? value : m_id;
45}
46
47QString OsmPlacemarkData::changeset() const
48{
49 return m_tags.value(QStringLiteral("mx:changeset"));
50}
51
52QString OsmPlacemarkData::version() const
53{
54 return m_tags.value(QStringLiteral("mx:version"));
55}
56
57QString OsmPlacemarkData::uid() const
58{
59 return m_tags.value(QStringLiteral("mx:uid"));
60}
61
62QString OsmPlacemarkData::isVisible() const
63{
64 return m_tags.value(QStringLiteral("mx:visible"));
65}
66
67QString OsmPlacemarkData::user() const
68{
69 return m_tags.value(QStringLiteral("mx:user"));
70}
71
72QString OsmPlacemarkData::timestamp() const
73{
74 return m_tags.value(QStringLiteral("mx:timestamp"));
75}
76
77QString OsmPlacemarkData::action() const
78{
79 return m_tags.value(QStringLiteral("mx:action"));
80}
81
82void OsmPlacemarkData::setId(qint64 id)
83{
84 m_id = id;
85}
86
87void OsmPlacemarkData::setVersion(const QString &version)
88{
89 m_tags[QStringLiteral("mx:version")] = version;
90}
91
92void OsmPlacemarkData::setChangeset(const QString &changeset)
93{
94 m_tags[QStringLiteral("mx:changeset")] = changeset;
95}
96
97void OsmPlacemarkData::setUid(const QString &uid)
98{
99 m_tags[QStringLiteral("mx:uid")] = uid;
100}
101
102void OsmPlacemarkData::setVisible(const QString &visible)
103{
104 m_tags[QStringLiteral("mx:visible")] = visible;
105}
106
107void OsmPlacemarkData::setUser(const QString &user)
108{
109 m_tags[QStringLiteral("mx:user")] = user;
110}
111
112void OsmPlacemarkData::setTimestamp(const QString &timestamp)
113{
114 m_tags[QStringLiteral("mx:timestamp")] = timestamp;
115}
116
117void OsmPlacemarkData::setAction(const QString &action)
118{
119 m_tags[QStringLiteral("mx:action")] = action;
120}
121
122QString OsmPlacemarkData::tagValue(const QString &key) const
123{
124 return m_tags.value(key);
125}
126
127void OsmPlacemarkData::addTag(const QString &key, const QString &value)
128{
129 m_tags.insert(key, value);
130}
131
132void OsmPlacemarkData::removeTag(const QString &key)
133{
134 m_tags.remove(key);
135}
136
137bool OsmPlacemarkData::containsTag(const QString &key, const QString &value) const
138{
139 auto const iter = m_tags.constFind(key);
140 return iter == m_tags.constEnd() ? false : iter.value() == value;
141}
142
143bool OsmPlacemarkData::containsTagKey(const QString &key) const
144{
145 return m_tags.contains(key);
146}
147
148QHash<QString, QString>::const_iterator OsmPlacemarkData::findTag(const QString &key) const
149{
150 return m_tags.constFind(key);
151}
152
153QHash<QString, QString>::const_iterator OsmPlacemarkData::tagsBegin() const
154{
155 return m_tags.begin();
156}
157
158QHash<QString, QString>::const_iterator OsmPlacemarkData::tagsEnd() const
159{
160 return m_tags.constEnd();
161}
162
163OsmPlacemarkData &OsmPlacemarkData::nodeReference(const GeoDataCoordinates &coordinates)
164{
165 return m_href->m_nodeReferences[coordinates];
166}
167
168OsmPlacemarkData OsmPlacemarkData::nodeReference(const GeoDataCoordinates &coordinates) const
169{
170 return m_href->m_nodeReferences.value(coordinates);
171}
172
173void OsmPlacemarkData::addNodeReference(const GeoDataCoordinates &key, const OsmPlacemarkData &value)
174{
175 m_href->m_nodeReferences.insert(key, value);
176}
177
178void OsmPlacemarkData::removeNodeReference(const GeoDataCoordinates &key)
179{
180 m_href->m_nodeReferences.remove(key);
181}
182
183bool OsmPlacemarkData::containsNodeReference(const GeoDataCoordinates &key) const
184{
185 return m_href->m_nodeReferences.contains(key);
186}
187
188void OsmPlacemarkData::changeNodeReference(const GeoDataCoordinates &oldKey, const GeoDataCoordinates &newKey)
189{
190 m_href->m_nodeReferences.insert(newKey, m_href->m_nodeReferences.value(oldKey));
191 m_href->m_nodeReferences.remove(oldKey);
192}
193
194OsmPlacemarkData &OsmPlacemarkData::memberReference(int key)
195{
196 return m_href->m_memberReferences[key];
197}
198
199OsmPlacemarkData OsmPlacemarkData::memberReference(int key) const
200{
201 return m_href->m_memberReferences.value(key);
202}
203
204void OsmPlacemarkData::addMemberReference(int key, const OsmPlacemarkData &value)
205{
206 m_href->m_memberReferences.insert(key, value);
207}
208
209void OsmPlacemarkData::removeMemberReference(int key)
210{
211 // If an inner boundary is deleted, all indexes higher than the deleted one
212 // must be lowered by 1 to keep order.
214 QHash<int, OsmPlacemarkData>::iterator it = m_href->m_memberReferences.begin();
215 QHash<int, OsmPlacemarkData>::iterator end = m_href->m_memberReferences.end();
216
217 for (; it != end; ++it) {
218 if (it.key() > key) {
219 newHash.insert(it.key() - 1, it.value());
220 } else if (it.key() < key) {
221 newHash.insert(it.key(), it.value());
222 }
223 }
224 m_href->m_memberReferences = newHash;
225}
226
227bool OsmPlacemarkData::containsMemberReference(int key) const
228{
229 return m_href->m_memberReferences.contains(key);
230}
231
232void OsmPlacemarkData::addRelation(qint64 id, OsmType type, const QString &role)
233{
234 m_relationReferences.insert({id, type}, role);
235}
236
237void OsmPlacemarkData::removeRelation(qint64 id)
238{
239 /// ### this is wrong and just done this way for backward behavior compatible
240 /// ### this method should probably take type as an additional argument
241 m_relationReferences.remove({id, OsmType::Node});
242 m_relationReferences.remove({id, OsmType::Way});
243 m_relationReferences.remove({id, OsmType::Relation});
244}
245
246bool OsmPlacemarkData::containsRelation(qint64 id) const
247{
248 /// ### this is wrong and just done this way for backward behavior compatible
249 /// ### this method should probably take type as an additional argument
250 return m_relationReferences.contains({id, OsmType::Node}) || m_relationReferences.contains({id, OsmType::Way})
251 || m_relationReferences.contains({id, OsmType::Relation});
252}
253
254QHash<OsmIdentifier, QString>::const_iterator OsmPlacemarkData::relationReferencesBegin() const
255{
256 return m_relationReferences.begin();
257}
258
259QHash<OsmIdentifier, QString>::const_iterator OsmPlacemarkData::relationReferencesEnd() const
260{
261 return m_relationReferences.constEnd();
262}
263
264bool OsmPlacemarkData::isNull() const
265{
266 return !m_id;
267}
268
269bool OsmPlacemarkData::isEmpty() const
270{
271 return m_tags.isEmpty() && m_href->m_nodeReferences.isEmpty() && m_href->m_memberReferences.isEmpty() && m_relationReferences.isEmpty();
272}
273
274OsmPlacemarkData OsmPlacemarkData::fromParserAttributes(const QXmlStreamAttributes &attributes)
275{
276 OsmPlacemarkData osmData;
277 osmData.setId(attributes.value(QLatin1StringView("id")).toLongLong());
278 if (const auto s = attributes.value(QLatin1StringView("version")); !s.isEmpty()) {
279 osmData.setVersion(s.toString());
280 }
281 if (const auto s = attributes.value(QLatin1StringView("changeset")); !s.isEmpty()) {
282 osmData.setChangeset(s.toString());
283 }
284 if (const auto s = attributes.value(QLatin1StringView("user")); !s.isEmpty()) {
285 osmData.setUser(s.toString());
286 }
287 if (const auto s = attributes.value(QLatin1StringView("uid")); !s.isEmpty()) {
288 osmData.setUid(s.toString());
289 }
290 if (const auto s = attributes.value(QLatin1StringView("visible")); !s.isEmpty()) {
291 osmData.setVisible(s.toString());
292 }
293 if (const auto s = attributes.value(QLatin1StringView("timestamp")); !s.isEmpty()) {
294 osmData.setTimestamp(s.toString());
295 }
296 if (const auto s = attributes.value(QLatin1StringView("action")); !s.isEmpty()) {
297 osmData.setAction(s.toString());
298 }
299 return osmData;
300}
301
302const char *OsmPlacemarkData::nodeType() const
303{
304 return "OsmPlacemarkDataType";
305}
306
307// ---------------------------------------------------------------------------------------------------------
308
309OsmPlacemarkDataHashRef::OsmPlacemarkDataHashRef() = default;
310
311QHash<GeoDataCoordinates, OsmPlacemarkData> &OsmPlacemarkDataHashRef::nodeReferences()
312{
313 return m_nodeReferences;
314}
315
316QHash<GeoDataCoordinates, OsmPlacemarkData>::const_iterator OsmPlacemarkDataHashRef::nodeReferencesBegin() const
317{
318 return m_nodeReferences.begin();
319}
320
321QHash<GeoDataCoordinates, OsmPlacemarkData>::const_iterator OsmPlacemarkDataHashRef::nodeReferencesEnd() const
322{
323 return m_nodeReferences.constEnd();
324}
325
326QHash<int, OsmPlacemarkData> &OsmPlacemarkDataHashRef::memberReferences()
327{
328 return m_memberReferences;
329}
330
331QHash<int, OsmPlacemarkData>::const_iterator OsmPlacemarkDataHashRef::memberReferencesBegin() const
332{
333 return m_memberReferences.begin();
334}
335
336QHash<int, OsmPlacemarkData>::const_iterator OsmPlacemarkDataHashRef::memberReferencesEnd() const
337{
338 return m_memberReferences.constEnd();
339}
340
341}
A 3d point representation.
Container to host hash-table functions with OsmPlacemarkData as values.
This class is used to encapsulate the osm data fields kept within a placemark's extendedData.
KCOREADDONS_EXPORT unsigned int version()
KTEXTEDITOR_EXPORT size_t qHash(KTextEditor::Cursor cursor, size_t seed=0) noexcept
Binds a QML item to a specific geodetic location in screen coordinates.
OsmType
Type of OSM element.
bool contains(const Key &key) const const
iterator insert(const Key &key, const T &value)
const_iterator constEnd() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString & insert(qsizetype position, QChar ch)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
bool isEmpty() const const
QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const const
Identifier for an OSM element.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.