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

osm2type.cpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: afba02cecd003ef9fdf1999066d5a0853f90e049 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "osm2type.hpp"
#include "xml_element.hpp"

#include "../indexer/classificator.hpp"
#include "../indexer/feature_visibility.hpp"

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

#include "../std/vector.hpp"

#include <QtCore/QString>


namespace ftype
{
  namespace
  {
    /// get value of mark (1 == "yes", -1 == "no", 0 == not a "yes\no")
    static int get_mark_value(string const & k, string const & v)
    {
      static char const * aTrue[] = { "yes", "true", "1", "*" };
      static char const * aFalse[] = { "no", "false", "-1" };

      strings::SimpleTokenizer it(v, "|");
      while (it)
      {
        if (strings::IsInArray(aTrue, *it)) return 1;
        if (strings::IsInArray(aFalse, *it)) return -1;
        ++it;
      }

      // "~" means no this tag, so sometimes it means true,
      // and all other cases - false. Choose according to key.
      if (v == "~")
        return (k == "access" ? 1 : -1);

      return 0;
    }

    bool is_skip_tag(string const & k)
    {
      // skip "cycleway's" tags because they interfer to set a valid types like "highway's"
      return (k == "created_by" || k == "description" || k == "cycleway" || k == "embankment");
    }

    template <class ToDo> class tags_wrapper
    {
      typedef typename ToDo::result_type res_t;

      string const & m_key;
      ToDo & m_toDo;
      res_t & m_res;

    public:
      tags_wrapper(string const & key, ToDo & toDo, res_t & res)
        : m_key(key), m_toDo(toDo), m_res(res) {}

      void operator() (string const & v)
      {
        if (!m_res)
          m_res = m_toDo(m_key, v);
      }
    };

    template <class ToDo>
    typename ToDo::result_type for_each_tag(XMLElement * p, ToDo toDo)
    {
      typedef typename ToDo::result_type res_t;

      res_t res = res_t();
      for (size_t i = 0; i < p->childs.size(); ++i)
      {
        if (p->childs[i].name == "tag")
        {
          string const & k = p->childs[i].attrs["k"];
          string const & v = p->childs[i].attrs["v"];

          if (k.empty() || is_skip_tag(k))
            continue;

          // this means "no"
          if (get_mark_value(k, v) == -1)
            continue;

          strings::Tokenize(v, ";", tags_wrapper<ToDo>(k, toDo, res));
          if (res) return res;
        }
      }
      return res;
    }

    bool is_name_tag(string const & k)
    {
      return (string::npos != k.find("name"));
    }

    class do_print
    {
      ostream & m_s;
    public:
      typedef bool result_type;

      do_print(ostream & s) : m_s(s) {}
      bool operator() (string const & k, string const & v) const
      {
        m_s << k << " <---> " << v << endl;
        return false;
      }
    };

    class do_find_name
    {
      set<string> m_savedNames;

      size_t & m_count;
      FeatureParams & m_params;
      bool m_tunnel;

    public:
      typedef bool result_type;

      do_find_name(size_t & count, FeatureParams & params)
        : m_count(count), m_params(params), m_tunnel(false)
      {
        m_count = 0;
      }
      ~do_find_name()
      {
        if (m_tunnel && m_params.layer < 0)
          m_params.layer = feature::LAYER_TRANSPARENT_TUNNEL;
      }

      bool GetLangByKey(string const & k, string & lang)
      {
        strings::SimpleTokenizer token(k, "\t :");
        if (!token)
          return false;

        // this is an international (latin) name
        if (*token == "int_name")
          lang = "int_name";
        else
        {
          if (*token == "name")
          {
            ++token;
            lang = (token ? *token : "default");

            // replace dummy arabian tag with correct tag
            if (lang == "ar1")
              lang = "ar";
          }
        }

        if (lang.empty())
          return false;

        // avoid duplicating names
        return m_savedNames.insert(lang).second;
      }

