From a63e8bc2a1420995f1c8a4262fa71c6030bec427 Mon Sep 17 00:00:00 2001 From: Viktor Govako Date: Fri, 28 Jul 2023 00:07:41 -0300 Subject: [PATCH] [gpx] Remove duplicated code. Signed-off-by: cyber-toad Signed-off-by: Viktor Govako --- kml/CMakeLists.txt | 1 + kml/serdes.cpp | 21 ------------------- kml/serdes_common.cpp | 28 +++++++++++++++++++++++++ kml/serdes_common.hpp | 6 ++++++ kml/serdes_gpx.cpp | 17 +-------------- xcode/kml/kml.xcodeproj/project.pbxproj | 14 +++++++++---- 6 files changed, 46 insertions(+), 41 deletions(-) create mode 100644 kml/serdes_common.cpp diff --git a/kml/CMakeLists.txt b/kml/CMakeLists.txt index cab79c16b7..0454c418df 100644 --- a/kml/CMakeLists.txt +++ b/kml/CMakeLists.txt @@ -3,6 +3,7 @@ project(kml) set(SRC header_binary.hpp minzoom_quadtree.hpp + serdes_common.cpp serdes_common.hpp serdes.cpp serdes.hpp diff --git a/kml/serdes.cpp b/kml/serdes.cpp index 77ebe46b24..565d1c19ee 100644 --- a/kml/serdes.cpp +++ b/kml/serdes.cpp @@ -13,8 +13,6 @@ #include "base/string_utils.hpp" #include "base/timer.hpp" -#include - namespace kml { namespace @@ -64,25 +62,6 @@ std::string const kIndent6 = Indent(6); std::string const kIndent8 = Indent(8); std::string const kIndent10 = Indent(10); -std::string PointToString(m2::PointD const & org) -{ - double const lon = mercator::XToLon(org.x); - double const lat = mercator::YToLat(org.y); - - std::ostringstream ss; - ss.precision(8); - - ss << lon << "," << lat; - return ss.str(); -} - -std::string PointToString(geometry::PointWithAltitude const & pt) -{ - if (pt.GetAltitude() != geometry::kInvalidAltitude) - return PointToString(pt.GetPoint()) + "," + strings::to_string(pt.GetAltitude()); - return PointToString(pt.GetPoint()); -} - std::string GetLocalizableString(LocalizableString const & s, int8_t lang) { auto const it = s.find(lang); diff --git a/kml/serdes_common.cpp b/kml/serdes_common.cpp new file mode 100644 index 0000000000..7cfa4ecb61 --- /dev/null +++ b/kml/serdes_common.cpp @@ -0,0 +1,28 @@ +#include "base/string_utils.hpp" +#include "kml/serdes_common.hpp" +#include "geometry/mercator.hpp" +#include + +namespace kml +{ + +std::string PointToString(m2::PointD const & org) +{ + double const lon = mercator::XToLon(org.x); + double const lat = mercator::YToLat(org.y); + + std::ostringstream ss; + ss.precision(8); + + ss << lon << "," << lat; + return ss.str(); +} + +std::string PointToString(geometry::PointWithAltitude const & pt) +{ + if (pt.GetAltitude() != geometry::kInvalidAltitude) + return PointToString(pt.GetPoint()) + "," + strings::to_string(pt.GetAltitude()); + return PointToString(pt.GetPoint()); +} + +} // namespace kml \ No newline at end of file diff --git a/kml/serdes_common.hpp b/kml/serdes_common.hpp index a93082e74e..f70e6e2a04 100644 --- a/kml/serdes_common.hpp +++ b/kml/serdes_common.hpp @@ -1,6 +1,8 @@ #pragma once #include "coding/string_utf8_multilang.hpp" +#include "geometry/point2d.hpp" +#include "geometry/point_with_altitude.hpp" namespace kml { @@ -15,4 +17,8 @@ uint32_t ToRGBA(Channel red, Channel green, Channel blue, Channel alpha) static_cast(blue) << 8 | static_cast(alpha); } +std::string PointToString(m2::PointD const & org); + +std::string PointToString(geometry::PointWithAltitude const & pt); + } // namespace kml diff --git a/kml/serdes_gpx.cpp b/kml/serdes_gpx.cpp index ab01a690a2..9096cabbcf 100644 --- a/kml/serdes_gpx.cpp +++ b/kml/serdes_gpx.cpp @@ -9,8 +9,6 @@ #include "base/assert.hpp" #include "base/string_utils.hpp" -#include - namespace kml { @@ -33,19 +31,6 @@ std::string_view constexpr kMetadata = "metadata"; std::string_view constexpr kEle = "ele"; int constexpr kInvalidColor = 0; -// TODO: Remove copypaste and use elevation/altitude. -std::string PointToString(m2::PointD const & org) -{ - double const lon = mercator::XToLon(org.x); - double const lat = mercator::YToLat(org.y); - - std::ostringstream ss; - ss.precision(8); - - ss << lon << "," << lat; - return ss.str(); -} - GpxParser::GpxParser(FileData & data) : m_data(data) , m_categoryData(&m_data.m_categoryData) @@ -78,7 +63,7 @@ bool GpxParser::MakeValid() { // Set default name. if (m_name.empty()) - m_name[kml::kDefaultLang] = gpx::PointToString(m_org); + m_name[kml::kDefaultLang] = kml::PointToString(m_org); // Set default pin. if (m_predefinedColor == PredefinedColor::None) diff --git a/xcode/kml/kml.xcodeproj/project.pbxproj b/xcode/kml/kml.xcodeproj/project.pbxproj index 6424248b62..0117ac7e52 100644 --- a/xcode/kml/kml.xcodeproj/project.pbxproj +++ b/xcode/kml/kml.xcodeproj/project.pbxproj @@ -15,6 +15,7 @@ 45E456142058509200D9F45E /* testingmain.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 45E456122058508C00D9F45E /* testingmain.cpp */; }; 464344F3294F952700984CB7 /* gpx_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 464344F2294F952700984CB7 /* gpx_tests.cpp */; }; 46AA9E60294549B000ECED73 /* serdes_gpx.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 464BD0FB294546B20011955A /* serdes_gpx.cpp */; }; + ACDD8A7B2A73684F000F2C43 /* serdes_common.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ACDD8A782A736045000F2C43 /* serdes_common.cpp */; }; E2AA225E25275C6B002589E2 /* minzoom_quadtree_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E2AA225C25275C6B002589E2 /* minzoom_quadtree_tests.cpp */; }; E2DC9C9125264E3E0098174E /* types.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E2DC9C9025264E3E0098174E /* types.cpp */; }; FA67C84B26BB365600B33DCA /* libplatform.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FA67C84A26BB365600B33DCA /* libplatform.a */; }; @@ -60,6 +61,8 @@ 464344F2294F952700984CB7 /* gpx_tests.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = gpx_tests.cpp; sourceTree = ""; }; 464BD0FB294546B20011955A /* serdes_gpx.cpp */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.cpp.cpp; path = serdes_gpx.cpp; sourceTree = ""; }; 464BD0FC294546B20011955A /* serdes_gpx.hpp */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.cpp.h; path = serdes_gpx.hpp; sourceTree = ""; }; + ACDD8A772A736045000F2C43 /* serdes_common.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = serdes_common.hpp; sourceTree = ""; }; + ACDD8A782A736045000F2C43 /* serdes_common.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = serdes_common.cpp; sourceTree = ""; }; E2AA225925275C1D002589E2 /* minzoom_quadtree.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = minzoom_quadtree.hpp; sourceTree = ""; }; E2AA225C25275C6B002589E2 /* minzoom_quadtree_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = minzoom_quadtree_tests.cpp; sourceTree = ""; }; E2AA225D25275C6B002589E2 /* tests_data.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = tests_data.hpp; sourceTree = ""; }; @@ -136,10 +139,14 @@ children = ( 45E4558D20584AB900D9F45E /* header_binary.hpp */, E2AA225925275C1D002589E2 /* minzoom_quadtree.hpp */, - 45E4559220584ABA00D9F45E /* serdes_binary.cpp */, - 45E4559320584ABA00D9F45E /* serdes_binary.hpp */, 45E4558E20584AB900D9F45E /* serdes.cpp */, 45E4559020584AB900D9F45E /* serdes.hpp */, + 45E4559220584ABA00D9F45E /* serdes_binary.cpp */, + 45E4559320584ABA00D9F45E /* serdes_binary.hpp */, + ACDD8A782A736045000F2C43 /* serdes_common.cpp */, + ACDD8A772A736045000F2C43 /* serdes_common.hpp */, + 464BD0FB294546B20011955A /* serdes_gpx.cpp */, + 464BD0FC294546B20011955A /* serdes_gpx.hpp */, 4568C86320BD455600E2192B /* type_utils.cpp */, 45E4558F20584AB900D9F45E /* type_utils.hpp */, E2DC9C8925264E0C0098174E /* types_v3.hpp */, @@ -150,8 +157,6 @@ E2DC9C9025264E3E0098174E /* types.cpp */, 45E4559420584ABA00D9F45E /* types.hpp */, 45E4559120584ABA00D9F45E /* visitors.hpp */, - 464BD0FB294546B20011955A /* serdes_gpx.cpp */, - 464BD0FC294546B20011955A /* serdes_gpx.hpp */, ); name = kml; path = ../../kml; @@ -282,6 +287,7 @@ 46AA9E60294549B000ECED73 /* serdes_gpx.cpp in Sources */, 45E4559520584ABA00D9F45E /* serdes.cpp in Sources */, E2DC9C9125264E3E0098174E /* types.cpp in Sources */, + ACDD8A7B2A73684F000F2C43 /* serdes_common.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };