From 34791b0168be4da436517deeae385c2928d954fb Mon Sep 17 00:00:00 2001 From: Sergey Yershov Date: Wed, 20 Apr 2016 12:58:42 +0300 Subject: [PATCH 1/4] Refactor countryId naming --- generator/borders_loader.cpp | 4 +-- search/v2/geocoder.hpp | 2 +- storage/country_decl.hpp | 6 ++--- storage/country_info_getter.cpp | 48 ++++++++++++++++++--------------- storage/country_info_getter.hpp | 26 +++++++++--------- storage/country_polygon.hpp | 4 +-- 6 files changed, 49 insertions(+), 41 deletions(-) diff --git a/generator/borders_loader.cpp b/generator/borders_loader.cpp index 3b343da80a..bb44fd22f4 100644 --- a/generator/borders_loader.cpp +++ b/generator/borders_loader.cpp @@ -176,8 +176,8 @@ void UnpackBorders(string const & baseDir, string const & targetDir) for (size_t id = 0; id < countries.size(); id++) { - ofstream poly(my::JoinFoldersToPath(targetDir, countries[id].m_name + ".poly")); - poly << countries[id].m_name << endl; + ofstream poly(my::JoinFoldersToPath(targetDir, countries[id].m_countryId + ".poly")); + poly << countries[id].m_countryId << endl; src = reader.GetReader(strings::to_string(id)); uint32_t const count = ReadVarUint(src); for (size_t i = 0; i < count; ++i) diff --git a/search/v2/geocoder.hpp b/search/v2/geocoder.hpp index 3cdefc2a22..c273aad162 100644 --- a/search/v2/geocoder.hpp +++ b/search/v2/geocoder.hpp @@ -118,7 +118,7 @@ public: { Region(Locality const & l, RegionType type) : Locality(l), m_center(0, 0), m_type(type) {} - storage::CountryInfoGetter::IdSet m_ids; + storage::CountryInfoGetter::TRegionIdSet m_ids; string m_enName; m2::PointD m_center; RegionType m_type; diff --git a/storage/country_decl.hpp b/storage/country_decl.hpp index f870df5547..c3bb3972d9 100644 --- a/storage/country_decl.hpp +++ b/storage/country_decl.hpp @@ -11,12 +11,12 @@ namespace storage struct CountryDef { /// File name without extension (equal to english name - used in search for region). - TCountryId m_name; + TCountryId m_countryId; m2::RectD m_rect; CountryDef() {} - CountryDef(string const & name, m2::RectD const & rect) - : m_name(name), m_rect(rect) + CountryDef(TCountryId const & countryId, m2::RectD const & rect) + : m_countryId(countryId), m_rect(rect) { } }; diff --git a/storage/country_info_getter.cpp b/storage/country_info_getter.cpp index 95100a1fdf..3994e618e5 100644 --- a/storage/country_info_getter.cpp +++ b/storage/country_info_getter.cpp @@ -39,9 +39,9 @@ public: void operator()(CountryDef const & c) { - if (c.m_name == "USA_Alaska") + if (c.m_countryId == "USA_Alaska") m_rects[1] = c.m_rect; - else if (c.m_name == "USA_Hawaii") + else if (c.m_countryId == "USA_Hawaii") m_rects[2] = c.m_rect; else m_rects[0].Add(c.m_rect); @@ -55,8 +55,8 @@ private: // CountryInfoGetter ------------------------------------------------------------------------------- TCountryId CountryInfoGetter::GetRegionCountryId(m2::PointD const & pt) const { - IdType const id = FindFirstCountry(pt); - return id != kInvalidId ? m_countries[id].m_name : kInvalidCountryId; + TRegionId const id = FindFirstCountry(pt); + return id != kInvalidId ? m_countries[id].m_countryId : kInvalidCountryId; } void CountryInfoGetter::GetRegionsCountryId(m2::PointD const & pt, TCountriesVec & closestCoutryIds) @@ -70,26 +70,26 @@ void CountryInfoGetter::GetRegionsCountryId(m2::PointD const & pt, TCountriesVec for (size_t id = 0; id < m_countries.size(); ++id) { if (m_countries[id].m_rect.IsIntersect(lookupRect) && IsCloseEnough(id, pt, kLookupRadiusM)) - closestCoutryIds.emplace_back(m_countries[id].m_name); + closestCoutryIds.emplace_back(m_countries[id].m_countryId); } } void CountryInfoGetter::GetRegionInfo(m2::PointD const & pt, CountryInfo & info) const { - IdType const id = FindFirstCountry(pt); + TRegionId const id = FindFirstCountry(pt); if (id != kInvalidId) - GetRegionInfo(m_countries[id].m_name, info); + GetRegionInfo(m_countries[id].m_countryId, info); } -void CountryInfoGetter::GetRegionInfo(string const & id, CountryInfo & info) const +void CountryInfoGetter::GetRegionInfo(TCountryId const & countryId, CountryInfo & info) const { - auto const it = m_id2info.find(id); + auto const it = m_id2info.find(countryId); if (it == m_id2info.end()) return; info = it->second; if (info.m_name.empty()) - info.m_name = id; + info.m_name = countryId; CountryInfo::FileName2FullName(info.m_name); } @@ -117,7 +117,7 @@ m2::RectD CountryInfoGetter::GetLimitRectForLeaf(TCountryId const & leafCountryI return m_countries[it->second].m_rect; } -void CountryInfoGetter::GetMatchedRegions(string const & affiliation, IdSet & regions) const +void CountryInfoGetter::GetMatchedRegions(string const & affiliation, TRegionIdSet & regions) const { CHECK(m_affiliations, ()); auto it = m_affiliations->find(affiliation); @@ -126,12 +126,12 @@ void CountryInfoGetter::GetMatchedRegions(string const & affiliation, IdSet & re for (size_t i = 0; i < m_countries.size(); ++i) { - if (binary_search(it->second.begin(), it->second.end(), m_countries[i].m_name)) + if (binary_search(it->second.begin(), it->second.end(), m_countries[i].m_countryId)) regions.push_back(i); } } -bool CountryInfoGetter::IsBelongToRegions(m2::PointD const & pt, IdSet const & regions) const +bool CountryInfoGetter::IsBelongToRegions(m2::PointD const & pt, TRegionIdSet const & regions) const { for (auto const & id : regions) { @@ -141,22 +141,28 @@ bool CountryInfoGetter::IsBelongToRegions(m2::PointD const & pt, IdSet const & r return false; } -bool CountryInfoGetter::IsBelongToRegions(string const & fileName, IdSet const & regions) const +bool CountryInfoGetter::IsBelongToRegions(TCountryId const & countryId, TRegionIdSet const & regions) const { for (auto const & id : regions) { - if (m_countries[id].m_name == fileName) + if (m_countries[id].m_countryId == countryId) return true; } return false; } +void CountryInfoGetter::RegionIdsToCountryIds(TRegionIdSet const & regions, TCountriesVec & countries) const +{ + for (auto const & id : regions) + countries.push_back(m_countries[id].m_countryId); +} + void CountryInfoGetter::InitAffiliationsInfo(TMappingAffiliations const * affiliations) { m_affiliations = affiliations; } -CountryInfoGetter::IdType CountryInfoGetter::FindFirstCountry(m2::PointD const & pt) const +CountryInfoGetter::TRegionId CountryInfoGetter::FindFirstCountry(m2::PointD const & pt) const { for (size_t id = 0; id < m_countries.size(); ++id) { @@ -176,7 +182,7 @@ void CountryInfoGetter::ForEachCountry(string const & prefix, ToDo && toDo) cons { for (auto const & country : m_countries) { - if (strings::StartsWith(country.m_name, prefix.c_str())) + if (strings::StartsWith(country.m_countryId, prefix.c_str())) toDo(country); } } @@ -234,7 +240,7 @@ CountryInfoReader::CountryInfoReader(ModelReaderPtr polyR, ModelReaderPtr countr size_t const countrySz = m_countries.size(); m_countryIndex.reserve(countrySz); for (size_t i = 0; i < countrySz; ++i) - m_countryIndex[m_countries[i].m_name] = i; + m_countryIndex[m_countries[i].m_countryId] = i; string buffer; countryR.ReadAsString(buffer); @@ -318,16 +324,16 @@ CountryInfoGetterForTesting::CountryInfoGetterForTesting(vector cons void CountryInfoGetterForTesting::AddCountry(CountryDef const & country) { m_countries.push_back(country); - string const & name = country.m_name; + string const & name = country.m_countryId; m_id2info[name].m_name = name; } void CountryInfoGetterForTesting::GetMatchedRegions(string const & affiliation, - IdSet & regions) const + TRegionIdSet & regions) const { for (size_t i = 0; i < m_countries.size(); ++i) { - if (m_countries[i].m_name == affiliation) + if (m_countries[i].m_countryId == affiliation) regions.push_back(i); } } diff --git a/storage/country_info_getter.hpp b/storage/country_info_getter.hpp index 3bb974d9d1..e1108aa1c1 100644 --- a/storage/country_info_getter.hpp +++ b/storage/country_info_getter.hpp @@ -25,8 +25,8 @@ class CountryInfoGetter { public: // Identifier of a region (index in m_countries array). - using IdType = size_t; - using IdSet = vector; + using TRegionId = size_t; + using TRegionIdSet = vector; CountryInfoGetter(bool isSingleMwm) : m_isSingleMwm(isSingleMwm) {} virtual ~CountryInfoGetter() = default; @@ -45,8 +45,8 @@ public: // Returns info for a region |pt| belongs to. void GetRegionInfo(m2::PointD const & pt, CountryInfo & info) const; - // Returns info for a country by file name without an extension. - void GetRegionInfo(string const & id, CountryInfo & info) const; + // Returns info for a country by id. + void GetRegionInfo(TCountryId const & countryId, CountryInfo & info) const; // Return limit rects of USA: // 0 - continental part @@ -63,15 +63,17 @@ public: m2::RectD GetLimitRectForLeaf(TCountryId const & leafCountryId) const; // Returns identifiers for all regions matching to correspondent |affiliation|. - virtual void GetMatchedRegions(string const & affiliation, IdSet & regions) const; + virtual void GetMatchedRegions(string const & affiliation, TRegionIdSet & regions) const; // Returns true when |pt| belongs to at least one of the specified // |regions|. - bool IsBelongToRegions(m2::PointD const & pt, IdSet const & regions) const; + bool IsBelongToRegions(m2::PointD const & pt, TRegionIdSet const & regions) const; - // Returns true if there're at least one region with name equals to - // |fileName|. - bool IsBelongToRegions(string const & fileName, IdSet const & regions) const; + // Returns true if there're at least one region with id equals to + // |countryId|. + bool IsBelongToRegions(TCountryId const & countryId, TRegionIdSet const & regions) const; + + void RegionIdsToCountryIds(TRegionIdSet const & regions, TCountriesVec & countries) const; // Clears regions cache. inline void ClearCaches() const { ClearCachesImpl(); } @@ -82,7 +84,7 @@ protected: CountryInfoGetter() = default; // Returns identifier of a first country containing |pt|. - IdType FindFirstCountry(m2::PointD const & pt) const; + TRegionId FindFirstCountry(m2::PointD const & pt) const; // Invokes |toDo| on each country whose name starts with |prefix|. template @@ -102,7 +104,7 @@ protected: // List of all known countries. vector m_countries; // Maps all leaf country id (file names) to their indices in m_countries. - unordered_map m_countryIndex; + unordered_map m_countryIndex; TMappingAffiliations const * m_affiliations = nullptr; @@ -164,7 +166,7 @@ public: void AddCountry(CountryDef const & country); // CountryInfoGetter overrides: - void GetMatchedRegions(string const & affiliation, IdSet & regions) const override; + void GetMatchedRegions(string const & affiliation, TRegionIdSet & regions) const override; protected: // CountryInfoGetter overrides: diff --git a/storage/country_polygon.hpp b/storage/country_polygon.hpp index 630fdd7c8f..425e739907 100644 --- a/storage/country_polygon.hpp +++ b/storage/country_polygon.hpp @@ -12,7 +12,7 @@ namespace storage { template void Read(TSource & src, CountryDef & p) { - rw::Read(src, p.m_name); + rw::Read(src, p.m_countryId); pair r; r.first = ReadVarInt(src); @@ -22,7 +22,7 @@ namespace storage template void Write(TSink & sink, CountryDef const & p) { - rw::Write(sink, p.m_name); + rw::Write(sink, p.m_countryId); pair const r = RectToInt64(p.m_rect, serial::CodingParams().GetCoordBits()); WriteVarInt(sink, r.first); From c3d68e8e3cf1f07ab86337919721d79bcef0a15f Mon Sep 17 00:00:00 2001 From: Sergey Yershov Date: Wed, 20 Apr 2016 13:03:47 +0300 Subject: [PATCH 2/4] [downloader] Implement retrieving countryId for topmost group node --- storage/storage.cpp | 25 +++++++++++++++++++++++++ storage/storage.hpp | 3 +++ 2 files changed, 28 insertions(+) diff --git a/storage/storage.cpp b/storage/storage.cpp index b1ee070bef..a5cddd1bab 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -1593,5 +1593,30 @@ void Storage::GetGroupNodePathToRoot(TCountryId const & groupNode, TCountriesVec }); path.push_back(m_countries.GetRoot().Value().Name()); } + +void Storage::GetTopmostNodesFor(TCountryId const & countryId, TCountriesVec & nodes) const +{ + nodes.clear(); + vector treeNodes; + m_countries.Find(countryId, treeNodes); + if (treeNodes.empty()) + { + LOG(LWARNING, ("TCountryId =", countryId, "not found in m_countries.")); + return; + } + + nodes.clear(); + nodes.resize(treeNodes.size()); + for (auto & node : nodes) + { + node = countryId; + ForEachAncestorExceptForTheRoot(treeNodes, + [&node](TCountryId const & id, TCountryTreeNode const &) + { + node = id; + }); + } +} + } // namespace storage diff --git a/storage/storage.hpp b/storage/storage.hpp index 428f9f566b..09081906c0 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -342,6 +342,9 @@ public: /// \param path is resulting array of TCountryId. void GetGroupNodePathToRoot(TCountryId const & groupNode, TCountriesVec & path) const; + /// \brief TODO + void GetTopmostNodesFor(TCountryId const & countryId, TCountriesVec & nodes) const; + /// \brief Returns current version for mwms which are used by storage. inline int64_t GetCurrentDataVersion() const { return m_currentVersion; } From 911148145667bc907380ba3482b0c2036708866c Mon Sep 17 00:00:00 2001 From: Sergey Yershov Date: Wed, 20 Apr 2016 13:04:58 +0300 Subject: [PATCH 3/4] [downloader] Add countryId in place page --- map/framework.cpp | 12 ++++++++++++ map/place_page_info.hpp | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/map/framework.cpp b/map/framework.cpp index c5a5cdeadb..d9d960d472 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -679,6 +679,18 @@ void Framework::FillFeatureInfo(FeatureID const & fid, place_page::Info & info) FeatureType ft; guard.GetFeatureByIndex(fid.m_index, ft); FillInfoFromFeatureType(ft, info); + + // Fill countryId for place page info + info.m_countryId = m_infoGetter->GetRegionCountryId(info.GetMercator()); + + uint32_t placeCountryType = classif().GetTypeByPath({"place", "country"}); + if (info.GetTypes().Has(placeCountryType)) + { + TCountriesVec countries; + Storage().GetTopmostNodesFor(info.m_countryId, countries); + if (countries.size() == 1) + info.m_countryId = countries.front(); + } } void Framework::FillPointInfo(m2::PointD const & mercator, string const & customTitle, place_page::Info & info) const diff --git a/map/place_page_info.hpp b/map/place_page_info.hpp index 19c2b6b7cc..313e7c3721 100644 --- a/map/place_page_info.hpp +++ b/map/place_page_info.hpp @@ -62,6 +62,10 @@ public: /// Formatted feature address. string m_address; + /// Which country this MapObject is in. + /// For a country point it will be set to topmost node for country. + storage::TCountryId m_countryId = storage::kInvalidCountryId; + bool m_isMyPosition = false; bool m_isEditable = false; From b34673c408781c551e67e84d54f7bd4fd2e8ae5e Mon Sep 17 00:00:00 2001 From: Sergey Yershov Date: Wed, 20 Apr 2016 13:07:07 +0300 Subject: [PATCH 4/4] [desktop] Add showing countryId in place page --- qt/place_page_dialog.cpp | 6 ++++++ xcode/MapsMe/MapsMe.xcodeproj/project.pbxproj | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/qt/place_page_dialog.cpp b/qt/place_page_dialog.cpp index fdd886f5ea..4162fb4a54 100644 --- a/qt/place_page_dialog.cpp +++ b/qt/place_page_dialog.cpp @@ -34,6 +34,12 @@ PlacePageDialog::PlacePageDialog(QWidget * parent, place_page::Info const & info label->setTextInteractionFlags(Qt::TextSelectableByMouse); grid->addWidget(label, row++, 1); } + { + grid->addWidget(new QLabel("CountryId"), row, 0); + QLabel * label = new QLabel(QString::fromStdString(info.m_countryId)); + label->setTextInteractionFlags(Qt::TextSelectableByMouse); + grid->addWidget(label, row++, 1); + } // Title/Name/Custom Name. if (!info.GetTitle().empty()) { diff --git a/xcode/MapsMe/MapsMe.xcodeproj/project.pbxproj b/xcode/MapsMe/MapsMe.xcodeproj/project.pbxproj index 1706e80a6a..e2c82581c3 100644 --- a/xcode/MapsMe/MapsMe.xcodeproj/project.pbxproj +++ b/xcode/MapsMe/MapsMe.xcodeproj/project.pbxproj @@ -119,6 +119,7 @@ 67A461A71C2172C400B18739 /* 07_roboto_medium.ttf in CopyFiles */ = {isa = PBXBuildFile; fileRef = 67A461A61C2172C400B18739 /* 07_roboto_medium.ttf */; }; 67E8DC931BBC1D3F0053C5BA /* libagg.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 67E8DC911BBC1D3F0053C5BA /* libagg.a */; }; 67E8DC941BBC1D3F0053C5BA /* liblodepng.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 67E8DC921BBC1D3F0053C5BA /* liblodepng.a */; }; + 67F4D3F51CC5400900C5D9BB /* cuisine-strings in Resources */ = {isa = PBXBuildFile; fileRef = 67F4D3F41CC5400900C5D9BB /* cuisine-strings */; }; 9D4AD1DD1B7388CD00EBDD27 /* 02_droidsans-fallback.ttf in CopyFiles */ = {isa = PBXBuildFile; fileRef = 9DF792621B73884F00AF2D19 /* 02_droidsans-fallback.ttf */; }; 9DA46A201C47E9E200EF52BA /* drules_proto_legacy.bin in Resources */ = {isa = PBXBuildFile; fileRef = 9DA46A1A1C47E9E200EF52BA /* drules_proto_legacy.bin */; }; 9DA46A211C47E9E200EF52BA /* resources-hdpi_legacy in Resources */ = {isa = PBXBuildFile; fileRef = 9DA46A1B1C47E9E200EF52BA /* resources-hdpi_legacy */; }; @@ -307,6 +308,7 @@ 67A461A61C2172C400B18739 /* 07_roboto_medium.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = 07_roboto_medium.ttf; sourceTree = ""; }; 67E8DC911BBC1D3F0053C5BA /* libagg.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libagg.a; path = "../../../omim-xcode-build/Debug/libagg.a"; sourceTree = ""; }; 67E8DC921BBC1D3F0053C5BA /* liblodepng.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = liblodepng.a; path = "../../../omim-xcode-build/Debug/liblodepng.a"; sourceTree = ""; }; + 67F4D3F41CC5400900C5D9BB /* cuisine-strings */ = {isa = PBXFileReference; lastKnownFileType = folder; path = "cuisine-strings"; sourceTree = ""; }; 9DA46A1A1C47E9E200EF52BA /* drules_proto_legacy.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; path = drules_proto_legacy.bin; sourceTree = ""; }; 9DA46A1B1C47E9E200EF52BA /* resources-hdpi_legacy */ = {isa = PBXFileReference; lastKnownFileType = folder; path = "resources-hdpi_legacy"; sourceTree = ""; }; 9DA46A1C1C47E9E200EF52BA /* resources-ldpi_legacy */ = {isa = PBXFileReference; lastKnownFileType = folder; path = "resources-ldpi_legacy"; sourceTree = ""; }; @@ -422,6 +424,7 @@ 6729A5C91A693045007D5872 /* Resources */ = { isa = PBXGroup; children = ( + 67F4D3F41CC5400900C5D9BB /* cuisine-strings */, 45B5B59D1CA422EE00D93E36 /* resources-6plus_dark */, 45B5B59E1CA422EE00D93E36 /* resources-hdpi_dark */, 45B5B59F1CA422EE00D93E36 /* resources-ldpi_dark */, @@ -632,6 +635,7 @@ 671182DC1C7F0D8C00CB8177 /* packed_polygons_obsolete.bin in Resources */, 45B5B5981CA422C800D93E36 /* resources-6plus_clear in Resources */, 671182DB1C7F0D8C00CB8177 /* countries_obsolete.txt in Resources */, + 67F4D3F51CC5400900C5D9BB /* cuisine-strings in Resources */, 9DA46A221C47E9E200EF52BA /* resources-ldpi_legacy in Resources */, ); runOnlyForDeploymentPostprocessing = 0;