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

osm_xml_parser.hpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 09e63076ea152d1500151bba62d2304792879a75 (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
#pragma once

#include "../base/exception.hpp"

#include "../geometry/point2d.hpp"

#include "../std/vector.hpp"
#include "../std/map.hpp"
#include "../std/set.hpp"
#include "../std/string.hpp"

namespace osm
{
  typedef int64_t OsmId;
  typedef vector<OsmId> OsmIds;

  typedef pair<string, string> OsmTag;
  typedef vector<OsmTag> OsmTags;

  struct RelationMember
  {
    OsmId m_ref;
    string m_type;
    string m_role;
  };
  typedef vector<RelationMember> RelationMembers;

  class OsmIdAndTagHolder
  {
    OsmId m_id;
    OsmTags m_tags;

  public:
    OsmIdAndTagHolder(OsmId id, OsmTags const & tags);
    OsmId Id() const { return m_id; }
    bool TagValueByKey(string const & key, string & outValue) const;
    template <class TFunctor> void ForEachTag(TFunctor & functor) const
    {
      for (OsmTags::const_iterator it = m_tags.begin(); it != m_tags.end(); ++it)
        functor(*it);
    }
  };

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

  class OsmWay : public OsmIdAndTagHolder
  {
    OsmIds m_points;

  public:
    OsmWay(OsmId id, OsmTags const & tags, OsmIds const & pointIds);

    size_t PointsCount() const;

    /// checks if first and last points are equal
    bool IsClosed() const;

    /// Merges ways if they have one common point
    /// @warning do not use it where merged way direction is important! (coastlines for example)
    bool MergeWith(OsmWay const & way);

    template <class TFunctor> void ForEachPoint(TFunctor & functor) const
    {
      for (typename OsmIds::const_iterator it = m_points.begin(); it != m_points.end(); ++it)
        functor(*it);
    }
  };

  typedef vector<OsmWay> OsmWays;

  class OsmRelation : public OsmIdAndTagHolder
  {
    RelationMembers m_members;

  public:
    OsmRelation(OsmId id, OsmTags const & tags, RelationMembers const & members);
    OsmIds MembersByTypeAndRole(string const & type, string const & role) const;
  };

  class OsmRawData
  {
    typedef map<OsmId, OsmNode> nodes_type;
    nodes_type m_nodes;
    typedef map<OsmId, OsmWay> ways_type;
    ways_type m_ways;
    typedef map<OsmId, OsmRelation> relations_type;
    relations_type m_relations;

  public:
    DECLARE_EXCEPTION(OsmInvalidIdException, RootException);

    void AddNode(OsmId id, OsmTags const & tags, double lat, double lon);
    void AddWay(OsmId id, OsmTags const & tags, OsmIds const & nodeIds);
    void AddRelation(OsmId id, OsmTags const & tags, RelationMembers const & members);

    OsmNode NodeById(OsmId id) const throw (OsmInvalidIdException);
    OsmWay WayById(OsmId id) const throw (OsmInvalidIdException);
    OsmRelation RelationById(OsmId id) const throw (OsmInvalidIdException);

    OsmIds RelationsByKey(string const & key) const;
    OsmIds RelationsByTag(OsmTag const & tag) const;
  };

  class OsmXmlParser
  {
    vector<string> m_xmlTags;

    OsmRawData & m_osmRawData;

    OsmId m_id;
    double m_lon;
    double m_lat;
    /// <tag k="..." v="..."/>
    string m_k;
    string m_v;
    OsmTags m_tags;
    /// <nd ref="..." />
    OsmId m_ref;
    OsmIds m_nds;
    RelationMember m_member;
    RelationMembers m_members;

  public:
    OsmXmlParser(OsmRawData & outData);

    bool Push(string const & element);
    void Pop(string const & element);
    void AddAttr(string const & attr, string const & value);
    void CharData(string const &) {}
  };

}