44{
45 TCLAP::CmdLine cmd(
46 "Remove points from geometry that are not used in any polyline nor "
47 "surface. Attention: Names of points are not handled correctly in "
48 "every case.\n\n"
49 "OpenGeoSys-6 software, version " +
51 ".\n"
52 "Copyright (c) 2012-2025, OpenGeoSys Community "
53 "(http://www.opengeosys.org)",
55
56 TCLAP::ValueArg<std::string> geo_output_arg(
57 "o", "output", "Output (.gml) geometry", true, "", "OUTPUT_FILE");
58 cmd.add(geo_output_arg);
59 TCLAP::ValueArg<std::string> geo_input_arg(
60 "i", "input", "Input (.gml) geometry", true, "conceptual model",
61 "INPUT_FILE");
62 cmd.add(geo_input_arg);
64 cmd.add(log_level_arg);
65 cmd.parse(argc, argv);
66
69
72 try
73 {
74 if (!xml.readFile(geo_input_arg.getValue()))
75 {
76 ERR(
"Failed to read file `{:s}'.", geo_input_arg.getValue());
77 return EXIT_FAILURE;
78 }
79 }
80 catch (std::runtime_error const& err)
81 {
82 ERR(
"Failed to read file `{:s}'.", geo_input_arg.getValue());
83 ERR(
"{:s}", err.what());
84 return EXIT_FAILURE;
85 }
86
88 auto* points = const_cast<std::vector<GeoLib::Point*>*>(
92
93
94 auto used_points = std::vector<bool>(points->size(), false);
95
96 auto mark = [&](auto const* const object)
98
99 if (polylines)
100 {
101 ranges::for_each(*polylines, mark);
102 }
103 if (surfaces)
104 {
105 ranges::for_each(*surfaces, mark);
106 }
107
108 auto const number_of_used_points = static_cast<std::size_t>(
109 std::count(used_points.begin(), used_points.end(), true));
110 if (number_of_used_points == 0)
111 {
113 "Geometry consists of points only. A new geometry file won't "
114 "be written.");
115 return EXIT_SUCCESS;
116 }
118 "{} points in this geometry file are not used in any polyline or "
119 "surface",
120 points->size() - number_of_used_points);
121
123
124 auto reset = [&mapping](auto* object)
126
127
128 if (polylines)
129 {
130 ranges::for_each(*polylines, reset);
131 }
132
133 if (surfaces)
134 {
135 ranges::for_each(*surfaces, reset);
136 }
137
138 std::stringstream unused_points_info;
139 unused_points_info << std::fixed;
140
141
143 ranges::views::zip(*points, used_points) |
144 ranges::views::
filter([](auto&& pair) {
return !pair.second; }) |
145 ranges::views::keys)
146 {
147 unused_points_info << point->getID() << " " << *point << '\n';
148 delete point;
149 point = nullptr;
150 }
151
152 std::erase(*points, nullptr);
153 if (!unused_points_info.str().empty())
154 {
155 INFO(
"Removed the following points:\n{}", unused_points_info.str());
156 }
157
158 WARN(
"Names of points are not handled correctly in every case.");
159
162 geo_output_arg.getValue());
163
164 return EXIT_SUCCESS;
165}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
auto createMapping(std::vector< bool > const &used_points)
Container class for geometric objects.
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
const std::vector< Point * > * getPointVec(const std::string &name) const
const std::vector< Surface * > * getSurfaceVec(const std::string &name) const
Returns the surface vector with the given name as a const.
const std::vector< Polyline * > * getPolylineVec(const std::string &name) const
int writeStringToFile(std::string_view content, std::filesystem::path const &file_path)
constexpr List filter(Pred, SomeListOfTypes<> *)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
void markUsedPoints(Polyline const &polyline, std::vector< bool > &used_points)
Resets the point IDs of the polyline corresponding to the mapping.
void resetPointIDs(Polyline &polyline, std::vector< std::size_t > const &mapping)
Resets the point IDs of the polyline corresponding to the mapping.
GITINFOLIB_EXPORT const std::string ogs_version