diff options
author | Maksim Andrianov <maksimandrianov1@gmail.com> | 2018-10-02 17:53:16 +0300 |
---|---|---|
committer | mpimenov <mpimenov@users.noreply.github.com> | 2018-10-05 17:36:29 +0300 |
commit | 95c92d6dd7b8bb4ba4075c0aa014f141242a50a5 (patch) | |
tree | 545d70977aba8fb80de8b06f1bd828637dd13fef /generator/regions | |
parent | 707289401e0188c438781eb2f3fb97c67b7b8fa1 (diff) |
[generator] Added geo objects translator.
Diffstat (limited to 'generator/regions')
-rw-r--r-- | generator/regions/city.hpp | 2 | ||||
-rw-r--r-- | generator/regions/collector_region_info.cpp (renamed from generator/regions/region_info_collector.cpp) | 130 | ||||
-rw-r--r-- | generator/regions/collector_region_info.hpp (renamed from generator/regions/region_info_collector.hpp) | 153 | ||||
-rw-r--r-- | generator/regions/region.cpp | 2 | ||||
-rw-r--r-- | generator/regions/region_base.hpp | 2 | ||||
-rw-r--r-- | generator/regions/regions.cpp | 10 |
6 files changed, 159 insertions, 140 deletions
diff --git a/generator/regions/city.hpp b/generator/regions/city.hpp index 545252af93..54c9f5bf9b 100644 --- a/generator/regions/city.hpp +++ b/generator/regions/city.hpp @@ -1,8 +1,8 @@ #pragma once #include "generator/feature_builder.hpp" +#include "generator/regions/collector_region_info.hpp" #include "generator/regions/region_base.hpp" -#include "generator/regions/region_info_collector.hpp" namespace generator { diff --git a/generator/regions/region_info_collector.cpp b/generator/regions/collector_region_info.cpp index 1cfa312be1..a011ed744a 100644 --- a/generator/regions/region_info_collector.cpp +++ b/generator/regions/collector_region_info.cpp @@ -1,4 +1,4 @@ -#include "generator/regions/region_info_collector.hpp" +#include "generator/regions/collector_region_info.hpp" #include "generator/feature_builder.hpp" #include "generator/osm_element.hpp" @@ -21,7 +21,7 @@ namespace generator { namespace regions { -std::string const RegionInfoCollector::kDefaultExt = ".regions.bin"; +std::string const CollectorRegionInfo::kDefaultExt = ".regions.bin"; PlaceType EncodePlaceType(std::string const & place) { @@ -40,54 +40,9 @@ PlaceType EncodePlaceType(std::string const & place) return it == m.end() ? PlaceType::Unknown : it->second; } -void RegionInfoCollector::IsoCode::SetAlpha2(std::string const & alpha2) -{ - CHECK_LESS_OR_EQUAL(alpha2.size() + 1, ARRAY_SIZE(m_alpha2), ()); - std::strcpy(m_alpha2, alpha2.data()); -} - -void RegionInfoCollector::IsoCode::SetAlpha3(std::string const & alpha3) -{ - CHECK_LESS_OR_EQUAL(alpha3.size() + 1, ARRAY_SIZE(m_alpha3), ()); - std::strcpy(m_alpha3, alpha3.data()); -} - -void RegionInfoCollector::IsoCode::SetNumeric(std::string const & numeric) -{ - CHECK_LESS_OR_EQUAL(numeric.size() + 1, ARRAY_SIZE(m_numeric), ()); - std::strcpy(m_numeric, numeric.data()); -} - -RegionInfoCollector::RegionInfoCollector(std::string const & filename) -{ - ParseFile(filename); -} - -RegionInfoCollector::RegionInfoCollector(Platform::FilesList const & filenames) -{ - for (auto const & filename : filenames) - ParseFile(filename); -} +CollectorRegionInfo::CollectorRegionInfo(std::string const & filename) : m_filename(filename) {} -void RegionInfoCollector::ParseFile(std::string const & filename) -{ - try - { - FileReader reader(filename); - ReaderSource<FileReader> src(reader); - uint8_t version; - ReadPrimitiveFromSource(src, version); - CHECK_EQUAL(version, kVersion, ("Versions do not match.")); - ReadMap(src, m_mapRegionData); - ReadMap(src, m_mapIsoCode); - } - catch (FileReader::Exception const & e) - { - LOG(LCRITICAL, ("Failed to parse regions info:", e.Msg())); - } -} - -void RegionInfoCollector::Add(base::GeoObjectId const & osmId, OsmElement const & el) +void CollectorRegionInfo::Collect(base::GeoObjectId const & osmId, OsmElement const & el) { RegionData regionData; FillRegionData(osmId, el, regionData); @@ -101,11 +56,11 @@ void RegionInfoCollector::Add(base::GeoObjectId const & osmId, OsmElement const } } -void RegionInfoCollector::Save(std::string const & filename) +void CollectorRegionInfo::Save() { try { - FileWriter writer(filename); + FileWriter writer(m_filename); WriteToSink(writer, kVersion); WriteMap(writer, m_mapRegionData); WriteMap(writer, m_mapIsoCode); @@ -116,12 +71,7 @@ void RegionInfoCollector::Save(std::string const & filename) } } -RegionDataProxy RegionInfoCollector::Get(base::GeoObjectId const & osmId) -{ - return RegionDataProxy(*this, osmId); -} - -void RegionInfoCollector::FillRegionData(base::GeoObjectId const & osmId, OsmElement const & el, +void CollectorRegionInfo::FillRegionData(base::GeoObjectId const & osmId, OsmElement const & el, RegionData & rd) { rd.m_osmId = osmId; @@ -152,7 +102,7 @@ void RegionInfoCollector::FillRegionData(base::GeoObjectId const & osmId, OsmEle } } -void RegionInfoCollector::FillIsoCode(base::GeoObjectId const & osmId, OsmElement const & el, +void CollectorRegionInfo::FillIsoCode(base::GeoObjectId const & osmId, OsmElement const & el, IsoCode & rd) { rd.m_osmId = osmId; @@ -161,36 +111,88 @@ void RegionInfoCollector::FillIsoCode(base::GeoObjectId const & osmId, OsmElemen rd.SetNumeric(el.GetTag("ISO3166-1:numeric")); } -RegionDataProxy::RegionDataProxy(RegionInfoCollector & regionInfoCollector, +void IsoCode::SetAlpha2(std::string const & alpha2) +{ + CHECK_LESS_OR_EQUAL(alpha2.size() + 1, ARRAY_SIZE(m_alpha2), ()); + std::strcpy(m_alpha2, alpha2.data()); +} + +void IsoCode::SetAlpha3(std::string const & alpha3) +{ + CHECK_LESS_OR_EQUAL(alpha3.size() + 1, ARRAY_SIZE(m_alpha3), ()); + std::strcpy(m_alpha3, alpha3.data()); +} + +void IsoCode::SetNumeric(std::string const & numeric) +{ + CHECK_LESS_OR_EQUAL(numeric.size() + 1, ARRAY_SIZE(m_numeric), ()); + std::strcpy(m_numeric, numeric.data()); +} + +RegionInfo::RegionInfo(std::string const & filename) +{ + ParseFile(filename); +} + +RegionInfo::RegionInfo(Platform::FilesList const & filenames) +{ + for (auto const & filename : filenames) + ParseFile(filename); +} + +void RegionInfo::ParseFile(std::string const & filename) +{ + try + { + FileReader reader(filename); + ReaderSource<FileReader> src(reader); + uint8_t version; + ReadPrimitiveFromSource(src, version); + CHECK_EQUAL(version, kVersion, ("Versions do not match.")); + ReadMap(src, m_mapRegionData); + ReadMap(src, m_mapIsoCode); + } + catch (FileReader::Exception const & e) + { + LOG(LCRITICAL, ("Failed to parse regions info:", e.Msg())); + } +} + +RegionDataProxy RegionInfo::Get(base::GeoObjectId const & osmId) +{ + return RegionDataProxy(*this, osmId); +} + +RegionDataProxy::RegionDataProxy(RegionInfo & regionInfoCollector, base::GeoObjectId const & osmId) : m_regionInfoCollector(regionInfoCollector), m_osmId(osmId) { } -RegionInfoCollector const & RegionDataProxy::GetCollector() const +RegionInfo const & RegionDataProxy::GetCollector() const { return m_regionInfoCollector; } -RegionInfoCollector & RegionDataProxy::GetCollector() +RegionInfo & RegionDataProxy::GetCollector() { return m_regionInfoCollector; } -RegionInfoCollector::MapRegionData & RegionDataProxy::GetMapRegionData() +MapRegionData & RegionDataProxy::GetMapRegionData() { return GetCollector().m_mapRegionData; } -RegionInfoCollector::MapRegionData const & RegionDataProxy::GetMapRegionData() const +MapRegionData const & RegionDataProxy::GetMapRegionData() const { return GetCollector().m_mapRegionData; } -RegionInfoCollector::MapIsoCode const & RegionDataProxy::GetMapIsoCode() const +MapIsoCode const & RegionDataProxy::GetMapIsoCode() const { return GetCollector().m_mapIsoCode; } diff --git a/generator/regions/region_info_collector.hpp b/generator/regions/collector_region_info.hpp index 633684bed1..06fa05414e 100644 --- a/generator/regions/region_info_collector.hpp +++ b/generator/regions/collector_region_info.hpp @@ -1,5 +1,7 @@ #pragma once +#include "generator/collector_interface.hpp" + #include "platform/platform.hpp" #include "coding/write_to_sink.hpp" @@ -59,68 +61,96 @@ PlaceType EncodePlaceType(std::string const & place); class RegionDataProxy; -// This is a class for working a file with additional information about regions. -class RegionInfoCollector +// Codes for the names of countries, dependent territories, and special areas of geographical +// interest. +// https://en.wikipedia.org/wiki/ISO_3166-1 +struct IsoCode { -public: - static std::string const kDefaultExt; + bool HasAlpha2() const { return m_alpha2[0] != '\0'; } + bool HasAlpha3() const { return m_alpha3[0] != '\0'; } + bool HasNumeric() const { return m_numeric[0] != '\0'; } - RegionInfoCollector() = default; - explicit RegionInfoCollector(std::string const & filename); - explicit RegionInfoCollector(Platform::FilesList const & filenames); - void Add(base::GeoObjectId const & osmId, OsmElement const & el); - RegionDataProxy Get(base::GeoObjectId const & osmId); - void Save(std::string const & filename); + void SetAlpha2(std::string const & alpha2); + void SetAlpha3(std::string const & alpha3); + void SetNumeric(std::string const & numeric); + + std::string GetAlpha2() const { return m_alpha2; } + std::string GetAlpha3() const { return m_alpha3; } + std::string GetNumeric() const { return m_numeric; } + + base::GeoObjectId m_osmId; + char m_alpha2[3] = {}; + char m_alpha3[4] = {}; + char m_numeric[4] = {}; +}; + +struct AdminCenter +{ + AdminCenter() : m_has(false) {} + AdminCenter(base::GeoObjectId const & id) : m_has(true), m_id(id) {} + + bool HasId() const { return m_has; } + base::GeoObjectId GetId() const { return m_id; } private: - friend class RegionDataProxy; + bool m_has; + base::GeoObjectId m_id; +}; - // Codes for the names of countries, dependent territories, and special areas of geographical - // interest. - // https://en.wikipedia.org/wiki/ISO_3166-1 - struct IsoCode - { - bool HasAlpha2() const { return m_alpha2[0] != '\0'; } - bool HasAlpha3() const { return m_alpha3[0] != '\0'; } - bool HasNumeric() const { return m_numeric[0] != '\0'; } +struct RegionData +{ + base::GeoObjectId m_osmId; + AdminLevel m_adminLevel = AdminLevel::Unknown; + PlaceType m_place = PlaceType::Unknown; + AdminCenter m_osmIdAdminCenter; +}; + +using MapRegionData = std::unordered_map<base::GeoObjectId, RegionData>; +using MapIsoCode = std::unordered_map<base::GeoObjectId, IsoCode>; - void SetAlpha2(std::string const & alpha2); - void SetAlpha3(std::string const & alpha3); - void SetNumeric(std::string const & numeric); +// CollectorRegionInfo class is responsible for collecting additional information about regions. +class CollectorRegionInfo : public CollectorInterface +{ +public: + static std::string const kDefaultExt; - std::string GetAlpha2() const { return m_alpha2; } - std::string GetAlpha3() const { return m_alpha3; } - std::string GetNumeric() const { return m_numeric; } + CollectorRegionInfo(std::string const & filename); - base::GeoObjectId m_osmId; - char m_alpha2[3] = {}; - char m_alpha3[4] = {}; - char m_numeric[4] = {}; - }; + // CollectorInterface overrides: + void Collect(base::GeoObjectId const & osmId, OsmElement const & el) override; + void Save() override; - struct AdminCenter +private: + template <typename Sink, typename Map> + void WriteMap(Sink & sink, Map & seq) { - AdminCenter() : m_has(false) {} - AdminCenter(base::GeoObjectId const & id) : m_has(true), m_id(id) {} + static_assert(std::is_trivially_copyable<typename Map::mapped_type>::value, ""); - bool HasId() const { return m_has; } - base::GeoObjectId GetId() const { return m_id; } + uint32_t const sizeRegionData = static_cast<uint32_t>(seq.size()); + WriteToSink(sink, sizeRegionData); + for (auto const & el : seq) + sink.Write(&el.second, sizeof(el.second)); + } + void FillRegionData(base::GeoObjectId const & osmId, OsmElement const & el, RegionData & rd); + void FillIsoCode(base::GeoObjectId const & osmId, OsmElement const & el, IsoCode & rd); + + std::string m_filename; + MapRegionData m_mapRegionData; + MapIsoCode m_mapIsoCode; +}; - private: - bool m_has; - base::GeoObjectId m_id; - }; +// RegionInfo class is responsible for reading and accessing additional information about the regions. +class RegionInfo +{ +public: + RegionInfo() = default; + explicit RegionInfo(std::string const & filename); + explicit RegionInfo(Platform::FilesList const & filenames); - struct RegionData - { - base::GeoObjectId m_osmId; - AdminLevel m_adminLevel = AdminLevel::Unknown; - PlaceType m_place = PlaceType::Unknown; - AdminCenter m_osmIdAdminCenter; - }; + RegionDataProxy Get(base::GeoObjectId const & osmId); - using MapRegionData = std::unordered_map<base::GeoObjectId, RegionData>; - using MapIsoCode = std::unordered_map<base::GeoObjectId, IsoCode>; +private: + friend class RegionDataProxy; template <typename Source, typename Map> void ReadMap(Source & src, Map & seq) @@ -135,20 +165,7 @@ private: } } - template <typename Sink, typename Map> - void WriteMap(Sink & sink, Map & seq) - { - static_assert(std::is_trivially_copyable<typename Map::mapped_type>::value, ""); - - uint32_t const sizeRegionData = static_cast<uint32_t>(seq.size()); - WriteToSink(sink, sizeRegionData); - for (auto const & el : seq) - sink.Write(&el.second, sizeof(el.second)); - } - void ParseFile(std::string const & filename); - void FillRegionData(base::GeoObjectId const & osmId, OsmElement const & el, RegionData & rd); - void FillIsoCode(base::GeoObjectId const & osmId, OsmElement const & el, IsoCode & rd); MapRegionData m_mapRegionData; MapIsoCode m_mapIsoCode; @@ -157,7 +174,7 @@ private: class RegionDataProxy { public: - RegionDataProxy(RegionInfoCollector & regionInfoCollector, base::GeoObjectId const & osmId); + RegionDataProxy(RegionInfo & regionInfoCollector, base::GeoObjectId const & osmId); base::GeoObjectId const & GetOsmId() const; AdminLevel GetAdminLevel() const; @@ -182,13 +199,13 @@ public: private: bool HasIsoCode() const; - RegionInfoCollector & GetCollector(); - RegionInfoCollector const & GetCollector() const; - RegionInfoCollector::MapRegionData & GetMapRegionData(); - RegionInfoCollector::MapRegionData const & GetMapRegionData() const; - RegionInfoCollector::MapIsoCode const & GetMapIsoCode() const; + RegionInfo & GetCollector(); + RegionInfo const & GetCollector() const; + MapRegionData & GetMapRegionData(); + MapRegionData const & GetMapRegionData() const; + MapIsoCode const & GetMapIsoCode() const; - std::reference_wrapper<RegionInfoCollector> m_regionInfoCollector; + std::reference_wrapper<RegionInfo> m_regionInfoCollector; base::GeoObjectId m_osmId; }; diff --git a/generator/regions/region.cpp b/generator/regions/region.cpp index 833a817f5d..06df354184 100644 --- a/generator/regions/region.cpp +++ b/generator/regions/region.cpp @@ -1,7 +1,7 @@ #include "generator/regions/region.hpp" #include "generator/regions/city.hpp" -#include "generator/regions/region_info_collector.hpp" +#include "generator/regions/collector_region_info.hpp" #include "base/assert.hpp" diff --git a/generator/regions/region_base.hpp b/generator/regions/region_base.hpp index 17641a293b..f97695e4c4 100644 --- a/generator/regions/region_base.hpp +++ b/generator/regions/region_base.hpp @@ -1,7 +1,7 @@ #pragma once #include "generator/feature_builder.hpp" -#include "generator/regions/region_info_collector.hpp" +#include "generator/regions/collector_region_info.hpp" #include "geometry/rect2d.hpp" diff --git a/generator/regions/regions.cpp b/generator/regions/regions.cpp index d20ce22d06..85135476f3 100644 --- a/generator/regions/regions.cpp +++ b/generator/regions/regions.cpp @@ -134,7 +134,7 @@ private: }; std::tuple<RegionsBuilder::Regions, PointCitiesMap> -ReadDatasetFromTmpMwm(feature::GenerateInfo const & genInfo, RegionInfoCollector & collector) +ReadDatasetFromTmpMwm(feature::GenerateInfo const & genInfo, RegionInfo & collector) { RegionsBuilder::Regions regions; PointCitiesMap pointCitiesMap; @@ -171,7 +171,7 @@ void FilterRegions(RegionsBuilder::Regions & regions) } RegionsBuilder::Regions ReadData(feature::GenerateInfo const & genInfo, - RegionInfoCollector & regionsInfoCollector) + RegionInfo & regionsInfoCollector) { RegionsBuilder::Regions regions; PointCitiesMap pointCitiesMap; @@ -192,8 +192,8 @@ bool GenerateRegions(feature::GenerateInfo const & genInfo) Transliteration::Instance().Init(GetPlatform().ResourcesDir()); auto const collectorFilename = genInfo.GetTmpFileName(genInfo.m_fileName, - RegionInfoCollector::kDefaultExt); - RegionInfoCollector regionsInfoCollector(collectorFilename); + CollectorRegionInfo::kDefaultExt); + RegionInfo regionsInfoCollector(collectorFilename); RegionsBuilder::Regions regions = ReadData(genInfo, regionsInfoCollector); auto jsonPolicy = std::make_unique<JsonPolicy>(genInfo.m_verbose); auto kvBuilder = std::make_unique<RegionsBuilder>(std::move(regions), std::move(jsonPolicy)); @@ -215,7 +215,7 @@ bool GenerateRegions(feature::GenerateInfo const & genInfo) auto const idStringList = kvBuilder->ToIdStringList(tree); for (auto const & s : idStringList) { - ofs << static_cast<int64_t>(s.first.GetEncodedId()) << " " << s.second << std::endl; + ofs << static_cast<int64_t>(s.first.GetEncodedId()) << " " << s.second << "\n"; ++countIds; if (!setIds.insert(s.first).second) LOG(LWARNING, ("Id alredy exists:", s.first)); |