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

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

#include "search/v2/house_to_street_table.hpp"
#include "search/v2/mwm_context.hpp"

#include "indexer/feature.hpp"
#include "indexer/feature_algo.hpp"
#include "indexer/ftypes_matcher.hpp"
#include "indexer/index.hpp"
#include "indexer/scales.hpp"

#include "base/stl_helpers.hpp"

#include "std/limits.hpp"

namespace search
{
namespace
{
size_t constexpr kSimilarityThresholdPercent = 10;
int const kQueryScale = scales::GetUpperScale();
} // namespace

// static
double const ReverseGeocoder::kLookupRadiusM = 500.0;

ReverseGeocoder::ReverseGeocoder(Index const & index) : m_index(index) {}

void ReverseGeocoder::GetNearbyStreets(MwmSet::MwmId id, m2::PointD const & center,
                                       vector<Street> & streets) const
{
  m2::RectD const rect = GetLookupRect(center, kLookupRadiusM);

  auto const addStreet = [&](FeatureType & ft)
  {
    if (ft.GetFeatureType() != feature::GEOM_LINE ||
        !ftypes::IsStreetChecker::Instance()(ft))
    {
      return;
    }

    string name;
    static int8_t const lang = StringUtf8Multilang::GetLangIndex("default");
    if (!ft.GetName(lang, name))
      return;

    ASSERT(!name.empty(), ());

    streets.push_back({ft.GetID(), feature::GetMinDistanceMeters(ft, center), name});
  };

  MwmSet::MwmHandle mwmHandle = m_index.GetMwmHandleById(id);
  if (mwmHandle.IsAlive())
  {
    search::v2::MwmContext(move(mwmHandle)).ForEachFeature(rect, addStreet);
    sort(streets.begin(), streets.end(), my::CompareBy(&Street::m_distanceMeters));
  }
}

void ReverseGeocoder::GetNearbyStreets(FeatureType & ft, vector<Street> & streets) const
{
  ASSERT(ft.GetID().IsValid(), ());
  GetNearbyStreets(ft.GetID().m_mwmId, feature::GetCenter(ft), streets);
}

// static
size_t ReverseGeocoder::GetMatchedStreetIndex(string const & keyName,
                                              vector<Street> const & streets)
{
  strings::UniString const expected = strings::MakeUniString(keyName);

  // Find the exact match or the best match in kSimilarityTresholdPercent limit.
  size_t const count = streets.size();
  size_t result = count;
  size_t minPercent = kSimilarityThresholdPercent + 1;

  for (size_t i = 0; i < count; ++i)
  {
    string key;
    search::GetStreetNameAsKey(streets[i].m_name, key);
    strings::UniString const actual = strings::MakeUniString(key);

    size_t const editDistance =
        strings::EditDistance(expected.begin(), expected.end(), actual.begin(), actual.end());

    if (editDistance == 0)
      return i;

    if (actual.empty())
      continue;

    size_t const percent = editDistance * 100 / actual.size();
    if (percent < minPercent)
    {
      result = i;
      minPercent = percent;
    }
  }

  return result;
}

void ReverseGeocoder::GetNearbyAddress(m2::PointD const & center, Address & addr) const
{
  vector<Building> buildings;
  GetNearbyBuildings(center, buildings);

  vector<Street> streets;
  unique_ptr<search::v2::HouseToStreetTable> table;
  MwmSet::MwmHandle mwmHandle;

  for (auto const & b : buildings)
  {
    if (!table || mwmHandle.GetId() != b.m_id.m_mwmId)
    {
      mwmHandle = m_index.GetMwmHandleById(b.m_id.m_mwmId);
      if (!mwmHandle.IsAlive())
        continue;
      table = search::v2::HouseToStreetTable::Load(*mwmHandle.GetValue<MwmValue>());
    }

    GetNearbyStreets(b.m_id.m_mwmId, b.m_center, streets);

    uint32_t ind;
    if (table->Get(b.m_id.m_index, ind) && ind < streets.size())
    {
      addr.m_building = b;
      addr.m_street = streets[ind];
      return;
    }
  }
}

pair<vector<ReverseGeocoder::Street>, uint32_t>
ReverseGeocoder::GetNearbyFeatureStreets(FeatureType const & feature) const
{
  pair<vector<ReverseGeocoder::Street>, uint32_t> result;
  auto & streetIndex = result.second;
  streetIndex = numeric_limits<uint32_t>::max();

  FeatureID const fid = feature.GetID();
  MwmSet::MwmHandle const mwmHandle = m_index.GetMwmHandleById(fid.m_mwmId);
  if (!mwmHandle.IsAlive())
  {
    LOG(LWARNING, ("MWM for", feature, "is dead"));
    return result;
  }

  auto & streets = result.first;
  GetNearbyStreets(const_cast<FeatureType &>(feature), streets);

  unique_ptr<search::v2::HouseToStreetTable> const table =
      search::v2::HouseToStreetTable::Load(*mwmHandle.GetValue<MwmValue>());

  if (table->Get(fid.m_index, streetIndex) && streetIndex >= streets.size())
    LOG(LWARNING, ("Out of bound index", streetIndex, "for", feature));
  return result;
}

void ReverseGeocoder::GetNearbyBuildings(m2::PointD const & center, vector<Building> & buildings) const
{
  m2::RectD const rect = GetLookupRect(center, kLookupRadiusM);

  auto const addBuilding = [&](FeatureType const & ft)
  {
    if (!ftypes::IsBuildingChecker::Instance()(ft))
      return;

    // Skip empty house numbers.
    string const number = ft.GetHouseNumber();
    if (number.empty())
      return;

    buildings.push_back({ft.GetID(), feature::GetMinDistanceMeters(ft, center),
                         number, feature::GetCenter(ft)});
  };

  m_index.ForEachInRect(addBuilding, rect, kQueryScale);
  sort(buildings.begin(), buildings.end(), my::CompareBy(&Building::m_distanceMeters));
}

// static
m2::RectD ReverseGeocoder::GetLookupRect(m2::PointD const & center, double radiusM)
{
  return MercatorBounds::RectByCenterXYAndSizeInMeters(center, radiusM);
}

} // namespace search