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

editor_config.cpp « editor - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f82018bb2749a0fc2782ff9762a3be645bea5b27 (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
#include "editor/editor_config.hpp"

#include "base/stl_helpers.hpp"

#include <algorithm>
#include <cstring>
#include <unordered_map>

using namespace std;

namespace
{
using EType = feature::Metadata::EType;

// TODO(mgsergio): It would be nice to have this map generated from editor.config.
static unordered_map<string, EType> const kNamesToFMD = {
    {"cuisine", feature::Metadata::FMD_CUISINE},
    {"opening_hours", feature::Metadata::FMD_OPEN_HOURS},
    {"phone", feature::Metadata::FMD_PHONE_NUMBER},
    {"fax", feature::Metadata::FMD_FAX_NUMBER},
    {"stars", feature::Metadata::FMD_STARS},
    {"operator", feature::Metadata::FMD_OPERATOR},
    // {"", feature::Metadata::FMD_URL},
    {"website", feature::Metadata::FMD_WEBSITE},
    {"internet", feature::Metadata::FMD_INTERNET},
    {"ele", feature::Metadata::FMD_ELE},
    // {"", feature::Metadata::FMD_TURN_LANES},
    // {"", feature::Metadata::FMD_TURN_LANES_FORWARD},
    // {"", feature::Metadata::FMD_TURN_LANES_BACKWARD},
    {"email", feature::Metadata::FMD_EMAIL},
    {"postcode", feature::Metadata::FMD_POSTCODE},
    {"wikipedia", feature::Metadata::FMD_WIKIPEDIA},
    // {"", feature::Metadata::FMD_MAXSPEED},
    {"flats", feature::Metadata::FMD_FLATS},
    {"height", feature::Metadata::FMD_HEIGHT},
    // {"", feature::Metadata::FMD_MIN_HEIGHT},
    {"denomination", feature::Metadata::FMD_DENOMINATION},
    {"building:levels", feature::Metadata::FMD_BUILDING_LEVELS},
    {"level", feature::Metadata::FMD_LEVEL}
    // description
};

unordered_map<string, int> const kPriorityWeights = {{"high", 0}, {"", 1}, {"low", 2}};

bool TypeDescriptionFromXml(pugi::xml_node const & root, pugi::xml_node const & node,
                            editor::TypeAggregatedDescription & outDesc)
{
  if (!node || strcmp(node.attribute("editable").value(), "no") == 0)
    return false;

  auto const handleField = [&outDesc](string const & fieldName) {
    if (fieldName == "name")
    {
      outDesc.m_name = true;
      return;
    }

    if (fieldName == "street" || fieldName == "housenumber" || fieldName == "housename")
    {
      outDesc.m_address = true;
      return;
    }

    // TODO(mgsergio): Add support for non-metadata fields like atm, wheelchair, toilet etc.
    auto const it = kNamesToFMD.find(fieldName);
    ASSERT(it != end(kNamesToFMD), ("Wrong field:", fieldName));
    outDesc.m_editableFields.push_back(it->second);
  };

  for (auto const & xNode : node.select_nodes("include[@group]"))
  {
    auto const node = xNode.node();
    string const groupName = node.attribute("group").value();

    string const xpath = "/mapsme/editor/fields/field_group[@name='" + groupName + "']";
    auto const group = root.select_node(xpath.data()).node();
    ASSERT(group, ("No such group", groupName));

    for (auto const & fieldRefXName : group.select_nodes("field_ref/@name"))
    {
      auto const fieldName = fieldRefXName.attribute().value();
      handleField(fieldName);
    }
  }

  for (auto const & xNode : node.select_nodes("include[@field]"))
  {
    auto const node = xNode.node();
    string const fieldName = node.attribute("field").value();
    handleField(fieldName);
  }

  base::SortUnique(outDesc.m_editableFields);
  return true;
}

/// The priority is defined by elems order, except elements with priority="high".
vector<pugi::xml_node> GetPrioritizedTypes(pugi::xml_node const & node)
{
  vector<pugi::xml_node> result;
  for (auto const & xNode : node.select_nodes("/mapsme/editor/types/type[@id]"))
    result.push_back(xNode.node());
  stable_sort(begin(result), end(result),
              [](pugi::xml_node const & lhs, pugi::xml_node const & rhs) {
                auto const lhsWeight = kPriorityWeights.find(lhs.attribute("priority").value());
                auto const rhsWeight = kPriorityWeights.find(rhs.attribute("priority").value());

                CHECK(lhsWeight != kPriorityWeights.end(), (""));
                CHECK(rhsWeight != kPriorityWeights.end(), (""));

                return lhsWeight->second < rhsWeight->second;
              });
  return result;
}
}  // namespace

namespace editor
{
bool EditorConfig::GetTypeDescription(vector<string> classificatorTypes,
                                      TypeAggregatedDescription & outDesc) const
{
  bool isBuilding = false;
  vector<string> addTypes;
  for (auto it = classificatorTypes.begin(); it != classificatorTypes.end(); ++it)
  {
    if (*it == "building")
    {
      outDesc.m_address = isBuilding = true;
      outDesc.m_editableFields.push_back(feature::Metadata::FMD_BUILDING_LEVELS);
      outDesc.m_editableFields.push_back(feature::Metadata::FMD_POSTCODE);
      classificatorTypes.erase(it);
      break;
    }
    // Adding partial types for 2..N-1 parts of a N-part type.
    auto hyphenPos = it->find('-');
    while ((hyphenPos = it->find('-', hyphenPos + 1)) != string::npos)
    {
      addTypes.push_back(it->substr(0, hyphenPos));
    }
  }
  classificatorTypes.insert(classificatorTypes.end(), addTypes.begin(), addTypes.end());

  auto const typeNodes = GetPrioritizedTypes(m_document);
  auto const it =
      find_if(begin(typeNodes), end(typeNodes), [&classificatorTypes](pugi::xml_node const & node) {
        return find(begin(classificatorTypes), end(classificatorTypes),
                    node.attribute("id").value()) != end(classificatorTypes);
      });
  if (it == end(typeNodes))
    return isBuilding;

  return TypeDescriptionFromXml(m_document, *it, outDesc);
}

vector<string> EditorConfig::GetTypesThatCanBeAdded() const
{
  auto const xpathResult =
      m_document.select_nodes("/mapsme/editor/types/type[not(@can_add='no' or @editable='no')]");

  vector<string> result;
  for (auto const & xNode : xpathResult)
    result.emplace_back(xNode.node().attribute("id").value());
  return result;
}

void EditorConfig::SetConfig(pugi::xml_document const & doc) { m_document.reset(doc); }
}  // namespace editor