Skip to content

Commit 794503e

Browse files
committed
fix postcode search when using primitive parser
1 parent 4cdbfbe commit 794503e

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

importer/src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,11 @@ class AdminVisitor: public osmscout::AdminRegionVisitor
582582
const auto &v = additional_postal_codes.at(rid);
583583
for (auto c: v)
584584
{
585-
sqlite3pp::command cmd(m_db, "INSERT INTO object_primary_tmp (id, postal_code, longitude, latitude) VALUES (?, ?, ?, ?)");
585+
sqlite3pp::command cmd(m_db, "INSERT INTO object_primary_tmp (id, postal_code, parent, longitude, latitude) VALUES (?, ?, ?, ?, ?)");
586586
id = IDs.next();
587587
cmd.binder() << id
588588
<< c.code
589+
<< regionID
589590
<< c.longitude
590591
<< c.latitude;
591592
if (cmd.execute() != SQLITE_OK)

src/geocoder.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ bool Geocoder::search(const std::vector<Postal::ParseResult> &parsed_query, std:
210210
search(r, postal_code, result);
211211
#ifdef GEONLP_PRINT_DEBUG_QUERIES
212212
else
213-
std::cout << "Skipping hierarchy since search result already has more levels than provided\n";
213+
std::cout << "Skipping hierarchy since search result already has more levels (" << m_levels_resolved << ") than provided\n";
214214
#endif
215215
#ifdef GEONLP_PRINT_DEBUG_QUERIES
216216
std::cout << "\n";
@@ -371,13 +371,14 @@ bool Geocoder::search(const Postal::Hierarchy &parsed, const std::string &postal
371371
!search(parsed, postal_is_ok ? "" : postal_code, result, level+1, id+1, last_subobject) )
372372
{
373373
size_t levels_resolved = level+1;
374+
bool newlevel = false;
374375
if ( m_levels_resolved < levels_resolved )
375376
{
376377
result.clear();
377-
m_levels_resolved = levels_resolved;
378+
newlevel = true;
378379
}
379-
380-
if (m_levels_resolved == levels_resolved && (m_max_results == 0 || result.size() < m_max_inter_results))
380+
381+
if ( (m_levels_resolved == levels_resolved || newlevel) && (m_max_results == 0 || result.size() < m_max_inter_results))
381382
{
382383
bool have_already = false;
383384
for (const auto &r: result)
@@ -395,6 +396,7 @@ bool Geocoder::search(const Postal::Hierarchy &parsed, const std::string &postal
395396
r.id = id;
396397
r.levels_resolved = levels_resolved;
397398
result.push_back(r);
399+
m_levels_resolved = levels_resolved;
398400
}
399401
else if (id < last_subobject)
400402
{
@@ -415,6 +417,7 @@ bool Geocoder::search(const Postal::Hierarchy &parsed, const std::string &postal
415417
v.getter() >> r.id;
416418
r.levels_resolved = levels_resolved;
417419
result.push_back(r);
420+
m_levels_resolved = levels_resolved;
418421
if (m_max_results > 0 && result.size() >= m_max_inter_results)
419422
break;
420423
}

src/postal.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ using namespace GeoNLP;
2727
#define ADDRESS_PARSER_LABEL_COUNTRY_REGION "country_region"
2828
#define ADDRESS_PARSER_LABEL_COUNTRY "country"
2929

30-
#define PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE "post:"
30+
#define PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_INPUT "post:"
31+
#define PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_KEY "h-postcode"
3132

3233
//////////////////////////////////////////////////////////////////////
3334
/// Helper string functions
@@ -311,29 +312,28 @@ bool Postal::parse(const std::string &input, std::vector<Postal::ParseResult> &r
311312
{
312313
std::vector<std::string> hier;
313314
split_tokens(input, ',', hier);
315+
if (hier.empty()) hier.push_back(input);
314316

315-
if (!hier.empty())
317+
ParseResult prim;
318+
int shift = 0;
319+
size_t np = strlen(PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_INPUT);
320+
for (size_t j = 0; j < hier.size(); j++)
316321
{
317-
ParseResult prim;
318-
int shift = 0;
319-
size_t np = strlen(PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE);
320-
for (size_t j = 0; j < hier.size(); j++)
322+
std::string v = hier[hier.size()-j-1];
323+
std::string key = primitive_key(j-shift);
324+
v = trim(v);
325+
if (v.compare(0, np, PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_INPUT) == 0)
321326
{
322-
std::string v = hier[hier.size()-j-1];
323-
std::string key = primitive_key(j-shift);
324-
if (v.compare(0, np, PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE) == 0)
325-
{
326-
v = v.substr(np+1);
327-
v = trim(v);
328-
key = PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE;
329-
shift += 1;
330-
}
331-
std::vector<std::string> pc; pc.push_back(v);
332-
prim[key] = pc;
327+
v = v.substr(np);
328+
v = trim(v);
329+
key = PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_KEY;
330+
shift += 1;
333331
}
334-
335-
expand(prim, result);
332+
std::vector<std::string> pc; pc.push_back(v);
333+
prim[key] = pc;
336334
}
335+
336+
expand(prim, result);
337337
}
338338

339339
if (m_initialize_for_every_call) drop();
@@ -370,7 +370,7 @@ void Postal::expand(const Postal::ParseResult &input, std::vector<Postal::ParseR
370370
{
371371
std::vector< std::string > norm;
372372
// no need to keep postal code in normalized and expanded
373-
if (i.first == ADDRESS_PARSER_LABEL_POSTAL_CODE)
373+
if (i.first == ADDRESS_PARSER_LABEL_POSTAL_CODE || i.first == PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_KEY)
374374
norm.push_back(tonorm);
375375
else
376376
{
@@ -492,6 +492,11 @@ void Postal::result2hierarchy(const std::vector<ParseResult> &p, std::vector<Hie
492492
}
493493
}
494494

495+
// overwrite with the primitive parser postal code if needed
496+
ParseResult::const_iterator it = r.find(PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_KEY);
497+
if (it != r.end() && it->second.size())
498+
postal_code = normalize_postalcode(it->second[0]);
499+
495500
h.push_back(h_result);
496501
}
497502
}

0 commit comments

Comments
 (0)