From 7ad4551e4c83df30b01f210dd4cb2a4c6b34fe82 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Mon, 14 Nov 2016 17:13:14 +0300 Subject: [PATCH] Using final stage osm ids to feature ids mapping for restriction generation. --- generator/gen_mwm_info.hpp | 12 ++-- .../restriction_collector_test.cpp | 2 +- generator/generator_tool/generator_tool.cpp | 2 +- generator/restriction_collector.cpp | 72 ++++++++++--------- generator/restriction_collector.hpp | 11 +-- generator/restriction_generator.cpp | 8 +-- 6 files changed, 56 insertions(+), 51 deletions(-) diff --git a/generator/gen_mwm_info.hpp b/generator/gen_mwm_info.hpp index bd58e4282e..d9f8ce99af 100644 --- a/generator/gen_mwm_info.hpp +++ b/generator/gen_mwm_info.hpp @@ -4,13 +4,14 @@ #include "base/assert.hpp" -#include "std/utility.hpp" #include "std/algorithm.hpp" - +#include "std/fstream.hpp" +#include "std/iostream.hpp" +#include "std/string.hpp" +#include "std/utility.hpp" namespace gen { - template class Accumulator { protected: @@ -30,6 +31,8 @@ public: { rw::ReadVectorOfPOD(src, m_data); } + + vector const & GetData() const { return m_data; } }; class OsmID2FeatureID : public Accumulator> @@ -63,5 +66,4 @@ public: return 0; } }; - -} +} // namespace gen diff --git a/generator/generator_tests/restriction_collector_test.cpp b/generator/generator_tests/restriction_collector_test.cpp index 233e8f952f..838a9a65e1 100644 --- a/generator/generator_tests/restriction_collector_test.cpp +++ b/generator/generator_tests/restriction_collector_test.cpp @@ -101,7 +101,7 @@ UNIT_TEST(RestrictionTest_ParseFeatureId2OsmIdsMapping) RestrictionCollector restrictionCollector("" /* restrictionPath */, "" /* featureIdToOsmIdsPath */); Platform const & platform = Platform(); - restrictionCollector.ParseFeatureId2OsmIdsMapping( + restrictionCollector.ParseOsmIdToFeatureIdMapping( my::JoinFoldersToPath(platform.WritableDir(), kFeatureIdToOsmIdsPath)); vector> const expectedOsmIds2FeatureId = { diff --git a/generator/generator_tool/generator_tool.cpp b/generator/generator_tool/generator_tool.cpp index 7e7394cc24..96843e401b 100644 --- a/generator/generator_tool/generator_tool.cpp +++ b/generator/generator_tool/generator_tool.cpp @@ -244,7 +244,7 @@ int main(int argc, char ** argv) { routing::BuildRoadRestrictions( datFile, genInfo.GetIntermediateFileName(genInfo.m_restrictions, "" /* extention */), - genInfo.GetIntermediateFileName(genInfo.m_featureIdToOsmIds, "" /* extention */)); + genInfo.GetTargetFileName(country) + OSM2FEATURE_FILE_EXTENSION); } } diff --git a/generator/restriction_collector.cpp b/generator/restriction_collector.cpp index ba0395f25a..3c977fe6e2 100644 --- a/generator/restriction_collector.cpp +++ b/generator/restriction_collector.cpp @@ -1,5 +1,10 @@ #include "generator/restriction_collector.hpp" +#include "generator/gen_mwm_info.hpp" + +#include "coding/file_reader.hpp" +#include "coding/reader.hpp" + #include "base/assert.hpp" #include "base/logging.hpp" #include "base/scope_guard.hpp" @@ -31,17 +36,17 @@ bool ParseLineOfNumbers(strings::SimpleTokenizer & iter, vector & numb namespace routing { RestrictionCollector::RestrictionCollector(string const & restrictionPath, - string const & featureIdToOsmIdsPath) + string const & osmIdsToFeatureIdPath) { MY_SCOPE_GUARD(clean, [this](){ m_osmIdToFeatureId.clear(); m_restrictions.clear(); }); - if (!ParseFeatureId2OsmIdsMapping(featureIdToOsmIdsPath)) + if (!ParseOsmIdToFeatureIdMapping(osmIdsToFeatureIdPath)) { LOG(LWARNING, ("An error happened while parsing feature id to osm ids mapping from file:", - featureIdToOsmIdsPath)); + osmIdsToFeatureIdPath)); return; } @@ -65,34 +70,34 @@ bool RestrictionCollector::IsValid() const [](Restriction const & r) { return !r.IsValid(); }) == end(m_restrictions); } -bool RestrictionCollector::ParseFeatureId2OsmIdsMapping(string const & featureIdToOsmIdsPath) +bool RestrictionCollector::ParseOsmIdToFeatureIdMapping(string const & osmIdsToFeatureIdPath) { - ifstream featureIdToOsmIdsStream(featureIdToOsmIdsPath); - if (featureIdToOsmIdsStream.fail()) - return false; + LOG(LINFO, ("osmIdsToFeatureIdPath =", osmIdsToFeatureIdPath)); - string line; - while (getline(featureIdToOsmIdsStream, line)) + using OsmIdToFeatureId = pair; + gen::Accumulator osmIdsToFeatureIds; + try { - vector osmIds; - strings::SimpleTokenizer iter(line, kDelim); - if (!ParseLineOfNumbers(iter, osmIds)) - { - LOG(LWARNING, ("Cannot parse feature id to osm ids mapping. Line:", line)); - return false; - } - - if (osmIds.size() < 2) - { - LOG(LWARNING, ("Parse result of mapping feature id to osm ids is too small. Line:", - line)); - return false; // Every line should contain feature id and at least one osm id. - } - - uint32_t const featureId = static_cast(osmIds.front()); - osmIds.erase(osmIds.begin()); - AddFeatureId(featureId, osmIds); + FileReader reader(osmIdsToFeatureIdPath); + ReaderSource src(reader); + osmIdsToFeatureIds.Read(src); } + catch (FileReader::Exception const & e) + { + LOG(LERROR, ("Exception while reading file:", osmIdsToFeatureIdPath, ". Msg:", e.Msg())); + return false; + } + + vector const & mapping = osmIdsToFeatureIds.GetData(); + if (mapping.empty()) + { + LOG(LINFO, ("No osm ids to feature ids mapping in file", osmIdsToFeatureIdPath)); + return true; + } + + for (auto const osmIdToFeatureId : mapping) + AddFeatureId(osmIdToFeatureId.second /* feature id */, osmIdToFeatureId.first /* osm id */); + return true; } @@ -150,18 +155,15 @@ bool RestrictionCollector::AddRestriction(Restriction::Type type, vector const & osmIds) +void RestrictionCollector::AddFeatureId(uint32_t featureId, uint64_t osmId) { // Note. One |featureId| could correspond to several osm ids. // But for road feature |featureId| corresponds to exactly one osm id. - for (uint64_t const & osmId : osmIds) + auto const result = m_osmIdToFeatureId.insert(make_pair(osmId, featureId)); + if (result.second == false) { - auto const result = m_osmIdToFeatureId.insert(make_pair(osmId, featureId)); - if (result.second == false) - { - LOG(LERROR, ("Osm id", osmId, "is included in two feature ids: ", featureId, - m_osmIdToFeatureId.find(osmId)->second)); - } + LOG(LERROR, ("Osm id", osmId, "is included in two feature ids: ", featureId, + m_osmIdToFeatureId.find(osmId)->second)); } } diff --git a/generator/restriction_collector.hpp b/generator/restriction_collector.hpp index 19a04a1f95..155e84973c 100644 --- a/generator/restriction_collector.hpp +++ b/generator/restriction_collector.hpp @@ -42,10 +42,10 @@ private: /// 137999, 5170186, /// 138000, 5170209, 5143342, /// 138001, 5170228, - /// \param featureIdToOsmIdsPath path to the text file. + /// \param osmIdsToFeatureIdPath path to the text file. /// \note Most restrictions consist of type and two linear(road) features. /// \note For the time being only line-point-line restrictions are supported. - bool ParseFeatureId2OsmIdsMapping(string const & featureIdToOsmIdsPath); + bool ParseOsmIdToFeatureIdMapping(string const & osmIdsToFeatureIdPath); /// \brief Parses comma separated text file with line in following format: /// , , , and so on @@ -56,9 +56,10 @@ private: /// \param featureIdToOsmIdsPath path to the text file. bool ParseRestrictions(string const & path); - /// \brief Adds feature id and corresponding vector of |osmIds| to |m_osmIdToFeatureId|. - /// \note One feature id (|featureId|) may correspond to several osm ids (|osmIds|). - void AddFeatureId(uint32_t featureId, vector const & osmIds); + /// \brief Adds feature id and corresponding |osmId| to |m_osmIdToFeatureId|. + /// \note In general one feature id (|featureId|) may correspond to several osm ids (|osmIds|). + /// But for road feature one feature id corresponds one osm id. + void AddFeatureId(uint32_t featureId, uint64_t osmId); /// \brief Adds a restriction (vector of osm id). /// \param type is a type of restriction diff --git a/generator/restriction_generator.cpp b/generator/restriction_generator.cpp index 2a5eb1a4bf..1320686461 100644 --- a/generator/restriction_generator.cpp +++ b/generator/restriction_generator.cpp @@ -15,15 +15,15 @@ namespace routing { bool BuildRoadRestrictions(string const & mwmPath, string const & restrictionPath, - string const & featureIdToOsmIdsPath) + string const & osmIdsTofeatureIdsPath) { LOG(LINFO, - ("BuildRoadRestrictions(", mwmPath, ", ", restrictionPath, ", ", featureIdToOsmIdsPath, ");")); - RestrictionCollector restrictionCollector(restrictionPath, featureIdToOsmIdsPath); + ("BuildRoadRestrictions(", mwmPath, ", ", restrictionPath, ", ", osmIdsTofeatureIdsPath, ");")); + RestrictionCollector restrictionCollector(restrictionPath, osmIdsTofeatureIdsPath); if (!restrictionCollector.HasRestrictions() || !restrictionCollector.IsValid()) { LOG(LWARNING, ("No valid restrictions for", mwmPath, "It's necessary to check that", - restrictionPath, "and", featureIdToOsmIdsPath, "are available.")); + restrictionPath, "and", osmIdsTofeatureIdsPath, "are available.")); return false; }