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

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

#include "storage/country_info_getter.hpp"

#include "indexer/ftypes_matcher.hpp"
#include "indexer/classificator.hpp"
#include "indexer/feature.hpp"
#include "indexer/scales.hpp"
#include "indexer/categories_holder.hpp"

#include "geometry/angles.hpp"

#include "platform/measurement_utils.hpp"

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

#ifndef OMIM_OS_LINUX
// Lib opening_hours is not built for Linux since stdlib doesn't have required functions.
#include "3party/opening_hours/parse.hpp"
#include "3party/opening_hours/rules_evaluation.hpp"
#endif


namespace search
{

/// All constants in meters.
double const DIST_EQUAL_RESULTS = 100.0;
double const DIST_SAME_STREET = 5000.0;


void ProcessMetadata(FeatureType const & ft, Result::Metadata & meta)
{
  ft.ParseMetadata();
  feature::Metadata const & src = ft.GetMetadata();

  meta.m_cuisine = src.Get(feature::Metadata::FMD_CUISINE);

#ifndef OMIM_OS_LINUX
  // Lib opening_hours is not built for Linux since stdlib doesn't have required functions.
  string const openHours = src.Get(feature::Metadata::FMD_OPEN_HOURS);
  if (!openHours.empty())
  {
    osmoh::TRuleSequences rules;
    Parse(openHours, rules);
    // TODO(mgsergio): Is there a way to report that openHours was not parsed?
    meta.m_isClosed = GetState(rules, time(nullptr)).IsClosed();
  }
#endif

  meta.m_stars = 0;
  (void) strings::to_int(src.Get(feature::Metadata::FMD_STARS), meta.m_stars);
  meta.m_stars = my::clamp(meta.m_stars, 0, 5);
}

namespace impl
{

template <class T> bool LessRankT(T const & r1, T const & r2)
{
  if (r1.m_rank != r2.m_rank)
    return (r1.m_rank > r2.m_rank);

  return (r1.m_distance < r2.m_distance);
}

template <class T> bool LessDistanceT(T const & r1, T const & r2)
{
  if (r1.m_distance != r2.m_distance)
    return (r1.m_distance < r2.m_distance);

  return (r1.m_rank > r2.m_rank);
}

PreResult1::PreResult1(FeatureID const & fID, uint8_t rank, m2::PointD const & center,
                       m2::PointD const & pivot, int8_t viewportID)
  : m_id(fID),
    m_center(center),
    m_rank(rank),
    m_viewportID(viewportID)
{
  ASSERT(m_id.IsValid(), ());

  CalcParams(pivot);
}

PreResult1::PreResult1(m2::PointD const & center, m2::PointD const & pivot)
  : m_center(center)
{
  CalcParams(pivot);
}

namespace
{

void AssertValid(m2::PointD const & p)
{
  ASSERT ( my::between_s(-180.0, 180.0, p.x), (p.x) );
  ASSERT ( my::between_s(-180.0, 180.0, p.y), (p.y) );
}

}

void PreResult1::CalcParams(m2::PointD const & pivot)
{
  AssertValid(m_center);
  m_distance = PointDistance(m_center, pivot);
}

bool PreResult1::LessRank(PreResult1 const & r1, PreResult1 const & r2)
{
  return LessRankT(r1, r2);
}

bool PreResult1::LessDistance(PreResult1 const & r1, PreResult1 const & r2)
{
  return LessDistanceT(r1, r2);
}

bool PreResult1::LessPointsForViewport(PreResult1 const & r1, PreResult1 const & r2)
{
  return r1.m_id < r2.m_id;
}

void PreResult2::CalcParams(m2::PointD const & pivot)
{
  m_distance = PointDistance(GetCenter(), pivot);
}

PreResult2::PreResult2(FeatureType const & f, PreResult1 const * p, m2::PointD const & pivot,
                       string const & displayName, string const & fileName)
  : m_id(f.GetID()),
    m_types(f),
    m_str(displayName),
    m_resultType(RESULT_FEATURE),
    m_geomType(f.GetFeatureType())
{
  ASSERT(m_id.IsValid(), ());
  ASSERT(!m_types.Empty(), ());

  m_types.SortBySpec();

  m_rank = p ? p->GetRank() : 0;

  m2::PointD fCenter;
  if (p && f.GetFeatureType() != feature::GEOM_POINT)
  {
    // Optimization tip - use precalculated center point if possible.
    fCenter = p->GetCenter();
  }
  else
    fCenter = f.GetLimitRect(FeatureType::WORST_GEOMETRY).Center();

  m_region.SetParams(fileName, fCenter);
  CalcParams(pivot);

  ProcessMetadata(f, m_metadata);
}

PreResult2::PreResult2(double lat, double lon)
  : m_str("(" + MeasurementUtils::FormatLatLon(lat, lon) + ")"),
    m_resultType(RESULT_LATLON)
{
  m_region.SetParams(string(), MercatorBounds::FromLatLon(lat, lon));
}

PreResult2::PreResult2(m2::PointD const & pt, string const & str, uint32_t type)
  : m_str(str), m_resultType(RESULT_BUILDING)
{
  m_region.SetParams(string(), pt);

  m_types.Assign(type);
}

namespace
{
  class SkipRegionInfo
  {
    static size_t const m_count = 2;
    uint32_t m_types[m_count];

  public:
    SkipRegionInfo()
    {
      char const * arr[][2] = {
        { "place", "continent" },
        { "place", "country" }
      };
      static_assert(m_count == ARRAY_SIZE(arr), "");

      Classificator const & c = classif();
      for (size_t i = 0; i < m_count; ++i)
        m_types[i] = c.GetTypeByPath(vector<string>(arr[i], arr[i] + 2));
    }

