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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
#include "indexer/feature_data.hpp"
#include "indexer/classificator.hpp"
#include "indexer/feature.hpp"
#include "indexer/feature_impl.hpp"
#include "indexer/feature_visibility.hpp"
#include "indexer/ftypes_matcher.hpp"
#include "base/assert.hpp"
#include "base/macros.hpp"
#include "base/stl_helpers.hpp"
#include "base/string_utils.hpp"
#include <algorithm>
#include <functional>
#include <vector>
using namespace feature;
using namespace std;
using namespace std::placeholders;
////////////////////////////////////////////////////////////////////////////////////
// TypesHolder implementation
////////////////////////////////////////////////////////////////////////////////////
namespace feature
{
string DebugPrint(TypesHolder const & holder)
{
Classificator const & c = classif();
string s;
for (uint32_t const type : holder)
s += c.GetReadableObjectName(type) + " ";
if (!s.empty())
s.pop_back();
return s;
}
TypesHolder::TypesHolder(FeatureType & f) : m_size(0), m_geoType(f.GetFeatureType())
{
f.ForEachType([this](uint32_t type)
{
Add(type);
});
}
void TypesHolder::Remove(uint32_t type)
{
UNUSED_VALUE(RemoveIf(base::EqualFunctor<uint32_t>(type)));
}
bool TypesHolder::Equals(TypesHolder const & other) const
{
vector<uint32_t> my(this->begin(), this->end());
vector<uint32_t> his(other.begin(), other.end());
sort(::begin(my), ::end(my));
sort(::begin(his), ::end(his));
return my == his;
}
} // namespace feature
namespace
{
class UselessTypesChecker
{
vector<uint32_t> m_types;
size_t m_count1;
template <size_t N, size_t M>
void AddTypes(char const * (&arr)[N][M])
{
Classificator const & c = classif();
for (size_t i = 0; i < N; ++i)
m_types.push_back(c.GetTypeByPath(vector<string>(arr[i], arr[i] + M)));
}
public:
UselessTypesChecker()
{
// Fill types that will be taken into account last,
// when we have many types for POI.
// 1-arity
char const * arr1[][1] = {
{ "building" },
{ "building:part" },
{ "hwtag" },
{ "psurface" },
{ "internet_access" },
{ "wheelchair" },
{ "sponsored" },
{ "entrance" },
{ "cuisine" },
};
AddTypes(arr1);
m_count1 = m_types.size();
// 2-arity
char const * arr2[][2] = {
{ "amenity", "atm" },
{ "amenity", "bench" },
{ "amenity", "shelter" },
{ "amenity", "toilets" },
{ "building", "address" },
{ "building", "has_parts" },
};
AddTypes(arr2);
}
bool operator() (uint32_t t) const
{
auto const end1 = m_types.begin() + m_count1;
// check 2-arity types
ftype::TruncValue(t, 2);
if (find(end1, m_types.end(), t) != m_types.end())
return true;
// check 1-arity types
ftype::TruncValue(t, 1);
if (find(m_types.begin(), end1, t) != end1)
return true;
return false;
}
};
} // namespace
namespace feature
{
uint8_t CalculateHeader(size_t const typesCount, uint8_t const headerGeomType,
FeatureParamsBase const & params)
{
ASSERT(typesCount != 0, ("Feature should have at least one type."));
uint8_t header = static_cast<uint8_t>(typesCount - 1);
if (!params.name.IsEmpty())
header |= HEADER_HAS_NAME;
if (params.layer != 0)
header |= HEADER_HAS_LAYER;
header |= headerGeomType;
// Geometry type for additional info is only one.
switch (headerGeomType)
{
case HEADER_GEOM_POINT:
if (params.rank != 0)
header |= HEADER_HAS_ADDINFO;
break;
case HEADER_GEOM_LINE:
if (!params.ref.empty())
header |= HEADER_HAS_ADDINFO;
break;
case HEADER_GEOM_AREA:
case HEADER_GEOM_POINT_EX:
if (!params.house.IsEmpty())
header |= HEADER_HAS_ADDINFO;
break;
}
return header;
}
void TypesHolder::SortBySpec()
{
if (m_size < 2)
return;
// Put "very common" types to the end of possible PP-description types.
static UselessTypesChecker checker;
UNUSED_VALUE(base::RemoveIfKeepValid(begin(), end(), [](uint32_t t) { return checker(t); }));
}
vector<string> TypesHolder::ToObjectNames() const
{
vector<string> result;
for (auto const type : *this)
result.push_back(classif().GetReadableObjectName(type));
return result;
}
} // namespace feature
////////////////////////////////////////////////////////////////////////////////////
// FeatureParamsBase implementation
////////////////////////////////////////////////////////////////////////////////////
void FeatureParamsBase::MakeZero()
{
layer = 0;
rank = 0;
ref.clear();
house.Clear();
name.Clear();
}
bool FeatureParamsBase::operator == (FeatureParamsBase const & rhs) const
{
return (name == rhs.name && house == rhs.house && ref == rhs.ref &&
layer == rhs.layer && rank == rhs.rank);
}
bool FeatureParamsBase::CheckValid() const
{
CHECK(layer > LAYER_LOW && layer < LAYER_HIGH, ());
return true;
}
string FeatureParamsBase::DebugString() const
{
string const utf8name = DebugPrint(name);
return ((!utf8name.empty() ? "Name:" + utf8name : "") +
(rank != 0 ? " Rank:" + DebugPrint(rank) : "") +
(!house.IsEmpty() ? " House:" + house.Get() : "") +
(!ref.empty() ? " Ref:" + ref : ""));
}
bool FeatureParamsBase::IsEmptyNames() const
{
return name.IsEmpty() && house.IsEmpty() && ref.empty();
}
namespace
{
// Most used dummy values are taken from
// https://taginfo.openstreetmap.org/keys/addr%3Ahousename#values
bool IsDummyName(string const & s)
{
return (s.empty() ||
s == "Bloc" || s == "bloc" ||
s == "жилой дом" ||
s == "Edificio" || s == "edificio");
}
}
/////////////////////////////////////////////////////////////////////////////////////////
// FeatureParams implementation
/////////////////////////////////////////////////////////////////////////////////////////
void FeatureParams::ClearName()
{
name.Clear();
}
bool FeatureParams::AddName(string const & lang, string const & s)
{
if (IsDummyName(s))
return false;
// The "default" new name will replace the old one if any (e.g. from AddHouseName call).
name.AddString(lang, s);
return true;
}
bool FeatureParams::AddHouseName(string const & s)
{
if (IsDummyName(s) || name.FindString(s) != StringUtf8Multilang::kUnsupportedLanguageCode)
return false;
// Most names are house numbers by statistics.
if (house.IsEmpty() && AddHouseNumber(s))
return true;
// If we got a clear number, replace the house number with it.
// Example: housename=16th Street, housenumber=34
if (strings::is_number(s))
{
string housename(house.Get());
if (AddHouseNumber(s))
{
// Duplicating code to avoid changing the method header.
string dummy;
if (!name.GetString(StringUtf8Multilang::kDefaultCode, dummy))
name.AddString(StringUtf8Multilang::kDefaultCode, housename);
return true;
}
}
// Add as a default name if we don't have it yet.
string dummy;
if (!name.GetString(StringUtf8Multilang::kDefaultCode, dummy))
{
name.AddString(StringUtf8Multilang::kDefaultCode, s);
return true;
}
return false;
}
bool FeatureParams::AddHouseNumber(string houseNumber)
{
ASSERT(!houseNumber.empty(), ("This check should be done by the caller."));
ASSERT_NOT_EQUAL(houseNumber.front(), ' ', ("Trim should be done by the caller."));
// Negative house numbers are not supported.
if (houseNumber.front() == '-' || houseNumber.find(u8"-") == 0)
return false;
// Replace full-width digits, mostly in Japan, by ascii-ones.
strings::NormalizeDigits(houseNumber);
// Remove leading zeroes from house numbers.
// It's important for debug checks of serialized-deserialized feature.
size_t i = 0;
while (i + 1 < houseNumber.size() && houseNumber[i] == '0')
++i;
houseNumber.erase(0, i);
if (any_of(houseNumber.cbegin(), houseNumber.cend(), IsDigit))
{
house.Set(houseNumber);
return true;
}
return false;
}
void FeatureParams::AddStreet(string s)
{
// Replace \n with spaces because we write addresses to txt file.
replace(s.begin(), s.end(), '\n', ' ');
m_addrTags.Add(AddressData::STREET, s);
}
void FeatureParams::AddAddress(string const & s)
{
size_t i = s.find_first_of("\t ");
if (i != string::npos)
{
string const house = s.substr(0, i);
if (feature::IsHouseNumber(house))
{
AddHouseNumber(house);
i = s.find_first_not_of("\t ", i);
}
else
{
i = 0;
}
}
else
{
i = 0;
}
AddStreet(s.substr(i));
}
void FeatureParams::AddPlace(string const & s)
{
m_addrTags.Add(AddressData::PLACE, s);
}
void FeatureParams::AddPostcode(string const & s)
{
m_addrTags.Add(AddressData::POSTCODE, s);
}
bool FeatureParams::FormatFullAddress(m2::PointD const & pt, string & res) const
{
string const street = GetStreet();
if (!street.empty() && !house.IsEmpty())
{
res = street + "|" + house.Get() + "|"
+ strings::to_string_dac(MercatorBounds::YToLat(pt.y), 8) + "|"
+ strings::to_string_dac(MercatorBounds::XToLon(pt.x), 8) + '\n';
return true;
}
return false;
}
string FeatureParams::GetStreet() const
{
return m_addrTags.Get(AddressData::STREET);
}
void FeatureParams::SetGeomType(feature::EGeomType t)
{
switch (t)
{
case GEOM_POINT: m_geomType = HEADER_GEOM_POINT; break;
case GEOM_LINE: m_geomType = HEADER_GEOM_LINE; break;
case GEOM_AREA: m_geomType = HEADER_GEOM_AREA; break;
default: ASSERT(false, ());
}
}
void FeatureParams::SetGeomTypePointEx()
{
ASSERT(m_geomType == HEADER_GEOM_POINT || m_geomType == HEADER_GEOM_POINT_EX, ());
ASSERT(!house.IsEmpty(), ());
m_geomType = HEADER_GEOM_POINT_EX;
}
feature::EGeomType FeatureParams::GetGeomType() const
{
CHECK_NOT_EQUAL(m_geomType, 0xFF, ());
switch (m_geomType)
{
case HEADER_GEOM_LINE: return GEOM_LINE;
case HEADER_GEOM_AREA: return GEOM_AREA;
default: return GEOM_POINT;
}
}
uint8_t FeatureParams::GetTypeMask() const
{
CHECK_NOT_EQUAL(m_geomType, 0xFF, ());
return m_geomType;
}
void FeatureParams::SetRwSubwayType(char const * cityName)
{
Classificator const & c = classif();
static uint32_t const src = c.GetTypeByPath({"railway", "station"});
uint32_t const dest = c.GetTypeByPath({"railway", "station", "subway", cityName});
for (size_t i = 0; i < m_types.size(); ++i)
{
uint32_t t = m_types[i];
ftype::TruncValue(t, 2);
if (t == src)
{
m_types[i] = dest;
break;
}
}
}
void FeatureParams::AddTypes(FeatureParams const & rhs, uint32_t skipType2)
{
if (skipType2 == 0)
{
m_types.insert(m_types.end(), rhs.m_types.begin(), rhs.m_types.end());
}
else
{
for (size_t i = 0; i < rhs.m_types.size(); ++i)
{
uint32_t t = rhs.m_types[i];
ftype::TruncValue(t, 2);
if (t != skipType2)
m_types.push_back(rhs.m_types[i]);
}
}
}
bool FeatureParams::FinishAddingTypes()
{
static uint32_t const boundary = classif().GetTypeByPath({ "boundary", "administrative" });
vector<uint32_t> newTypes;
newTypes.reserve(m_types.size());
for (size_t i = 0; i < m_types.size(); ++i)
{
uint32_t candidate = m_types[i];
// Assume that classificator types are equal if they are equal for 2-arity dimension
// (e.g. "place-city-capital" is equal to "place-city" and we leave the longest one "place-city-capital").
// The only exception is "boundary-administrative" type.
uint32_t type = m_types[i];
ftype::TruncValue(type, 2);
if (type != boundary)
{
// Find all equal types (2-arity).
auto j = base::RemoveIfKeepValid(m_types.begin() + i + 1, m_types.end(), [type] (uint32_t t)
{
ftype::TruncValue(t, 2);
return (type == t);
});
// Choose the best type from equals by arity level.
for (auto k = j; k != m_types.end(); ++k)
if (ftype::GetLevel(*k) > ftype::GetLevel(candidate))
candidate = *k;
// Delete equal types.
m_types.erase(j, m_types.end());
}
newTypes.push_back(candidate);
}
// Remove duplicated types.
sort(newTypes.begin(), newTypes.end());
newTypes.erase(unique(newTypes.begin(), newTypes.end()), newTypes.end());
m_types.swap(newTypes);
if (m_types.size() > kMaxTypesCount)
m_types.resize(kMaxTypesCount);
// Patch fix that removes house number from localities.
if (!house.IsEmpty() && ftypes::IsLocalityChecker::Instance()(m_types))
{
LOG(LINFO, ("Locality with house number", *this));
house.Clear();
}
return !m_types.empty();
}
void FeatureParams::SetType(uint32_t t)
{
m_types.clear();
m_types.push_back(t);
}
bool FeatureParams::PopAnyType(uint32_t & t)
{
CHECK(!m_types.empty(), ());
t = m_types.back();
m_types.pop_back();
return m_types.empty();
}
bool FeatureParams::PopExactType(uint32_t t)
{
m_types.erase(remove(m_types.begin(), m_types.end(), t), m_types.end());
return m_types.empty();
}
bool FeatureParams::IsTypeExist(uint32_t t) const
{
return (find(m_types.begin(), m_types.end(), t) != m_types.end());
}
uint32_t FeatureParams::FindType(uint32_t comp, uint8_t level) const
{
for (uint32_t const type : m_types)
{
uint32_t t = type;
ftype::TruncValue(t, level);
if (t == comp)
return type;
}
return ftype::GetEmptyValue();
}
bool FeatureParams::CheckValid() const
{
CHECK(!m_types.empty() && m_types.size() <= kMaxTypesCount, ());
CHECK_NOT_EQUAL(m_geomType, 0xFF, ());
return FeatureParamsBase::CheckValid();
}
uint8_t FeatureParams::GetHeader() const
{
return CalculateHeader(m_types.size(), GetTypeMask(), *this);
}
uint32_t FeatureParams::GetIndexForType(uint32_t t)
{
return classif().GetIndexForType(t);
}
uint32_t FeatureParams::GetTypeForIndex(uint32_t i)
{
return classif().GetTypeForIndex(i);
}
string DebugPrint(FeatureParams const & p)
{
Classificator const & c = classif();
string res = "Types: ";
for (size_t i = 0; i < p.m_types.size(); ++i)
res = res + c.GetReadableObjectName(p.m_types[i]) + "; ";
return (res + p.DebugString());
}
|