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

feature_utils.cpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 71a1930231203f8abdf6d4f849c4c4654b4cf68d (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
#include "feature_utils.hpp"
#include "classificator.hpp"
#include "../geometry/point2d.hpp"
#include "../base/base.hpp"
#include "../std/vector.hpp"

namespace feature
{

namespace impl
{

class FeatureEstimator
{
public:

  FeatureEstimator()
  {
    m_TypeContinent   = GetType("place", "continent");
    m_TypeCountry     = GetType("place", "country");
    m_TypeCity        = GetType("place", "city");
    m_TypeCityCapital = GetType("place", "city", "capital");
    m_TypeTown        = GetType("place", "town");
    m_TypeVillage     = GetType("place", "village");
  }

  m2::RectD GetViewport(FeatureType const & feature) const
  {
    m2::RectD limitRect = feature.GetLimitRect(-2);
    if (feature.GetFeatureType() != feature::GEOM_POINT)
      return limitRect;

    m2::PointD maxSizeMeters(100, 100);
    FeatureBase::GetTypesFn types;
    feature.ForEachTypeRef(types);
    for (size_t i = 0; i < types.m_size; ++i)
    {
      m2::PointD const sizeMeters = GetSizeForType(types.m_types[i], feature);
      maxSizeMeters.x = max(maxSizeMeters.x, sizeMeters.x);
      maxSizeMeters.y = max(maxSizeMeters.y, sizeMeters.y);
    }

    m2::PointD const centerXY = limitRect.Center();
    return MercatorBounds::RectByCenterXYAndSizeInMeters(centerXY.x, centerXY.y,
                                                         maxSizeMeters.x, maxSizeMeters.y);
  }

  uint8_t GetSearchRank(FeatureType const & feature) const
  {
    uint32_t const population = feature.GetPopulation();

    FeatureBase::GetTypesFn types;
    feature.ForEachTypeRef(types);
    for (size_t i = 0; i < types.m_size; ++i)
    {
      if (types.m_types[i] == m_TypeVillage)
        population = max(population, 1000);
      else if (types.m_types[i] == m_TypeTown)
        population = max(population, 10000);
      else if (types.m_types[i] == m_TypeCity)
        population = max(population, 100000);
      else if (types.m_types[i] == m_TypeCityCapital)
        population = max(population, 1000000);
      else if (types.m_types[i] == m_TypeCountry)
        population = max(population, 2000000);
      else if (types.m_types[i] == m_TypeContinent)
        population = max(population, 20000000);
    }

    return static_cast<uint8_t>(log(double(population)) / log(1.1));
  }

private:

  // Returns width and height (lon and lat) for a given type.
  m2::PointD GetSizeForType(uint32_t const type, FeatureType const & feature) const
  {
    static double const km = 1000.0;
    if (type == m_TypeContinent)
      return m2::PointD(5000*km, 5000*km);

    /// @todo Load countries bounding rects.
    if (type == m_TypeCountry)
      return m2::PointD(500*km, 500*km);

    if (type == m_TypeCity || type == m_TypeCityCapital)
    {
      double const radius = sqrt(static_cast<double>(feature.GetPopulation() / 3000));
      return m2::PointD(radius*km, radius*km);
    }

    if (type == m_TypeTown)
      return m2::PointD(8*km, 8*km);
    if (type == m_TypeVillage)
      return m2::PointD(3*km, 3*km);
    return m2::PointD(0, 0);
  }

  static uint32_t GetType(string const & s1,
                          string const & s2 = string(),
                          string const & s3 = string())
  {
    vector<string> path;
    path.push_back(s1);
    if (!s2.empty()) path.push_back(s2);
    if (!s3.empty()) path.push_back(s3);
    return classif().GetTypeByPath(path);
  }

  uint32_t m_TypeContinent;
  uint32_t m_TypeCountry;
  uint32_t m_TypeCity;
  uint32_t m_TypeCityCapital;
  uint32_t m_TypeTown;
  uint32_t m_TypeVillage;
};

FeatureEstimator const & GetFeatureEstimator()
{
  static FeatureEstimator const featureEstimator;
  return featureEstimator;
}

}  // namespace feature::impl

m2::RectD GetFeatureViewport(FeatureType const & feature)
{
  return impl::GetFeatureEstimator().GetViewport(feature);
}

uint8_t GetSearchRank(FeatureType const & feature)
{
  return impl::GetFeatureEstimator().GetSearchRank(feature);
}

} // namespace feature