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
|
#include "indexer/editable_map_object.hpp"
#include "indexer/classificator.hpp"
#include "indexer/cuisines.hpp"
#include "indexer/osm_editor.hpp"
#include "indexer/postcodes_matcher.hpp"
#include "base/macros.hpp"
#include "base/string_utils.hpp"
#include "std/cctype.hpp"
#include "std/cmath.hpp"
namespace
{
bool ExtractName(StringUtf8Multilang const & names, int8_t const langCode,
vector<osm::LocalizedName> & result)
{
if (StringUtf8Multilang::kUnsupportedLanguageCode == langCode ||
StringUtf8Multilang::kDefaultCode == langCode)
{
return false;
}
// Exclude languages that are already present.
auto const it =
find_if(result.begin(), result.end(), [langCode](osm::LocalizedName const & localizedName) {
return localizedName.m_code == langCode;
});
if (result.end() != it)
return false;
string name;
names.GetString(langCode, name);
result.emplace_back(langCode, name);
return true;
}
size_t PushMwmLanguages(StringUtf8Multilang const & names, vector<string> const & mwmLanguages, vector<osm::LocalizedName> & result)
{
size_t count = 0;
auto langCode = StringUtf8Multilang::kUnsupportedLanguageCode;
static size_t const kMaxCountMwmLanguages = 2;
for (auto const & language : mwmLanguages)
{
langCode = StringUtf8Multilang::GetLangIndex(language);
if (ExtractName(names, langCode, result))
++count;
if (count >= kMaxCountMwmLanguages)
return count;
}
return count;
}
}
namespace osm
{
// LocalizedName -----------------------------------------------------------------------------------
LocalizedName::LocalizedName(int8_t const code, string const & name)
: m_code(code)
, m_lang(StringUtf8Multilang::GetLangByCode(code))
, m_langName(StringUtf8Multilang::GetLangNameByCode(code))
, m_name(name)
{
}
LocalizedName::LocalizedName(string const & langCode, string const & name)
: m_code(StringUtf8Multilang::GetLangIndex(langCode))
, m_lang(StringUtf8Multilang::GetLangByCode(m_code))
, m_langName(StringUtf8Multilang::GetLangNameByCode(m_code))
, m_name(name)
{
}
// EditableMapObject -------------------------------------------------------------------------------
// static
int8_t const EditableMapObject::kMaximumLevelsEditableByUsers = 25;
bool EditableMapObject::IsNameEditable() const { return m_editableProperties.m_name; }
bool EditableMapObject::IsAddressEditable() const { return m_editableProperties.m_address; }
vector<Props> EditableMapObject::GetEditableProperties() const
{
return MetadataToProps(m_editableProperties.m_metadata);
}
vector<feature::Metadata::EType> const & EditableMapObject::GetEditableFields() const
{
return m_editableProperties.m_metadata;
}
StringUtf8Multilang const & EditableMapObject::GetName() const { return m_name; }
NamesDataSource EditableMapObject::GetNamesDataSource() const
{
return GetNamesDataSource(m_name, GetMwmLanguages(), languages::GetCurrentNorm());
}
// static
NamesDataSource EditableMapObject::GetNamesDataSource(StringUtf8Multilang const & source,
vector<string> const & nativeMwmLanguages,
string const & userLanguage)
{
NamesDataSource result;
auto & names = result.names;
auto & mandatoryCount = result.mandatoryNamesCount;
// Push Mwm languages.
mandatoryCount = PushMwmLanguages(source, nativeMwmLanguages, names);
// Push user's language.
auto const langCode = StringUtf8Multilang::GetLangIndex(userLanguage);
if (ExtractName(source, langCode, names))
++mandatoryCount;
// Push international language.
if (ExtractName(source, StringUtf8Multilang::kInternationalCode, names))
++mandatoryCount;
// Push other languages.
source.ForEach([&names, mandatoryCount](int8_t code, string const & name) -> bool {
// Exclude default name.
if (StringUtf8Multilang::kDefaultCode == code)
return true;
auto const mandatoryNamesEnd = names.begin() + mandatoryCount;
// Exclude languages which already in container (languages with top priority).
auto const it = find_if(
names.begin(), mandatoryNamesEnd,
[code](LocalizedName const & localizedName) { return localizedName.m_code == code; });
if (mandatoryNamesEnd == it)
names.emplace_back(code, name);
return true;
});
return result;
}
vector<LocalizedStreet> const & EditableMapObject::GetNearbyStreets() const { return m_nearbyStreets; }
string const & EditableMapObject::GetHouseNumber() const { return m_houseNumber; }
string EditableMapObject::GetPostcode() const
{
return m_metadata.Get(feature::Metadata::FMD_POSTCODE);
}
string EditableMapObject::GetWikipedia() const
{
return m_metadata.Get(feature::Metadata::FMD_WIKIPEDIA);
}
void EditableMapObject::SetEditableProperties(osm::EditableProperties const & props)
{
m_editableProperties = props;
}
void EditableMapObject::SetName(StringUtf8Multilang const & name) { m_name = name; }
void EditableMapObject::SetName(string name, int8_t langCode)
{
strings::Trim(name);
if (name.empty())
return;
ASSERT_NOT_EQUAL(StringUtf8Multilang::kDefaultCode, langCode,
("You trying to set ", name,
" as default, but direct editing of default name is deprecated."));
if (!Editor::Instance().WasDefaultNameSaved(GetID()) &&
CanUseAsDefaultName(langCode, m_name, GetMwmLanguages()))
{
m_name.AddString(StringUtf8Multilang::kDefaultCode, name);
}
m_name.AddString(langCode, name);
}
// static
bool EditableMapObject::CanUseAsDefaultName(int8_t const langCode, StringUtf8Multilang const & name,
vector<string> const & nativeMwmLanguages)
{
auto index = StringUtf8Multilang::kUnsupportedLanguageCode;
string unused;
// Languages priority: 1. Mwm languages 2. International language.
for (auto const & language : nativeMwmLanguages)
{
index = StringUtf8Multilang::GetLangIndex(language);
if (StringUtf8Multilang::kUnsupportedLanguageCode == index)
return false;
if (langCode == index)
return true;
// A name with a higher priority was added already.
if (name.GetString(index, unused))
return false;
}
if (langCode == StringUtf8Multilang::kInternationalCode)
return true;
return false;
}
void EditableMapObject::SetMercator(m2::PointD const & center) { m_mercator = center; }
void EditableMapObject::SetType(uint32_t featureType)
{
if (m_types.GetGeoType() == feature::EGeomType::GEOM_UNDEFINED)
{
// Support only point type for newly created features.
m_types = feature::TypesHolder(feature::EGeomType::GEOM_POINT);
m_types.Assign(featureType);
}
else
{
// Correctly replace "main" type in cases when feature holds more types.
ASSERT(!m_types.Empty(), ());
feature::TypesHolder copy = m_types;
// TODO(mgsergio): Replace by correct sorting from editor's config.
copy.SortBySpec();
m_types.Remove(*copy.begin());
m_types.operator ()(featureType);
}
}
void EditableMapObject::SetID(FeatureID const & fid) { m_featureID = fid; }
void EditableMapObject::SetStreet(LocalizedStreet const & st) { m_street = st; }
void EditableMapObject::SetNearbyStreets(vector<LocalizedStreet> && streets)
{
m_nearbyStreets = move(streets);
}
void EditableMapObject::SetHouseNumber(string const & houseNumber)
{
m_houseNumber = houseNumber;
}
void EditableMapObject::SetPostcode(string const & postcode)
{
m_metadata.Set(feature::Metadata::FMD_POSTCODE, postcode);
}
void EditableMapObject::SetPhone(string const & phone)
{
m_metadata.Set(feature::Metadata::FMD_PHONE_NUMBER, phone);
}
void EditableMapObject::SetFax(string const & fax)
{
m_metadata.Set(feature::Metadata::FMD_FAX_NUMBER, fax);
}
void EditableMapObject::SetEmail(string const & email)
{
m_metadata.Set(feature::Metadata::FMD_EMAIL, email);
}
void EditableMapObject::SetWebsite(string website)
{
if (!website.empty() &&
!strings::StartsWith(website, "http://") &&
!strings::StartsWith(website, "https://"))
{
website = "http://" + website;
}
m_metadata.Set(feature::Metadata::FMD_WEBSITE, website);
m_metadata.Drop(feature::Metadata::FMD_URL);
}
void EditableMapObject::SetInternet(Internet internet)
{
m_metadata.Set(feature::Metadata::FMD_INTERNET, DebugPrint(internet));
}
void EditableMapObject::SetStars(int stars)
{
if (stars > 0 && stars <= 7)
m_metadata.Set(feature::Metadata::FMD_STARS, strings::to_string(stars));
else
LOG(LWARNING, ("Ignored invalid value to Stars:", stars));
}
void EditableMapObject::SetOperator(string const & op)
{
m_metadata.Set(feature::Metadata::FMD_OPERATOR, op);
}
void EditableMapObject::SetElevation(double ele)
{
// TODO: Reuse existing validadors in generator (osm2meta).
constexpr double kMaxElevationOnTheEarthInMeters = 10000;
constexpr double kMinElevationOnTheEarthInMeters = -15000;
if (ele < kMaxElevationOnTheEarthInMeters && ele > kMinElevationOnTheEarthInMeters)
m_metadata.Set(feature::Metadata::FMD_ELE, strings::to_string_dac(ele, 1));
else
LOG(LWARNING, ("Ignored invalid value to Elevation:", ele));
}
void EditableMapObject::SetWikipedia(string const & wikipedia)
{
m_metadata.Set(feature::Metadata::FMD_WIKIPEDIA, wikipedia);
}
void EditableMapObject::SetFlats(string const & flats)
{
m_metadata.Set(feature::Metadata::FMD_FLATS, flats);
}
void EditableMapObject::SetBuildingLevels(string const & buildingLevels)
{
m_metadata.Set(feature::Metadata::FMD_BUILDING_LEVELS, buildingLevels);
}
LocalizedStreet const & EditableMapObject::GetStreet() const { return m_street; }
void EditableMapObject::SetCuisines(vector<string> const & cuisine)
{
m_metadata.Set(feature::Metadata::FMD_CUISINE, strings::JoinStrings(cuisine, ';'));
}
void EditableMapObject::SetOpeningHours(string const & openingHours)
{
m_metadata.Set(feature::Metadata::FMD_OPEN_HOURS, openingHours);
}
void EditableMapObject::SetPointType() { m_geomType = feature::EGeomType::GEOM_POINT; }
// static
bool EditableMapObject::ValidateBuildingLevels(string const & buildingLevels)
{
if (buildingLevels.empty())
return true;
if (buildingLevels.size() > 18 /* max number of digits in uint_64 */)
return false;
if ('0' == buildingLevels.front())
return false;
uint64_t levels;
return strings::to_uint64(buildingLevels, levels) && levels > 0 && levels <= kMaximumLevelsEditableByUsers;
}
// static
bool EditableMapObject::ValidateHouseNumber(string const & houseNumber)
{
// TODO(mgsergio): Use LooksLikeHouseNumber!
if (houseNumber.empty())
return true;
strings::UniString us = strings::MakeUniString(houseNumber);
// TODO: Improve this basic limit - it was choosen by @Zverik.
auto constexpr kMaxHouseNumberLength = 15;
if (us.size() > kMaxHouseNumberLength)
return false;
// TODO: Should we allow arabic numbers like U+0661 ١ Arabic-Indic Digit One?
strings::NormalizeDigits(us);
for (auto const c : us)
{
// Valid house numbers contain at least one digit.
if (strings::IsASCIIDigit(c))
return true;
}
return false;
}
// static
bool EditableMapObject::ValidateFlats(string const & flats)
{
for (auto it = strings::SimpleTokenizer(flats, ";"); it; ++it)
{
auto token = *it;
strings::Trim(token);
vector<string> range;
for (auto i = strings::SimpleTokenizer(token, "-"); i; ++i)
range.push_back(*i);
if (range.empty() || range.size() > 2)
return false;
for (auto const & rangeBorder : range)
{
if (!all_of(begin(rangeBorder), end(rangeBorder), isalnum))
return false;
}
}
return true;
}
// static
bool EditableMapObject::ValidatePostCode(string const & postCode)
{
if (postCode.empty())
return true;
return search::LooksLikePostcode(postCode, false /* IsPrefix */);
}
// static
bool EditableMapObject::ValidatePhone(string const & phone)
{
if (phone.empty())
return true;
auto curr = begin(phone);
auto const last = end(phone);
auto const kMaxNumberLen = 15;
auto const kMinNumberLen = 5;
if (*curr == '+')
++curr;
auto digitsCount = 0;
for (; curr != last; ++curr)
{
auto const isCharValid = isdigit(*curr) || *curr == '(' ||
*curr == ')' || *curr == ' ' || *curr == '-';
if (!isCharValid)
return false;
if (isdigit(*curr))
++digitsCount;
}
return kMinNumberLen <= digitsCount && digitsCount <= kMaxNumberLen;
}
// static
bool EditableMapObject::ValidateWebsite(string const & site)
{
if (site.empty())
return true;
// Site should contain at least one dot but not at the begining/end.
if ('.' == site.front() || '.' == site.back())
return false;
if (string::npos == site.find("."))
return false;
if (string::npos != site.find(".."))
return false;
return true;
}
// static
bool EditableMapObject::ValidateEmail(string const & email)
{
if (email.empty())
return true;
if (strings::IsASCIIString(email))
return regex_match(email, regex(R"([^@\s]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$)"));
if ('@' == email.front() || '@' == email.back())
return false;
if ('.' == email.back())
return false;
auto const atPos = find(begin(email), end(email), '@');
if (atPos == end(email))
return false;
// There should be only one '@' sign.
if (find(next(atPos), end(email), '@') != end(email))
return false;
// There should be at least one '.' sign after '@'
if (find(next(atPos), end(email), '.') == end(email))
return false;
return true;
}
} // namespace osm
|