diff --git a/map/framework.cpp b/map/framework.cpp index 06a0df8d67..aa192e8ee7 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -246,10 +246,11 @@ bool Framework::PreMigrate(ms::LatLon const & position, { Storage().PrefetchMigrateData(); - storage::CountryInfoReader infoGetter(GetPlatform().GetReader(PACKED_POLYGONS_MIGRATE_FILE), - GetPlatform().GetReader(COUNTRIES_MIGRATE_FILE)); + auto const infoGetter = + storage::CountryInfoReader::CreateCountryInfoReaderTwoComponentMwms(GetPlatform()); - TCountryId currentCountryId = infoGetter.GetRegionCountryId(MercatorBounds::FromLatLon(position)); + TCountryId currentCountryId = + infoGetter->GetRegionCountryId(MercatorBounds::FromLatLon(position)); if (currentCountryId == kInvalidCountryId) return false; @@ -989,23 +990,7 @@ void Framework::InitCountryInfoGetter() { ASSERT(!m_infoGetter.get(), ("InitCountryInfoGetter() must be called only once.")); Platform const & platform = GetPlatform(); - try - { - if (platform::migrate::NeedMigrate()) - { - m_infoGetter.reset(new storage::CountryInfoReader(platform.GetReader(PACKED_POLYGONS_FILE), - platform.GetReader(COUNTRIES_FILE))); - } - else - { - m_infoGetter.reset(new storage::CountryInfoReader(platform.GetReader(PACKED_POLYGONS_MIGRATE_FILE), - platform.GetReader(COUNTRIES_MIGRATE_FILE))); - } - } - catch (RootException const & e) - { - LOG(LCRITICAL, ("Can't load needed resources for storage::CountryInfoGetter:", e.Msg())); - } + m_infoGetter = CountryInfoReader::CreateCountryInfoReader(platform); } void Framework::InitSearchEngine() diff --git a/pedestrian_routing_tests/pedestrian_routing_tests.cpp b/pedestrian_routing_tests/pedestrian_routing_tests.cpp index 4875392cb8..e4e32ac0d2 100644 --- a/pedestrian_routing_tests/pedestrian_routing_tests.cpp +++ b/pedestrian_routing_tests/pedestrian_routing_tests.cpp @@ -85,8 +85,7 @@ private: unique_ptr CreateCountryInfoGetter() { Platform & platform = GetPlatform(); - return unique_ptr(new storage::CountryInfoReader(platform.GetReader(PACKED_POLYGONS_MIGRATE_FILE), - platform.GetReader(COUNTRIES_MIGRATE_FILE))); + return storage::CountryInfoReader::CreateCountryInfoReader(platform); } unique_ptr CreatePedestrianAStarTestRouter(Index & index, storage::CountryInfoGetter & cig) diff --git a/routing/routing_integration_tests/routing_test_tools.cpp b/routing/routing_integration_tests/routing_test_tools.cpp index 2af16c1afc..35b82d5af4 100644 --- a/routing/routing_integration_tests/routing_test_tools.cpp +++ b/routing/routing_integration_tests/routing_test_tools.cpp @@ -66,8 +66,7 @@ namespace integration unique_ptr CreateCountryInfoGetter() { Platform const & platform = GetPlatform(); - return make_unique(platform.GetReader(PACKED_POLYGONS_MIGRATE_FILE), - platform.GetReader(COUNTRIES_MIGRATE_FILE)); + return storage::CountryInfoReader::CreateCountryInfoReader(platform); } shared_ptr CreateOsrmRouter(Index & index, diff --git a/search/search_tests_support/test_search_engine.cpp b/search/search_tests_support/test_search_engine.cpp index f4a9fecc82..a08df838bf 100644 --- a/search/search_tests_support/test_search_engine.cpp +++ b/search/search_tests_support/test_search_engine.cpp @@ -52,8 +52,7 @@ namespace tests_support { TestSearchEngine::TestSearchEngine(string const & locale) : m_platform(GetPlatform()) - , m_infoGetter(new storage::CountryInfoReader(m_platform.GetReader(PACKED_POLYGONS_FILE), - m_platform.GetReader(COUNTRIES_FILE))) + , m_infoGetter(storage::CountryInfoReader::CreateCountryInfoReader(m_platform)) , m_engine(*this, GetDefaultCategories(), *m_infoGetter, locale, make_unique()) { @@ -81,10 +80,8 @@ TestSearchEngine::TestSearchEngine(string const & locale, TestSearchEngine::TestSearchEngine(string const & locale, unique_ptr<::search::SearchQueryFactory> factory) : m_platform(GetPlatform()) - , m_infoGetter(new storage::CountryInfoReader(m_platform.GetReader(PACKED_POLYGONS_FILE), - m_platform.GetReader(COUNTRIES_FILE))) - , m_engine(*this, GetDefaultCategories(), *m_infoGetter, locale, - move(factory)) + , m_infoGetter(storage::CountryInfoReader::CreateCountryInfoReader(m_platform)) + , m_engine(*this, GetDefaultCategories(), *m_infoGetter, locale, move(factory)) { } diff --git a/storage/country_info_getter.cpp b/storage/country_info_getter.cpp index bce6e79b81..a6b356a936 100644 --- a/storage/country_info_getter.cpp +++ b/storage/country_info_getter.cpp @@ -2,6 +2,8 @@ #include "storage/country_info_getter.hpp" #include "storage/country_polygon.hpp" +#include "platform/local_country_file_utils.hpp" + #include "indexer/geometry_serialization.hpp" #include "geometry/latlon.hpp" @@ -10,6 +12,7 @@ #include "coding/read_write_utils.hpp" +#include "base/logging.hpp" #include "base/string_utils.hpp" #include "3party/Alohalytics/src/alohalytics.h" @@ -173,6 +176,66 @@ void CountryInfoGetter::ForEachCountry(string const & prefix, ToDo && toDo) cons } // CountryInfoReader ------------------------------------------------------------------------------- +// static +unique_ptr CountryInfoReader::CreateCountryInfoReader(Platform const & platform) +{ + try + { + CountryInfoReader * result; + if (platform::migrate::NeedMigrate()) + { + result = new CountryInfoReader(platform.GetReader(PACKED_POLYGONS_FILE), + platform.GetReader(COUNTRIES_FILE)); + } + else + { + result = new CountryInfoReader(platform.GetReader(PACKED_POLYGONS_MIGRATE_FILE), + platform.GetReader(COUNTRIES_MIGRATE_FILE)); + } + return unique_ptr(result); + } + catch (RootException const & e) + { + LOG(LCRITICAL, ("Can't load needed resources for storage::CountryInfoGetter:", e.Msg())); + } + return unique_ptr(); +} + +// static +unique_ptr CountryInfoReader::CreateCountryInfoReaderTwoComponentMwms( + Platform const & platform) +{ + try + { + CountryInfoReader * result = new CountryInfoReader(platform.GetReader(PACKED_POLYGONS_FILE), + platform.GetReader(COUNTRIES_FILE)); + return unique_ptr(result); + } + catch (RootException const & e) + { + LOG(LCRITICAL, ("Can't load needed resources for storage::CountryInfoGetter:", e.Msg())); + } + return unique_ptr(); +} + +// static +unique_ptr CountryInfoReader::CreateCountryInfoReaderOneComponentMwms( + Platform const & platform) +{ + try + { + CountryInfoReader * result = + new CountryInfoReader(platform.GetReader(PACKED_POLYGONS_MIGRATE_FILE), + platform.GetReader(COUNTRIES_MIGRATE_FILE)); + return unique_ptr(result); + } + catch (RootException const & e) + { + LOG(LCRITICAL, ("Can't load needed resources for storage::CountryInfoGetter:", e.Msg())); + } + return unique_ptr(); +} + CountryInfoReader::CountryInfoReader(ModelReaderPtr polyR, ModelReaderPtr countryR) : CountryInfoGetter(true), m_reader(polyR), m_cache(3) { diff --git a/storage/country_info_getter.hpp b/storage/country_info_getter.hpp index fe83a0e221..82b40a39fd 100644 --- a/storage/country_info_getter.hpp +++ b/storage/country_info_getter.hpp @@ -2,6 +2,8 @@ #include "storage/country_decl.hpp" +#include "platform/platform.hpp" + #include "geometry/region2d.hpp" #include "coding/file_container.hpp" @@ -113,9 +115,25 @@ protected: class CountryInfoReader : public CountryInfoGetter { public: - CountryInfoReader(ModelReaderPtr polyR, ModelReaderPtr countryR); + // This is the proper way to obtain a CountryInfoReader because + // it accounts for migration and such. + static unique_ptr CreateCountryInfoReader(Platform const & platform); + + // The older version. The polygons are read from a file that was + // used at the time when routing and map data were in different files. + // This is a legacy method and it is extremely unlikely that you need it in your code. + static unique_ptr CreateCountryInfoReaderTwoComponentMwms( + Platform const & platform); + + // The newer version. Use this one after the migration to single-component + // mwm files has been carried out. + // This is a legacy method and it is extremely unlikely that you need it in your code. + static unique_ptr CreateCountryInfoReaderOneComponentMwms( + Platform const & platform); protected: + CountryInfoReader(ModelReaderPtr polyR, ModelReaderPtr countryR); + // CountryInfoGetter overrides: void ClearCachesImpl() const override; bool IsBelongToRegionImpl(size_t id, m2::PointD const & pt) const override; diff --git a/storage/storage_tests/helpers.cpp b/storage/storage_tests/helpers.cpp index 6d935454d8..9efdcdd157 100644 --- a/storage/storage_tests/helpers.cpp +++ b/storage/storage_tests/helpers.cpp @@ -10,16 +10,12 @@ namespace storage { unique_ptr CreateCountryInfoGetter() { - Platform & platform = GetPlatform(); - return make_unique(platform.GetReader(PACKED_POLYGONS_FILE), - platform.GetReader(COUNTRIES_FILE)); + return CountryInfoReader::CreateCountryInfoReaderTwoComponentMwms(GetPlatform()); } unique_ptr CreateCountryInfoGetterMigrate() { - Platform & platform = GetPlatform(); - return make_unique(platform.GetReader(PACKED_POLYGONS_MIGRATE_FILE), - platform.GetReader(COUNTRIES_MIGRATE_FILE)); + return CountryInfoReader::CreateCountryInfoReaderOneComponentMwms(GetPlatform()); } bool AlmostEqualRectsAbs(const m2::RectD & r1, const m2::RectD & r2)