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
|
#pragma once
#include "search/feature_loader.hpp"
#include "search/projection_on_street.hpp"
#include "indexer/feature_decl.hpp"
#include "indexer/ftypes_matcher.hpp"
#include "geometry/point2d.hpp"
#include "base/macros.hpp"
#include "std/string.hpp"
#include "std/queue.hpp"
class Index;
namespace search
{
struct ParsedNumber
{
string m_fullN;
int m_startN, m_endN;
public:
/// @todo Pass correct "American" notation flag.
ParsedNumber(string const & number, bool american = false);
inline string const & GetNumber() const { return m_fullN; }
inline bool IsOdd() const { return (m_startN % 2 == 1); }
inline int GetIntNumber() const { return m_startN; }
bool IsIntersect(ParsedNumber const & number, int offset = 0) const;
};
class House
{
ParsedNumber m_number;
m2::PointD m_point;
public:
House(string const & number, m2::PointD const & point)
: m_number(number), m_point(point)
{
}
inline string const & GetNumber() const { return m_number.GetNumber(); }
inline int GetIntNumber() const { return m_number.GetIntNumber(); }
inline m2::PointD const & GetPosition() const { return m_point; }
/// @return \n
/// -1 - no match;
/// 0 - full match;
/// 1 - integer number match with odd (even).
/// 2 - integer number match.
int GetMatch(ParsedNumber const & number) const;
bool GetNearbyMatch(ParsedNumber const & number) const;
};
// NOTE: DO NOT DELETE instances of this class by a pointer/reference
// to ProjectionOnStreet, because both classes have non-virtual destructors.
struct HouseProjection : public ProjectionOnStreet
{
House const * m_house;
/// Distance in mercator, from street beginning to projection on street
double m_streetDistance;
inline bool IsOdd() const { return (m_house->GetIntNumber() % 2 == 1); }
struct LessDistance
{
bool operator() (HouseProjection const * p1, HouseProjection const * p2) const
{
return p1->m_distMeters < p2->m_distMeters;
}
};
class EqualHouse
{
House const * m_house;
public:
EqualHouse(House const * h) : m_house(h) {}
bool operator() (HouseProjection const * p) const { return m_house == p->m_house; }
};
};
// many features combines to street
class Street
{
string m_name;
string m_processedName;
public:
void SetName(string const & name);
vector<m2::PointD> m_points;
vector<HouseProjection> m_houses;
double m_length; /// Length in mercator
int m_number; /// Some ordered number after merging
bool m_housesRead;
Street() : m_length(0.0), m_number(-1), m_housesRead(false) {}
void Reverse();
void SortHousesProjection();
/// Get limit rect for street with ortho offset to the left and right.
m2::RectD GetLimitRect(double offsetMeters) const;
double GetLength() const;
double GetPrefixLength(size_t numSegs) const;
inline static bool IsSameStreets(Street const * s1, Street const * s2)
{
return s1->m_processedName == s2->m_processedName;
}
inline string const & GetDbgName() const { return m_processedName; }
inline string const & GetName() const { return m_name; }
};
class MergedStreet
{
double m_length;
public:
deque<Street *> m_cont;
MergedStreet() : m_length(0.0) {}
string const & GetDbgName() const;
string const & GetName() const;
bool IsHousesRead() const;
void FinishReadingHouses();
HouseProjection const * GetHousePivot(bool isOdd, bool & sign) const;
struct GreaterLength
{
bool operator() (MergedStreet const & s1, MergedStreet const & s2) const
{
return (s1.m_length > s2.m_length);
}
};
inline void Swap(MergedStreet & s)
{
m_cont.swap(s.m_cont);
std::swap(m_length, s.m_length);
}
public:
struct Index
{
size_t s, h;
Index() : s(0), h(0) {}
};
inline Index Begin() const
{
Index i;
Next(i);
return i;
}
inline void Inc(Index & i) const
{
++i.h;
Next(i);
}
inline bool IsEnd(Index const & i) const
{
return i.s == m_cont.size();
}
inline HouseProjection const & Get(Index const & i) const
{
ASSERT(!IsEnd(i), ());
return m_cont[i.s]->m_houses[i.h];
}
private:
void Erase(Index & i);
void Next(Index & i) const;
};
inline void swap(MergedStreet & s1, MergedStreet & s2)
{
s1.Swap(s2);
}
struct HouseResult
{
House const * m_house;
MergedStreet const * m_street;
HouseResult(House const * house, MergedStreet const * street) : m_house(house), m_street(street)
{
}
inline bool operator<(HouseResult const & a) const { return m_house < a.m_house; }
inline bool operator==(HouseResult const & a) const { return m_house == a.m_house; }
m2::PointD const & GetOrg() const { return m_house->GetPosition(); }
};
inline string DebugPrint(HouseResult const & r)
{
return r.m_house->GetNumber() + ", " + r.m_street->GetName();
}
class HouseDetector
{
FeatureLoader m_loader;
typedef map<FeatureID, Street *> StreetMapT;
StreetMapT m_id2st;
typedef map<FeatureID, House *> HouseMapT;
HouseMapT m_id2house;
vector<pair<m2::PointD, Street *> > m_end2st;
vector<MergedStreet> m_streets;
double m_metres2Mercator;
int m_streetNum;
double m_houseOffsetM;
typedef pair<Street *, bool> StreetPtr;
StreetPtr FindConnection(Street const * st, bool beg) const;
void MergeStreets(Street * st);
template <class ProjectionCalcT>
void ReadHouse(FeatureType const & f, Street * st, ProjectionCalcT & calc);
void ReadHouses(Street * st);
void SetMetres2Mercator(double factor);
double GetApprLengthMeters(int index) const;
public:
HouseDetector(Index const & index);
~HouseDetector();
int LoadStreets(vector<FeatureID> const & ids);
/// @return number of different joined streets.
int MergeStreets();
static int const DEFAULT_OFFSET_M = 200;
void ReadAllHouses(double offsetMeters = DEFAULT_OFFSET_M);
void GetHouseForName(string const & houseNumber, vector<HouseResult> & res);
void ClearCaches();
void ClearUnusedStreets(vector<FeatureID> const & ids);
};
}
|