diff --git a/generator/generator_tool/generator_tool.cpp b/generator/generator_tool/generator_tool.cpp index 090290ddaf..32f4d75d52 100644 --- a/generator/generator_tool/generator_tool.cpp +++ b/generator/generator_tool/generator_tool.cpp @@ -172,8 +172,8 @@ DEFINE_bool(generate_traffic_keys, false, // Generating geo objects key-value. DEFINE_string(regions_index, "", "Input regions index file."); DEFINE_string(regions_key_value, "", "Input regions key-value file."); -DEFINE_string(geo_objects_features, "", "Input geo_objects tmp.mwm file."); -DEFINE_string(ids_without_address, "", "Output file with objects ids without adresses."); +DEFINE_string(geo_objects_features, "", "Input tmp.mwm file with geo objects."); +DEFINE_string(ids_without_addresses, "", "Output file with objects ids without addresses."); DEFINE_string(geo_objects_key_value, "", "Output geo objects key-value file."); // Common. @@ -329,7 +329,7 @@ int main(int argc, char ** argv) if (!FLAGS_geo_objects_key_value.empty()) { if (!geo_objects::GenerateGeoObjects(FLAGS_regions_index, FLAGS_regions_key_value, - FLAGS_geo_objects_features, FLAGS_ids_without_address, + FLAGS_geo_objects_features, FLAGS_ids_without_addresses, FLAGS_geo_objects_key_value, FLAGS_verbose)) return -1; } diff --git a/generator/geo_objects/geo_objects.cpp b/generator/geo_objects/geo_objects.cpp index 05565c53ac..bd826e96b7 100644 --- a/generator/geo_objects/geo_objects.cpp +++ b/generator/geo_objects/geo_objects.cpp @@ -5,7 +5,6 @@ #include "indexer/locality_index.hpp" -#include "coding/file_container.hpp" #include "coding/mmap_reader.hpp" #include "base/geo_object_id.hpp" @@ -22,89 +21,77 @@ namespace using KeyValue = std::map; using IndexReader = ReaderPtr; -template -typename Index::Type ReadIndex(std::string const & pathIndx) -{ - FilesContainerR cont(pathIndx); - auto const offsetSize = cont.GetAbsoluteOffsetAndSize(Index::kFileTag); - MmapReader reader(pathIndx); - typename Index::ReaderType subReader(reader.CreateSubReader(offsetSize.first, offsetSize.second)); - typename Index::Type index(subReader); - return index; -} - -std::vector ReadTmpMwm(std::string const & pathInGeoObjectsTmpMwm) +std::vector ReadTmpMwm(std::string const & pathToGeoObjectsTmpMwm) { std::vector geoObjects; auto const toDo = [&geoObjects](FeatureBuilder1 & fb, uint64_t /* currPos */) { geoObjects.emplace_back(std::move(fb)); }; - feature::ForEachFromDatRawFormat(pathInGeoObjectsTmpMwm, toDo); + feature::ForEachFromDatRawFormat(pathToGeoObjectsTmpMwm, toDo); return geoObjects; } -KeyValue ReadRegionsKv(std::string const & pathInRegionsKv) +KeyValue ReadRegionsKv(std::string const & pathToRegionsKv) { KeyValue regionKv; - std::ifstream stream(pathInRegionsKv); + std::ifstream stream(pathToRegionsKv); std::string line; while (std::getline(stream, line)) { auto const pos = line.find(" "); if (pos == std::string::npos) { - LOG(LWARNING, ("Can not find separator.")); + LOG(LWARNING, ("Cannot find separator.")); continue; } - int64_t tmpId; - if (!strings::to_int64(line.substr(0, pos), tmpId)) + int64_t id; + if (!strings::to_int64(line.substr(0, pos), id)) { - LOG(LWARNING, ("Can not parse id.")); + LOG(LWARNING, ("Cannot parse id.")); continue; } base::Json json; try { - json = base::Json(line.substr(pos + 1, line.size())); + json = base::Json(line.substr(pos + 1)); if (!json.get()) continue; } - catch (...) + catch (base::Json::Exception const &) { - LOG(LWARNING, ("Can not create base::Json.")); + LOG(LWARNING, ("Cannot create base::Json.")); continue; } - regionKv.emplace(static_cast(tmpId), json); + regionKv.emplace(static_cast(id), json); } return regionKv; } -template +template std::vector SearchObjectsInIndex(FeatureBuilder1 const & fb, Index const & index) { std::vector ids; - auto const fn = [&ids] (const base::GeoObjectId & osmid) { ids.emplace_back(osmid); }; + auto const fn = [&ids] (base::GeoObjectId const & osmid) { ids.emplace_back(osmid); }; auto const center = fb.GetLimitRect().Center(); auto const rect = MercatorBounds::RectByCenterXYAndSizeInMeters(center, 0 /* meters */); index.ForEachInRect(fn, rect); return ids; } -int GetRankFromValue(const base::Json & json) +int GetRankFromValue(base::Json const & json) { - int tempRank; + int rank; auto properties = json_object_get(json.get(), "properties"); - FromJSONObject(properties, "rank", tempRank); - return tempRank; + FromJSONObject(properties, "rank", rank); + return rank; } base::Json GetDeepestRegion(std::vector const & ids, KeyValue const & regionKv) { - base::Json deepest; int deepestRank = 0; for (auto const & id : ids) @@ -113,13 +100,16 @@ base::Json GetDeepestRegion(std::vector const & ids, KeyValue auto const it = regionKv.find(id.GetEncodedId()); if (it == std::end(regionKv)) { - LOG(LWARNING, ("Not found id in region key-value:", id)); + LOG(LWARNING, ("Id not found in region key-value:", id)); continue; } temp = it->second; if (!json_is_object(temp.get())) + { + LOG(LWARNING, ("Value is not a json object in region key-value:", id)); continue; + } if (!deepest.get()) { @@ -129,7 +119,7 @@ base::Json GetDeepestRegion(std::vector const & ids, KeyValue else { int tempRank = GetRankFromValue(temp); - if (tempRank > deepestRank) + if (deepestRank < tempRank) { deepest = temp; deepestRank = tempRank; @@ -140,7 +130,7 @@ base::Json GetDeepestRegion(std::vector const & ids, KeyValue return deepest; } -base::Json AddAddress(FeatureBuilder1 const & fb, base::Json const regionJson) +base::Json AddAddress(FeatureBuilder1 const & fb, base::Json const & regionJson) { base::Json result = regionJson.GetDeepCopy(); int const kHouseRank = 30; @@ -153,7 +143,7 @@ base::Json AddAddress(FeatureBuilder1 const & fb, base::Json const regionJson) if (!street.empty()) ToJSONObject(*address, "street", street); - // auto localies = json_object_get(result.get(), "localies"); + // auto locales = json_object_get(result.get(), "locales"); // auto en = json_object_get(result.get(), "en"); // todo(maksimandrianov): Add en locales. return result; @@ -161,7 +151,7 @@ base::Json AddAddress(FeatureBuilder1 const & fb, base::Json const regionJson) std::unique_ptr MakeGeoObjectValue(FeatureBuilder1 const & fb, - typename indexer::RegionsIndex::Type const & regionIndex, + typename indexer::RegionsIndexBox::IndexType const & regionIndex, KeyValue const & regionKv) { auto const ids = SearchObjectsInIndex(fb, regionIndex); @@ -173,7 +163,7 @@ MakeGeoObjectValue(FeatureBuilder1 const & fb, return buffer; } -bool GenerateGeoObjects(typename indexer::RegionsIndex::Type const & regionIndex, +bool GenerateGeoObjects(typename indexer::RegionsIndexBox::IndexType const & regionIndex, KeyValue const & regionKv, std::vector const & geoObjects, std::ostream & streamIdsWithoutAddress, @@ -184,14 +174,14 @@ bool GenerateGeoObjects(typename indexer::RegionsIndex::Type const if (fb.GetParams().house.IsEmpty()) continue; - const auto value = MakeGeoObjectValue(fb, regionIndex, regionKv); + auto const value = MakeGeoObjectValue(fb, regionIndex, regionKv); streamGeoObjectsKv << static_cast(fb.GetMostGenericOsmId().GetEncodedId()) << " " << value.get() << "\n"; } return true; } -} +} // namespace namespace generator { @@ -203,7 +193,7 @@ bool GenerateGeoObjects(std::string const & pathInRegionsIndx, std::string const & pathOutIdsWithoutAddress, std::string const & pathOutGeoObjectsKv, bool verbose) { - auto const index = ReadIndex>(pathInRegionsIndx); + auto const index = indexer::ReadIndex, MmapReader>(pathInRegionsIndx); auto const regionsKv = ReadRegionsKv(pathInRegionsKv); auto geoObjects = ReadTmpMwm(pathInGeoObjectsTmpMwm); std::ofstream streamIdsWithoutAddress(pathOutIdsWithoutAddress); diff --git a/indexer/indexer_tests/locality_index_test.cpp b/indexer/indexer_tests/locality_index_test.cpp index 4766ca9667..fc115ba97f 100644 --- a/indexer/indexer_tests/locality_index_test.cpp +++ b/indexer/indexer_tests/locality_index_test.cpp @@ -83,7 +83,7 @@ UNIT_TEST(BuildLocalityIndexTest) BuildGeoObjectsIndex(objects, writer, "tmp"); MemReader reader(localityIndex.data(), localityIndex.size()); - indexer::GeoObjectsIndex::Type index(reader); + indexer::GeoObjectsIndex index(reader); TEST_EQUAL(GetIds(index, m2::RectD{-0.5, -0.5, 0.5, 0.5}), (Ids{1}), ()); TEST_EQUAL(GetIds(index, m2::RectD{0.5, -0.5, 1.5, 1.5}), (Ids{2, 3}), ()); @@ -104,7 +104,7 @@ UNIT_TEST(LocalityIndexRankTest) BuildGeoObjectsIndex(objects, writer, "tmp"); MemReader reader(localityIndex.data(), localityIndex.size()); - indexer::GeoObjectsIndex::Type index(reader); + indexer::GeoObjectsIndex index(reader); TEST_EQUAL(GetRankedIds(index, m2::PointD{1, 0} /* center */, m2::PointD{4, 0} /* border */, 4 /* topSize */), (vector{1, 2, 3, 4}), ()); @@ -136,7 +136,7 @@ UNIT_TEST(LocalityIndexTopSizeTest) BuildGeoObjectsIndex(objects, writer, "tmp"); MemReader reader(localityIndex.data(), localityIndex.size()); - indexer::GeoObjectsIndex::Type index(reader); + indexer::GeoObjectsIndex index(reader); TEST_EQUAL(GetRankedIds(index, m2::PointD{1, 0} /* center */, m2::PointD{0, 0} /* border */, 4 /* topSize */) .size(), diff --git a/indexer/locality_index.hpp b/indexer/locality_index.hpp index f1841e0e24..83ffaa9013 100644 --- a/indexer/locality_index.hpp +++ b/indexer/locality_index.hpp @@ -6,6 +6,8 @@ #include "indexer/locality_object.hpp" #include "indexer/scales.hpp" +#include "coding/file_container.hpp" + #include "geometry/rect2d.hpp" #include "base/geo_object_id.hpp" @@ -14,6 +16,7 @@ #include #include #include +#include #include "defines.hpp" @@ -77,20 +80,37 @@ private: }; template -struct GeoObjectsIndex +using GeoObjectsIndex = LocalityIndex; + +template +using RegionsIndex = LocalityIndex; + +template +struct GeoObjectsIndexBox { static constexpr const char * kFileTag = GEO_OBJECTS_INDEX_FILE_TAG; using ReaderType = Reader; - using Type = LocalityIndex; + using IndexType = GeoObjectsIndex; }; template -struct RegionsIndex +struct RegionsIndexBox { static constexpr const char * kFileTag = REGIONS_INDEX_FILE_TAG; using ReaderType = Reader; - using Type = LocalityIndex; + using IndexType = RegionsIndex; }; + +template +typename IndexBox::IndexType ReadIndex(std::string const & pathIndx) +{ + FilesContainerR cont(pathIndx); + auto const offsetSize = cont.GetAbsoluteOffsetAndSize(IndexBox::kFileTag); + Reader reader(pathIndx); + typename IndexBox::ReaderType subReader(reader.CreateSubReader(offsetSize.first, offsetSize.second)); + typename IndexBox::IndexType index(subReader); + return index; +} } // namespace indexer