      bool operator() (string const & k, string const & v)
      {
        ++m_count;

        if (v.empty()) return false;

        // get name with language suffix
        string lang;
        if (GetLangByKey(k, lang))
        {
          // Unicode Compatibility Decomposition,
          // followed by Canonical Composition (NFKC).
          // Needed for better search matching
          QByteArray const normBytes = QString::fromUtf8(
                v.c_str()).normalized(QString::NormalizationForm_KC).toUtf8();
          m_params.name.AddString(lang, normBytes.constData());
        }

        // get layer
        if (k == "layer" && m_params.layer == 0)
        {
          m_params.layer = atoi(v.c_str());
          int8_t const bound = 10;
          m_params.layer = my::clamp(m_params.layer, -bound, bound);
        }

        // get reference (we process road numbers only)
        if (k == "ref")
          m_params.ref = v;

        // get house number
        if (k == "addr:housenumber") m_params.AddHouseNumber(v);
        if (k == "addr:housename") m_params.AddHouseName(v);

        // get population rank
        if (k == "population")
        {
          int n;
          if (strings::to_int(v, n))
            m_params.rank = static_cast<uint8_t>(log(double(n)) / log(1.1));
        }

        // set 'tunnel' flag
        if (k == "tunnel")
          m_tunnel = true;

        return false;
      }
    };

    class do_find_obj
    {
      ClassifObject const * m_parent;
      bool m_isKey;

      bool is_good_tag(string const & k, string const & v) const
      {
        if (is_name_tag(k))
          return false;

        int dummy;
        if (!m_isKey && strings::to_int(v, dummy))
          return (k == "admin_level");

        return true;
      }

    public:
      typedef ClassifObjectPtr result_type;

      do_find_obj(ClassifObject const * p, bool isKey) : m_parent(p), m_isKey(isKey) {}

      ClassifObjectPtr operator() (string const & k, string const & v) const
      {
        if (is_good_tag(k, v))
        {
          ClassifObjectPtr p = m_parent->BinaryFind(m_isKey ? k : v);
          if (p) return p;
        }
        return ClassifObjectPtr(0, 0);
      }
    };

    typedef vector<ClassifObjectPtr> path_type;

    class do_find_root_obj
    {
      set<string> const & m_skipTags;
      path_type & m_path;

    public:
      typedef ClassifObjectPtr result_type;

      do_find_root_obj(set<string> const & skipTags, path_type & path)
        : m_skipTags(skipTags), m_path(path)
      {
      }

      ClassifObjectPtr operator() (string const & k, string const & v) const
      {
        if (m_skipTags.find(k) == m_skipTags.end())
        {
          // first try to match key
          ClassifObjectPtr p = do_find_obj(classif().GetRoot(), true)(k, v);
          if (p)
          {
            m_path.push_back(p);

            // now try to match correspondent value
            p = do_find_obj(p.get(), false)(k, v);
            if (p) m_path.push_back(p);
          }
        }

        return (!m_path.empty() ? m_path.back() : ClassifObjectPtr(0, 0));
      }
    };
  }

  ClassifObjectPtr find_object(ClassifObject const * parent, XMLElement * p, bool isKey)
  {
    return for_each_tag(p, do_find_obj(parent, isKey));
  }

  size_t process_common_params(XMLElement * p, FeatureParams & params)
  {
    size_t count;
    for_each_tag(p, do_find_name(count, params));
    return count;
  }

//#ifdef DEBUG
//  class debug_find_string
//  {
//    string m_comp;
//  public:
//    debug_find_string(string const & comp) : m_comp(comp) {}
//    typedef bool result_type;
//    bool operator() (string const & k, string const & v) const
//    {
//      return (k == m_comp || v == m_comp);
//    }
//  };
//#endif

  void GetNameAndType(XMLElement * p, FeatureParams & params)
  {
//#ifdef DEBUG
//    // code to set a breakpoint
//    if (for_each_tag(p, debug_find_string("bridge")))
//    {
//      int break_here = 0;
//    }
//#endif

    // maybe an empty feature
    if (process_common_params(p, params) == 0)
      return;

    set<string> skipRootKeys;

    do
    {
      path_type path;

      // find first root object by key
      do_find_root_obj doFindRoot(skipRootKeys, path);
      (void)for_each_tag(p, doFindRoot);

      if (path.empty())
        break;

      // continue find path from last element
      do
      {
        // next objects trying to find by value first
        ClassifObjectPtr pObj = find_object(path.back().get(), p, false);
        if (!pObj)
        {
          // if no - try find object by key (in case of k = "area", v = "yes")
          pObj = find_object(path.back().get(), p, true);
        }

        // add to path or stop search
        if (pObj)
          path.push_back(pObj);
        else
          break;

      } while (true);

      // assign type
      uint32_t t = ftype::GetEmptyValue();
      for (size_t i = 0; i < path.size(); ++i)
        ftype::PushValue(t, path[i].GetIndex());

      // use features only with drawing rules
      if (feature::IsDrawableAny(t))
        params.AddType(t);

      // save this root to skip, and try again
      skipRootKeys.insert(path[0]->GetName());

    } while (true);
  }
}