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

osm_xml_parser.cpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aff442688dce8f0d89e73072b5d643a1d8631c01 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "osm_xml_parser.hpp"

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

namespace osm
{
  OsmIdAndTagHolder::OsmIdAndTagHolder(OsmId id, OsmTags const & tags)
    : m_id(id), m_tags(tags)
  {
  }

  bool OsmIdAndTagHolder::TagValueByKey(string const & key, string & outValue) const
  {
    for (OsmTags::const_iterator it = m_tags.begin(); it != m_tags.end(); ++it)
    {
      if (it->first == key)
      {
        outValue = it->second;
        return true;
      }
    }
    return false;
  }

  /////////////////////////////////////////////////////////
  OsmNode::OsmNode(OsmId id, OsmTags const & tags, double lat, double lon)
    : OsmIdAndTagHolder(id, tags), m_lat(lat), m_lon(lon)
  {
  }

  /////////////////////////////////////////////////////////

  OsmWay::OsmWay(OsmId id, OsmTags const & tags, OsmIds const & pointIds)
    : OsmIdAndTagHolder(id, tags), m_points(pointIds)
  {
    CHECK_GREATER_OR_EQUAL(m_points.size(), 2, ("Can't construct a way with less than 2 points", id));
  }

  size_t OsmWay::PointsCount() const
  {
    return m_points.size();
  }

  bool OsmWay::IsClosed() const
  {
    return m_points.front() == m_points.back();
  }

  bool OsmWay::MergeWith(OsmWay const & way)
  {
    size_t const oldSize = m_points.size();

    // first, try to connect our end with their start
    if (m_points.back() == way.m_points.front())
      m_points.insert(m_points.end(), ++way.m_points.begin(), way.m_points.end());
    // our end and their end
    else if (m_points.back() == way.m_points.back())
      m_points.insert(m_points.end(), ++way.m_points.rbegin(), way.m_points.rend());
    // our start and their end
    else if (m_points.front() == way.m_points.back())
      m_points.insert(m_points.begin(), way.m_points.begin(), --way.m_points.end());
    // our start and their start
    else if (m_points.front() == way.m_points.front())
      m_points.insert(m_points.begin(), way.m_points.rbegin(), --way.m_points.rend());

    return m_points.size() > oldSize;
  }

  ///////////////////////////////////////////////////////////////

  OsmRelation::OsmRelation(OsmId id, OsmTags const & tags, RelationMembers const & members)
    : OsmIdAndTagHolder(id, tags), m_members(members)
  {
    CHECK(!m_members.empty(), ("Can't construct a relation without members", id));
  }

  OsmIds OsmRelation::MembersByTypeAndRole(string const & type, string const & role) const
  {
    OsmIds result;
    for (RelationMembers::const_iterator it = m_members.begin(); it != m_members.end(); ++it)
      if (it->m_type == type && it->m_role == role)
        result.push_back(it->m_ref);
    return result;
  }

  ///////////////////////////////////////////////////////////////

  void OsmRawData::AddNode(OsmId id, OsmTags const & tags, double lat, double lon)
  {
    m_nodes.insert(nodes_type::value_type(id, OsmNode(id, tags, lat, lon)));
  }

  void OsmRawData::AddWay(OsmId id, OsmTags const & tags, OsmIds const & nodeIds)
  {
    m_ways.insert(ways_type::value_type(id, OsmWay(id, tags, nodeIds)));
  }

  void OsmRawData::AddRelation(OsmId id, OsmTags const & tags, RelationMembers const & members)
  {
    m_relations.insert(relations_type::value_type(id, OsmRelation(id, tags, members)));
  }

  OsmNode OsmRawData::NodeById(OsmId id) const
  {
    nodes_type::const_iterator found = m_nodes.find(id);
    if (found == m_nodes.end())
      MYTHROW( OsmInvalidIdException, (id, "node not found") );
    return found->second;
  }