    bool IsSkip(uint32_t type) const
    {
      for (uint32_t t : m_types)
      {
        if (t == type)
          return true;
      }
      return false;
    }
  };
}

string PreResult2::GetRegionName(storage::CountryInfoGetter const & infoGetter,
                                 uint32_t fType) const
{
  static SkipRegionInfo const checker;
  if (checker.IsSkip(fType))
    return string();

  storage::CountryInfo info;
  m_region.GetRegion(infoGetter, info);
  return info.m_name;
}

Result PreResult2::GenerateFinalResult(storage::CountryInfoGetter const & infoGetter,
                                       CategoriesHolder const * pCat,
                                       set<uint32_t> const * pTypes, int8_t locale) const
{
  uint32_t const type = GetBestType(pTypes);
  string const regionName = GetRegionName(infoGetter, type);

  switch (m_resultType)
  {
  case RESULT_FEATURE:
    return Result(m_id, GetCenter(), m_str, regionName, ReadableFeatureType(pCat, type, locale)
              #ifdef DEBUG
                  + ' ' + strings::to_string(int(m_rank))
              #endif
                  , type, m_metadata);

  case RESULT_BUILDING:
    return Result(GetCenter(), m_str, regionName, ReadableFeatureType(pCat, type, locale));

  default:
    ASSERT_EQUAL(m_resultType, RESULT_LATLON, ());
    return Result(GetCenter(), m_str, regionName, string());
  }
}

Result PreResult2::GeneratePointResult(CategoriesHolder const * pCat, set<uint32_t> const * pTypes,
                                       int8_t locale) const
{
  uint32_t const type = GetBestType(pTypes);
  return Result(m_id, GetCenter(), m_str, ReadableFeatureType(pCat, type, locale));
}

bool PreResult2::LessRank(PreResult2 const & r1, PreResult2 const & r2)
{
  return LessRankT(r1, r2);
}

bool PreResult2::LessDistance(PreResult2 const & r1, PreResult2 const & r2)
{
  return LessDistanceT(r1, r2);
}

bool PreResult2::StrictEqualF::operator() (PreResult2 const & r) const
{
  if (m_r.m_resultType == r.m_resultType && m_r.m_resultType == RESULT_FEATURE)
  {
    if (m_r.IsEqualCommon(r))
      return (PointDistance(m_r.GetCenter(), r.GetCenter()) < DIST_EQUAL_RESULTS);
  }

  return false;
}

bool PreResult2::LessLinearTypesF::operator() (PreResult2 const & r1, PreResult2 const & r2) const
{
  if (r1.m_geomType != r2.m_geomType)
    return (r1.m_geomType < r2.m_geomType);

  if (r1.m_str != r2.m_str)
    return (r1.m_str < r2.m_str);

  uint32_t const t1 = r1.GetBestType();
  uint32_t const t2 = r2.GetBestType();
  if (t1 != t2)
    return (t1 < t2);

  // Should stay the best feature, after unique, so add this criteria:
  return (r1.m_distance < r2.m_distance);
}

bool PreResult2::EqualLinearTypesF::operator() (PreResult2 const & r1, PreResult2 const & r2) const
{
  // Note! Do compare for distance when filtering linear objects.
  // Otherwise we will skip the results for different parts of the map.
  return (r1.m_geomType == feature::GEOM_LINE &&
          r1.IsEqualCommon(r2) &&
          PointDistance(r1.GetCenter(), r2.GetCenter()) < DIST_SAME_STREET);
}

bool PreResult2::IsEqualCommon(PreResult2 const & r) const
{
  return (m_geomType == r.m_geomType &&
          GetBestType() == r.GetBestType() &&
          m_str == r.m_str);
}

bool PreResult2::IsStreet() const
{
  return (m_geomType == feature::GEOM_LINE &&
          ftypes::IsStreetChecker::Instance()(m_types));
}

string PreResult2::DebugPrint() const
{
  stringstream ss;
  ss << "{ IntermediateResult: " <<
        "Name: " << m_str <<
        "; Type: " << GetBestType() <<
        "; Rank: " << int(m_rank) <<
        "; Distance: " << m_distance << " }";
  return ss.str();
}

uint32_t PreResult2::GetBestType(set<uint32_t> const * pPrefferedTypes) const
{
  if (pPrefferedTypes)
  {
    for (uint32_t type : m_types)
    {
      if (pPrefferedTypes->count(type) > 0)
        return type;
    }
  }

  // Do type truncate (2-level is enough for search results) only for
  // non-preffered types (types from categories leave original).
  uint32_t type = m_types.GetBestType();
  ftype::TruncValue(type, 2);
  return type;
}

string PreResult2::ReadableFeatureType(CategoriesHolder const * pCat,
                                       uint32_t type, int8_t locale) const
{
  ASSERT_NOT_EQUAL(type, 0, ());
  if (pCat)
  {
    string name;
    if (pCat->GetNameByType(type, locale, name))
      return name;
  }

  return classif().GetReadableObjectName(type);
}

void PreResult2::RegionInfo::GetRegion(storage::CountryInfoGetter const & infoGetter,
                                       storage::CountryInfo & info) const
{
  if (!m_file.empty())
    infoGetter.GetRegionInfo(m_file, info);
  else
    infoGetter.GetRegionInfo(m_point, info);
}
}  // namespace search::impl
}  // namespace search