6#include <boost/property_tree/xml_parser.hpp>
33 doc.ignoreConfigAttribute(
"xmlns:xsi");
34 doc.ignoreConfigAttribute(
"xsi:noNamespaceSchemaLocation");
35 doc.ignoreConfigAttribute(
"xmlns:ogs");
38 auto geo_name = doc.getConfigParameter<std::string>(
"name");
41 OGS_FATAL(
"BoostXmlGmlInterface::readFile(): <name> tag is empty.");
45 for (
auto st : doc.getConfigSubtreeList(
"points"))
47 std::vector<GeoLib::Point*> points;
51 std::move(pnt_names));
54 std::vector<GeoLib::Polyline*> polylines;
57 for (
auto st : doc.getConfigSubtreeList(
"polylines"))
66 std::vector<GeoLib::Surface*> surfaces;
70 for (
auto st : doc.getConfigSubtreeList(
"surfaces"))
77 if (!polylines.empty())
79 _geo_objects.addPolylineVec(std::move(polylines), geo_name,
80 std::move(ply_names));
83 if (!surfaces.empty())
85 _geo_objects.addSurfaceVec(std::move(surfaces), geo_name,
86 std::move(sfc_names));
94 std::vector<GeoLib::Point*>& points,
95 std::map<std::string, std::size_t>& pnt_names)
101 auto const p_id = pt.getConfigAttribute<std::size_t>(
"id");
103 auto const p_x = pt.getConfigAttribute<
double>(
"x");
105 auto const p_y = pt.getConfigAttribute<
double>(
"y");
107 auto const p_z = pt.getConfigAttribute<
double>(
"z");
109 auto const p_size = points.size();
111 "The point id is not unique.");
114 if (
auto const p_name =
116 pt.getConfigAttributeOptional<std::string>(
"name"))
120 OGS_FATAL(
"Empty point name found in geometry file.");
124 pnt_names, *p_name, p_size,
"The point name is not unique.");
131 std::vector<GeoLib::Polyline*>& polylines,
132 std::vector<GeoLib::Point*>
const& points,
133 std::vector<std::size_t>
const& pnt_id_map,
134 std::map<std::string, std::size_t>& ply_names)
140 auto const id = pl.getConfigAttribute<std::size_t>(
"id");
147 if (
auto const p_name =
149 pl.getConfigAttributeOptional<std::string>(
"name"))
153 OGS_FATAL(
"Empty polyline name found in geometry file.");
157 ply_names, *p_name, polylines.size() - 1,
158 "The polyline name is not unique.");
160 auto accessOrError = [
this, &p_name](
auto pt_idx)
162 auto search =
_idx_map.find(pt_idx);
166 "Polyline `{:s}' contains the point id `{:d}', but the "
167 "id is not in the point list.",
168 p_name->c_str(), pt_idx);
170 return search->second;
174 for (
auto const pt : pl.getConfigParameterList<std::size_t>(
"pnt"))
176 polylines.back()->addPoint(pnt_id_map[accessOrError(pt)]);
182 pl.ignoreConfigParameterAll(
"pnt");
184 "Polyline name is required! Polylines without a name are "
192 std::vector<GeoLib::Surface*>& surfaces,
193 std::vector<GeoLib::Point*>
const& points,
194 const std::vector<std::size_t>& pnt_id_map,
195 std::map<std::string, std::size_t>& sfc_names)
201 auto const id = sfc.getConfigAttribute<std::size_t>(
"id");
207 if (
auto const s_name =
209 sfc.getConfigAttributeOptional<std::string>(
"name"))
213 OGS_FATAL(
"Empty surface name found in geometry file.");
217 sfc_names, *s_name, surfaces.size() - 1,
218 "The surface name is not unique.");
221 for (
auto const& element : sfc.getConfigParameterList(
"element"))
225 element.getConfigAttribute<std::size_t>(
"p1");
228 element.getConfigAttribute<std::size_t>(
"p2");
231 element.getConfigAttribute<std::size_t>(
"p3");
233 auto accessOrError = [
this, &s_name](std::size_t pt_idx)
235 auto search =
_idx_map.find(pt_idx);
239 "The element list of the surface `{:s}' contains "
240 "the invalid point id `{:d}'.",
241 s_name->c_str(), pt_idx);
243 return search->second;
246 auto const p1 = pnt_id_map[accessOrError(p1_attr)];
247 auto const p2 = pnt_id_map[accessOrError(p2_attr)];
248 auto const p3 = pnt_id_map[accessOrError(p3_attr)];
249 surfaces.back()->addTriangle(p1, p2, p3);
255 sfc.ignoreConfigParameterAll(
"element");
264 ERR(
"BoostXmlGmlInterface::write(): No geometry specified.");
272 ERR(
"BoostXmlGmlInterface::write(): No PointVec within the geometry "
281 ERR(
"BoostXmlGmlInterface::write(): No points within the geometry "
288 boost::property_tree::ptree pt;
291 pt.put(
"<xmlattr>.xmlns:xsi",
"http://www.w3.org/2001/XMLSchema-instance");
292 pt.put(
"<xmlattr>.xmlns:ogs",
"https://www.opengeosys.org");
293 auto& geometry_set = pt.add(
"OpenGeoSysGLI",
"");
296 auto& pnts_tag = geometry_set.add(
"points",
"");
297 for (std::size_t k(0); k < pnts.size(); k++)
299 auto& pnt_tag = pnts_tag.add(
"point",
"");
300 pnt_tag.put(
"<xmlattr>.id", k);
301 pnt_tag.put(
"<xmlattr>.x", (*pnts[k])[0]);
302 pnt_tag.put(
"<xmlattr>.y", (*pnts[k])[1]);
303 pnt_tag.put(
"<xmlattr>.z", (*pnts[k])[2]);
305 if (!point_name.empty())
307 pnt_tag.put(
"<xmlattr>.name", point_name);
314 boost::property_tree::xml_writer_settings<std::string> settings(
'\t', 1);
315 write_xml(
out, pt, settings);
320 boost::property_tree::ptree& geometry_set)
327 "BoostXmlGmlInterface::addSurfacesToPropertyTree(): No surfaces "
328 "within the geometry '{:s}'.",
333 auto const& surfaces(sfc_vec->
getVector());
334 if (surfaces.empty())
337 "BoostXmlGmlInterface::addSurfacesToPropertyTree(): No surfaces "
338 "within the geometry '{:s}'.",
343 auto& surfaces_tag = geometry_set.add(
"surfaces",
"");
344 for (std::size_t i = 0; i < surfaces.size(); ++i)
347 std::string sfc_name;
349 auto& surface_tag = surfaces_tag.add(
"surface",
"");
350 surface_tag.put(
"<xmlattr>.id", i);
351 if (!sfc_name.empty())
353 surface_tag.put(
"<xmlattr>.name", sfc_name);
357 auto& element_tag = surface_tag.add(
"element",
"");
358 element_tag.put(
"<xmlattr>.p1", (*(*surface)[j])[0]);
359 element_tag.put(
"<xmlattr>.p2", (*(*surface)[j])[1]);
360 element_tag.put(
"<xmlattr>.p3", (*(*surface)[j])[2]);
366 boost::property_tree::ptree& geometry_set)
373 "BoostXmlGmlInterface::addPolylinesToPropertyTree(): No polylines "
374 "within the geometry '{:s}'.",
380 if (polylines.empty())
383 "BoostXmlGmlInterface::addPolylinesToPropertyTree(): No polylines "
384 "within the geometry '{:s}'.",
389 auto& polylines_tag = geometry_set.add(
"polylines",
"");
390 for (std::size_t i = 0; i < polylines.size(); ++i)
393 std::string ply_name;
395 auto& polyline_tag = polylines_tag.add(
"polyline",
"");
396 polyline_tag.put(
"<xmlattr>.id", i);
397 if (!ply_name.empty())
399 polyline_tag.put(
"<xmlattr>.name", ply_name);
403 polyline_tag.add(
"pnt", polyline->
getPointID(j));
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)
Range< SubtreeIterator > getConfigSubtreeList(std::string const &root) const
Range< ValueIterator< T > > getConfigParameterList(std::string const ¶m) const
std::ostringstream out
The stream to write to.
Container class for geometric objects.
bool write() override
Writes the object to the internal stream. This method must be implemented by a subclass....
GeoLib::GEOObjects & _geo_objects
void readPolylines(BaseLib::ConfigTree const &polylinesRoot, std::vector< GeoLib::Polyline * > &polylines, std::vector< GeoLib::Point * > const &points, const std::vector< std::size_t > &pnt_id_map, std::map< std::string, std::size_t > &ply_names)
Reads GeoLib::Polyline-objects from an xml-file.
void addPolylinesToPropertyTree(boost::property_tree::ptree &geometry_set)
void readPoints(BaseLib::ConfigTree const &pointsRoot, std::vector< GeoLib::Point * > &points, std::map< std::string, std::size_t > &pnt_names)
Reads GeoLib::Point-objects from an xml-file.
void readSurfaces(BaseLib::ConfigTree const &surfacesRoot, std::vector< GeoLib::Surface * > &surfaces, std::vector< GeoLib::Point * > const &points, const std::vector< std::size_t > &pnt_id_map, std::map< std::string, std::size_t > &sfc_names)
Reads GeoLib::Surface-objects from an xml-file.
bool readFile(const std::string &fname) override
Reads an xml-file containing OGS geometry.
void addSurfacesToPropertyTree(boost::property_tree::ptree &geometry_set)
std::map< std::size_t, std::size_t > _idx_map
BoostXmlGmlInterface(GeoLib::GEOObjects &geo_objs)
This class manages pointers to Points in a std::vector along with a name. It also handles the deletio...
std::string const & getItemNameByID(std::size_t id) const
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
std::size_t getPointID(std::size_t const i) const
std::size_t getNumberOfPoints() const
A Surface is represented by Triangles. It consists of a reference to a vector of (pointers to) points...
std::size_t getNumberOfTriangles() const
std::map< std::string, std::size_t > NameIdMap
bool getNameOfElement(const T *data, std::string &name) const
std::vector< T * > const & getVector() const
ConfigTree makeConfigTreeFromFile(const std::string &filepath, const bool be_ruthless, const std::string &toplevel_tag)
void insertIfKeyUniqueElseError(Map &map, Key const &key, Value &&value, std::string const &error_message)
TemplateVec< GeoLib::Surface > SurfaceVec
TemplateVec< GeoLib::Polyline > PolylineVec
class PolylineVec encapsulate a std::vector of Polylines additional one can give the vector of polyli...