  OsmWay OsmRawData::WayById(OsmId id) const
  {
    ways_type::const_iterator found = m_ways.find(id);
    if (found == m_ways.end())
      MYTHROW( OsmInvalidIdException, (id, "way not found") );
    return found->second;
  }

  OsmRelation OsmRawData::RelationById(OsmId id) const
  {
    relations_type::const_iterator found = m_relations.find(id);
    if (found == m_relations.end())
      MYTHROW( OsmInvalidIdException, (id, "relation not found") );
    return found->second;
  }

  OsmIds OsmRawData::RelationsByKey(string const & key) const
  {
    OsmIds result;
    string value;
    for (relations_type::const_iterator it = m_relations.begin(); it != m_relations.end(); ++it)
    {
      if (it->second.TagValueByKey(key, value))
        result.push_back(it->first);
    }
    return result;
  }

  OsmIds OsmRawData::RelationsByTag(OsmTag const & tag) const
  {
    OsmIds result;
    string value;
    for (relations_type::const_iterator it = m_relations.begin(); it != m_relations.end(); ++it)
    {
      if (it->second.TagValueByKey(tag.first, value) && value == tag.second)
        result.push_back(it->first);
    }
    return result;
  }

  /////////////////////////////////////////////////////////

  OsmXmlParser::OsmXmlParser(OsmRawData & outData)
    : m_osmRawData(outData)
  {
  }

  bool OsmXmlParser::Push(string const & element)
  {
    m_xmlTags.push_back(element);
    m_invalidTags.push_back(false);

    return true;
  }

  void OsmXmlParser::Pop(string const & element)
  {
    bool invalid = m_invalidTags.back();
    if (element == "node")
    {
      if (!invalid)
        m_osmRawData.AddNode(m_id, m_tags, m_lat, m_lon);
      m_tags.clear();
    }
    else if (element == "nd")
    {
      if (!invalid)
        m_nds.push_back(m_ref);
    }
    else if (element == "way")
    {
      if (!invalid)
        m_osmRawData.AddWay(m_id, m_tags, m_nds);
      m_nds.clear();
      m_tags.clear();
    }
    else if (element == "tag")
    {
      if (!invalid)
        m_tags.push_back(OsmTag(m_k, m_v));
    }
    else if (element == "member")
    {
      if (!invalid)
        m_members.push_back(m_member);
    }
    else if (element == "relation")
    {
      if (!invalid)
        m_osmRawData.AddRelation(m_id, m_tags, m_members);
      m_members.clear();
      m_tags.clear();
    }

    m_invalidTags.pop_back();
    m_xmlTags.pop_back();
  }

  void OsmXmlParser::AddAttr(string const & attr, string const & value)
  {
    string const & elem = m_xmlTags.back();
    if (attr == "id" && (elem == "node" || elem == "way" || elem == "relation"))
    {
      CHECK(strings::to_int64(value, m_id), ());
      CHECK_NOT_EQUAL(m_id, 0, ("id == 0 is invalid"));
    }
    else if (attr == "lat" && elem == "node")
    {
      CHECK(strings::to_double(value, m_lat), ());
    }
    else if (attr == "lon" && elem == "node")
    {
      CHECK(strings::to_double(value, m_lon), ());
    }
    else if (attr == "ref")
    {
      int64_t numVal;
      CHECK(strings::to_int64(value, numVal), ());
      if (elem == "nd")
        m_ref = numVal;
      else if (elem == "member")
        m_member.m_ref = numVal;
    }
    else if (attr == "k" && elem == "tag")
    {
      m_k = value;
    }
    else if (attr == "v" && elem == "tag")
    {
      m_v = value;
    }
    else if (attr == "type" && elem == "member")
    {
      m_member.m_type = value;
    }
    else if (attr == "role" && elem == "member")
    {
      m_member.m_role = value;
    }
    else if ((attr == "action" && value == "delete")
             || (attr == "visible" && value == "false"))
    {
      m_invalidTags.back() = true;
    }
  }

}