File indexing completed on 2024-05-12 04:42:17

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "datatypes.h"
0008 
0009 using namespace OSM;
0010 
0011 DataSet::DataSet() = default;
0012 DataSet::DataSet(DataSet &&) noexcept = default;
0013 DataSet::~DataSet() = default;
0014 
0015 DataSet& DataSet::operator=(DataSet &&) noexcept = default;
0016 
0017 TagKey DataSet::makeTagKey(const char *keyName, OSM::StringMemory keyMemOpt)
0018 {
0019     return m_tagKeyRegistry.makeKey(keyName, keyMemOpt);
0020 }
0021 
0022 Role DataSet::makeRole(const char *roleName, OSM::StringMemory memOpt)
0023 {
0024     return m_roleRegistry.makeKey(roleName, memOpt);
0025 }
0026 
0027 TagKey DataSet::tagKey(const char *keyName) const
0028 {
0029     return m_tagKeyRegistry.key(keyName);
0030 }
0031 
0032 Role DataSet::role(const char *roleName) const
0033 {
0034     return m_roleRegistry.key(roleName);
0035 }
0036 
0037 const Node* DataSet::node(Id id) const
0038 {
0039     const auto it = std::lower_bound(nodes.begin(), nodes.end(), id);
0040     if (it != nodes.end() && (*it).id == id) {
0041         return &(*it);
0042     }
0043     return nullptr;
0044 }
0045 
0046 const Way* DataSet::way(Id id) const
0047 {
0048     const auto it = std::lower_bound(ways.begin(), ways.end(), id);
0049     if (it != ways.end() && (*it).id == id) {
0050         return &(*it);
0051     }
0052     return nullptr;
0053 }
0054 
0055 Way* DataSet::way(Id id)
0056 {
0057     const auto it = std::lower_bound(ways.begin(), ways.end(), id);
0058     if (it != ways.end() && (*it).id == id) {
0059         return &(*it);
0060     }
0061     return nullptr;
0062 }
0063 
0064 const Relation* DataSet::relation(Id id) const
0065 {
0066     const auto it = std::lower_bound(relations.begin(), relations.end(), id);
0067     if (it != relations.end() && (*it).id == id) {
0068         return &(*it);
0069     }
0070     return nullptr;
0071 }
0072 
0073 void DataSet::addNode(Node &&node)
0074 {
0075     const auto it = std::lower_bound(nodes.begin(), nodes.end(), node);
0076     if (it != nodes.end() && (*it).id == node.id) {
0077         // do we need to merge something here?
0078         return;
0079     }
0080     nodes.insert(it, std::move(node));
0081 }
0082 
0083 void DataSet::addWay(Way &&way)
0084 {
0085     const auto it = std::lower_bound(ways.begin(), ways.end(), way);
0086     if (it != ways.end() && (*it).id == way.id) {
0087         // already there?
0088         return;
0089     }
0090     ways.insert(it, std::move(way));
0091 }
0092 
0093 void DataSet::addRelation(Relation &&rel)
0094 {
0095     const auto it = std::lower_bound(relations.begin(), relations.end(), rel);
0096     if (it != relations.end() && (*it).id == rel.id) {
0097         // do we need to merge something here?
0098         return;
0099     }
0100     relations.insert(it, std::move(rel));
0101 }
0102 
0103 OSM::Id DataSet::nextInternalId() const
0104 {
0105     static OSM::Id nextId = 0;
0106     return --nextId;
0107 }
0108 
0109 // resolve ids for elements split in Marble vector tiles
0110 template <typename T>
0111 static QString actualIdString(const T &elem)
0112 {
0113     const auto mxoid = OSM::tagValue(elem, "mx:oid");
0114     return mxoid.isEmpty() ? QString::number(elem.id) : QString::fromUtf8(mxoid);
0115 }
0116 
0117 QString OSM::Node::url() const
0118 {
0119     return QStringLiteral("https://openstreetmap.org/node/") + actualIdString(*this);
0120 }
0121 
0122 bool OSM::Way::isClosed() const
0123 {
0124     return nodes.size() >= 2 && nodes.front() == nodes.back();
0125 }
0126 
0127 QString OSM::Way::url() const
0128 {
0129     return QStringLiteral("https://openstreetmap.org/way/") + actualIdString(*this);
0130 }
0131 
0132 QString OSM::Relation::url() const
0133 {
0134     return QStringLiteral("https://openstreetmap.org/relation/") + actualIdString(*this);
0135 }
0136 
0137 QDebug operator<<(QDebug debug, OSM::Coordinate coord)
0138 {
0139     QDebugStateSaver saver(debug);
0140     debug.nospace() << '(' << coord.latF() << ',' << coord.lonF() << ')';
0141     return debug;
0142 }
0143 
0144 QDebug operator<<(QDebug debug, OSM::BoundingBox bbox)
0145 {
0146     QDebugStateSaver saver(debug);
0147     debug.nospace() << '[' << bbox.min.latF() << ',' << bbox.min.lonF() << '|' << bbox.max.latF() << ',' << bbox.max.lonF() << ']';
0148     return debug;
0149 }