Welcome to mirror list, hosted at ThFree Co, Russian Federation.

osm_element.hpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e55b1ff0713051eafde4564391176d9789af76be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#pragma once

#include "base/assert.hpp"
#include "base/geo_object_id.hpp"
#include "base/math.hpp"
#include "base/string_utils.hpp"

#include "std/string_view.hpp"

#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>

struct OsmElement
{
  enum class EntityType
  {
    Unknown = 0x0,
    Node = 0x6F6E, // "no"
    Way = 0x6177, // "wa"
    Relation = 0x6572, // "re"
    Tag = 0x6174, // "ta"
    Nd = 0x646E, // "nd"
    Member = 0x656D, // "me"
    Osm = 0x736F, // "os"
  };

  struct Member
  {
    uint64_t ref = 0;
    EntityType type = EntityType::Unknown;
    std::string role;

    Member() = default;
    Member(uint64_t ref, EntityType type, std::string const & role)
      : ref(ref), type(type), role(role)
    {}

    bool operator == (Member const & e) const
    {
      return ref == e.ref && type == e.type && role == e.role;
    }
  };

  struct Tag
  {
    std::string key;
    std::string value;

    Tag() = default;
    Tag(std::string const & k, std::string const & v) : key(k), value(v) {}

    bool operator == (Tag const & e) const
    {
      return key == e.key && value == e.value;
    }
    bool operator < (Tag const & e) const
    {
      if (key == e.key)
        return value < e.value;
      return key < e.key;
    }
  };

  EntityType type = EntityType::Unknown;
  uint64_t id = 0;
  double lon = 0;
  double lat = 0;
  uint64_t ref = 0;
  std::string k;
  std::string v;
  EntityType memberType = EntityType::Unknown;
  std::string role;

  std::vector<uint64_t> m_nds;
  std::vector<Member> m_members;
  std::vector<Tag> m_tags;

  void Clear()
  {
    type = EntityType::Unknown;
    id = 0;
    lon = 0;
    lat = 0;
    ref = 0;
    k.clear();
    v.clear();
    memberType = EntityType::Unknown;
    role.clear();

    m_nds.clear();
    m_members.clear();
    m_tags.clear();
  }

  std::string ToString(std::string const & shift = std::string()) const;

  std::vector<uint64_t> const & Nodes() const { return m_nds; }
  std::vector<Member> const & Members() const { return m_members; }
  std::vector<Tag> const & Tags() const { return m_tags; }

  bool IsNode() const { return type == EntityType::Node; }
  bool IsWay() const { return type == EntityType::Way; }
  bool IsRelation() const { return type == EntityType::Relation; }

  static EntityType StringToEntityType(std::string const & t)
  {
    if (t == "way")
      return EntityType::Way;
    if (t == "node")
      return EntityType::Node;
    if (t == "relation")
      return EntityType::Relation;
    ASSERT(false, ("Unknown type", t));
    return EntityType::Unknown;
  }

  bool operator==(OsmElement const & e) const
  {
    return type == e.type
        && id == e.id
        && base::AlmostEqualAbs(lon, e.lon, 1e-7)
        && base::AlmostEqualAbs(lat, e.lat, 1e-7)
        && ref == e.ref
        && k == e.k
        && v == e.v
        && memberType == e.memberType
        && role == e.role
        && m_nds == e.m_nds
        && m_members == e.m_members
        && m_tags == e.m_tags;
  }

  void AddNd(uint64_t ref) { m_nds.emplace_back(ref); }
  void AddMember(uint64_t ref, EntityType type, std::string const & role)
  {
    m_members.emplace_back(ref, type, role);
  }

  void AddTag(std::string_view const & k, std::string_view const & v);
  bool HasTag(std::string_view const & key) const;
  bool HasTag(std::string_view const & k, std::string_view const & v) const;
  bool HasAnyTag(std::unordered_multimap<std::string, std::string> const & tags) const;

  template <class Fn>
  void UpdateTag(std::string const & k, Fn && fn)
  {
    for (auto & tag : m_tags)
    {
      if (tag.key == k)
      {
        fn(tag.value);
        return;
      }
    }

    std::string v;
    fn(v);
    if (!v.empty())
      AddTag(k, v);
  }

  std::string GetTag(std::string const & key) const;
  std::string_view GetTagValue(std::string_view const & key, std::string_view const & defaultValue) const;
};

base::GeoObjectId GetGeoObjectId(OsmElement const & element);

std::string DebugPrint(OsmElement const & e);
std::string DebugPrint(OsmElement::EntityType e);
std::string DebugPrint(OsmElement::Tag const & tag);