OGS
GeoLib::IO::XmlStnInterface Class Referencefinal

Detailed Description

Reads and writes Observation Sites to and from XML files.

Definition at line 22 of file XmlStnInterface.h.

#include <XmlStnInterface.h>

Inheritance diagram for GeoLib::IO::XmlStnInterface:
[legend]
Collaboration diagram for GeoLib::IO::XmlStnInterface:
[legend]

Public Member Functions

 XmlStnInterface (GeoLib::GEOObjects &geo_objs)
int readFile (const QString &fileName) override
 Reads an xml-file containing station object definitions into the GEOObjects used in the constructor (requires Qt)
bool readFile (std::string const &fname) override
Public Member Functions inherited from BaseLib::IO::Writer
 Writer ()
virtual ~Writer ()=default
std::string writeToString ()
 Writes the object to a string.
Public Member Functions inherited from BaseLib::IO::XMLQtInterface
 XMLQtInterface (QString schemaFile="")
virtual ~XMLQtInterface ()=default

Protected Member Functions

bool write () override
 Writes the object to the internal stream. This method must be implemented by a subclass. The implementation should return true on success, else false.
Protected Member Functions inherited from BaseLib::IO::XMLQtInterface
bool checkHash () const
QByteArray const & getContent () const

Private Member Functions

void readStations (const QDomNode &stationsRoot, std::vector< GeoLib::Point * > &stations, const std::string &station_file_name)
 Reads GeoLib::Station- or StationBorehole-objects from an xml-file.
void writeBoreholeData (QDomDocument &doc, QDomElement &boreholeTag, GeoLib::StationBorehole *borehole) const
 Writes borehole-specific data to a station-xml-file.
void readStratigraphy (const QDomNode &stratRoot, GeoLib::StationBorehole *borehole)
 Reads the stratigraphy of a borehole from an xml-file.

Private Attributes

GeoLib::GEOObjects_geo_objs

Additional Inherited Members

Public Attributes inherited from BaseLib::IO::XMLInterface
std::string export_name = {}
Protected Attributes inherited from BaseLib::IO::Writer
std::ostringstream out
 The stream to write to.

Constructor & Destructor Documentation

◆ XmlStnInterface()

GeoLib::IO::XmlStnInterface::XmlStnInterface ( GeoLib::GEOObjects & geo_objs)
explicit

Definition at line 21 of file XmlStnInterface.cpp.

22 : XMLQtInterface("OpenGeoSysSTN.xsd"), _geo_objs(geo_objs)
23{
24}
XMLQtInterface(QString schemaFile="")
GeoLib::GEOObjects & _geo_objs

References BaseLib::IO::XMLQtInterface::XMLQtInterface(), and _geo_objs.

Member Function Documentation

◆ readFile() [1/2]

int GeoLib::IO::XmlStnInterface::readFile ( const QString & fileName)
overridevirtual

Reads an xml-file containing station object definitions into the GEOObjects used in the constructor (requires Qt)

Reimplemented from BaseLib::IO::XMLQtInterface.

Definition at line 26 of file XmlStnInterface.cpp.

27{
28 if (XMLQtInterface::readFile(fileName) == 0)
29 {
30 return 0;
31 }
32
33 QDomDocument doc("OGS-STN-DOM");
34 doc.setContent(getContent());
35 QDomElement docElement =
36 doc.documentElement(); // root element, used for identifying file-type
37 if (docElement.nodeName().compare("OpenGeoSysSTN"))
38 {
39 ERR("XmlStnInterface::readFile(): Unexpected XML root.");
40 return 0;
41 }
42
43 QDomNodeList lists = docElement.childNodes();
44 for (int i = 0; i < lists.count(); i++)
45 {
46 // read all the station lists
47 QDomNodeList stationList = lists.at(i).childNodes();
48 std::vector<GeoLib::Point*> stations;
49 std::string stnName("[NN]");
50
51 for (int j = 0; j < stationList.count(); j++)
52 {
53 const QDomNode station_node(stationList.at(j));
54 const QString station_type(station_node.nodeName());
55 if (station_type.compare("name") == 0)
56 {
57 stnName = station_node.toElement().text().toStdString();
58 }
59 else if (station_type.compare("stations") == 0)
60 {
61 readStations(station_node, stations, fileName.toStdString());
62 }
63 else if (station_type.compare("boreholes") == 0)
64 {
65 readStations(station_node, stations, fileName.toStdString());
66 }
67 }
68
69 if (!stations.empty())
70 {
71 _geo_objs.addStationVec(std::move(stations), stnName);
72 }
73 }
74
75 return 1;
76}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
QByteArray const & getContent() const
void readStations(const QDomNode &stationsRoot, std::vector< GeoLib::Point * > &stations, const std::string &station_file_name)
Reads GeoLib::Station- or StationBorehole-objects from an xml-file.

References _geo_objs, ERR(), BaseLib::IO::XMLQtInterface::getContent(), and readStations().

Referenced by MainWindow::loadFile(), FileIO::XmlPrjInterface::readFile(), and readFile().

◆ readFile() [2/2]

bool GeoLib::IO::XmlStnInterface::readFile ( std::string const & fname)
inlineoverridevirtual

Implements BaseLib::IO::XMLInterface.

Definition at line 31 of file XmlStnInterface.h.

32 {
33 return readFile(QString(fname.c_str())) != 0;
34 }
int readFile(const QString &fileName) override
Reads an xml-file containing station object definitions into the GEOObjects used in the constructor (...

References readFile().

◆ readStations()

void GeoLib::IO::XmlStnInterface::readStations ( const QDomNode & stationsRoot,
std::vector< GeoLib::Point * > & stations,
const std::string & station_file_name )
private

Reads GeoLib::Station- or StationBorehole-objects from an xml-file.

Definition at line 78 of file XmlStnInterface.cpp.

81{
82 QDomElement station = stationsRoot.firstChildElement();
83 while (!station.isNull())
84 {
85 if (station.hasAttribute("id") && station.hasAttribute("x") &&
86 station.hasAttribute("y"))
87 {
88 std::string stationName("[NN]");
89 std::string sensor_data_file_name;
90 std::string boreholeDate("0000-00-00");
91 double boreholeDepth(0.0);
92 double stationValue(0.0);
93
94 QDomNodeList stationFeatures = station.childNodes();
95 for (int i = 0; i < stationFeatures.count(); i++)
96 {
97 // check for general station features
98 const QDomNode feature_node(stationFeatures.at(i));
99 const QString feature_name(feature_node.nodeName());
100 const QString element_text(feature_node.toElement().text());
101 if (feature_name.compare("name") == 0)
102 {
103 stationName = element_text.toStdString();
104 }
105 if (feature_name.compare("sensordata") == 0)
106 {
107 sensor_data_file_name = element_text.toStdString();
108 /* add other station features here */
109
110 // check for general borehole features
111 }
112 else if (feature_name.compare("value") == 0)
113 {
114 stationValue = element_text.toDouble();
115 }
116 else if (feature_name.compare("bdepth") == 0)
117 {
118 boreholeDepth = element_text.toDouble();
119 }
120 else if (feature_name.compare("bdate") == 0)
121 {
122 boreholeDate = element_text.toStdString();
123 }
124 /* add other borehole features here */
125 }
126
127 double zVal = (station.hasAttribute("z"))
128 ? station.attribute("z").toDouble()
129 : 0.0;
130
131 if (station.nodeName().compare("station") == 0)
132 {
133 GeoLib::Station* s =
134 new GeoLib::Station(station.attribute("x").toDouble(),
135 station.attribute("y").toDouble(),
136 zVal,
137 stationName);
138 s->setStationValue(stationValue);
139 if (!sensor_data_file_name.empty())
140 {
142 station_file_name, sensor_data_file_name));
143 }
144 stations.push_back(s);
145 }
146 else if (station.nodeName().compare("borehole") == 0)
147 {
148 GeoLib::StationBorehole* s =
150 stationName,
151 station.attribute("x").toDouble(),
152 station.attribute("y").toDouble(),
153 zVal,
154 boreholeDepth,
155 boreholeDate);
156 s->setStationValue(stationValue);
157 /* add stratigraphy to the borehole */
158 for (int j = 0; j < stationFeatures.count(); j++)
159 {
160 if (stationFeatures.at(j).nodeName().compare("strat") == 0)
161 {
162 this->readStratigraphy(stationFeatures.at(j), s);
163 }
164 }
165
166 stations.push_back(s);
167 }
168 }
169 else
170 {
171 WARN(
172 "XmlStnInterface::readStations(): Attribute missing in "
173 "<station> tag.");
174 }
175 station = station.nextSiblingElement();
176 }
177}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
void readStratigraphy(const QDomNode &stratRoot, GeoLib::StationBorehole *borehole)
Reads the stratigraphy of a borehole from an xml-file.
static StationBorehole * createStation(const std::string &name, double x, double y, double z, double depth, const std::string &date="")
Creates a new borehole object based on the given parameters.
void addSensorDataFromCSV(const std::string &file_name)
Allows to add sensor data from a CSV file to the observation site.
Definition Station.h:72
void setStationValue(double station_value)
Definition Station.h:66
std::string joinPaths(std::string const &pathA, std::string const &pathB)

References GeoLib::Station::addSensorDataFromCSV(), GeoLib::StationBorehole::createStation(), BaseLib::joinPaths(), readStratigraphy(), GeoLib::Station::setStationValue(), and WARN().

Referenced by readFile().

◆ readStratigraphy()

void GeoLib::IO::XmlStnInterface::readStratigraphy ( const QDomNode & stratRoot,
GeoLib::StationBorehole * borehole )
private

Reads the stratigraphy of a borehole from an xml-file.

Definition at line 179 of file XmlStnInterface.cpp.

181{
182 // borehole->addSoilLayer((*borehole)[0], (*borehole)[1], (*borehole)[2],
183 // "");
184 double depth_check((*borehole)[2]);
185 QDomElement horizon = stratRoot.firstChildElement();
186 while (!horizon.isNull())
187 {
188 if (horizon.hasAttribute("id") && horizon.hasAttribute("x") &&
189 horizon.hasAttribute("y") && horizon.hasAttribute("z"))
190 {
191 std::string horizonName("[NN]");
192
193 QDomNodeList horizonFeatures = horizon.childNodes();
194 for (int i = 0; i < horizonFeatures.count(); i++)
195 {
196 if (horizonFeatures.at(i).nodeName().compare("name") == 0)
197 {
198 horizonName =
199 horizonFeatures.at(i).toElement().text().toStdString();
200 }
201 }
202 /* add other horizon features here */
203
204 double depth(horizon.attribute("z").toDouble());
205 if (std::abs(depth - depth_check) >
206 std::numeric_limits<double>::
207 epsilon()) // skip soil-layer if its thickness is zero
208 {
209 borehole->addSoilLayer(horizon.attribute("x").toDouble(),
210 horizon.attribute("y").toDouble(),
211 depth,
212 horizonName);
213 depth_check = depth;
214 }
215 else
216 {
217 WARN(
218 "XmlStnInterface::readStratigraphy(): Skipped layer '{:s}' "
219 "in borehole '{:s}' because of thickness 0.0.",
220 horizonName, borehole->getName());
221 }
222 }
223 else
224 {
225 WARN(
226 "XmlStnInterface::readStratigraphy(): Attribute missing in "
227 "<horizon> tag.");
228 }
229 horizon = horizon.nextSiblingElement();
230 }
231}
void addSoilLayer(double thickness, const std::string &soil_name)
Add a soil layer to the boreholes stratigraphy.
std::string const & getName() const
Returns the name of the station.
Definition Station.h:49

References GeoLib::StationBorehole::addSoilLayer(), GeoLib::Station::getName(), and WARN().

Referenced by readStations().

◆ write()

bool GeoLib::IO::XmlStnInterface::write ( )
overrideprotectedvirtual

Writes the object to the internal stream. This method must be implemented by a subclass. The implementation should return true on success, else false.

Implements BaseLib::IO::Writer.

Definition at line 233 of file XmlStnInterface.cpp.

234{
235 if (export_name.empty())
236 {
237 ERR("XmlStnInterface::write(): No station list specified.");
238 return false;
239 }
240
241 out << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; // xml
242 // definition
243 out << "<?xml-stylesheet type=\"text/xsl\" "
244 "href=\"OpenGeoSysSTN.xsl\"?>\n\n"; // stylefile definition
245
246 QDomDocument doc("OGS-STN-DOM");
247 QDomElement root = doc.createElement("OpenGeoSysSTN");
248 root.setAttribute("xmlns:ogs", "http://www.opengeosys.org");
249 root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
250
251 const std::vector<GeoLib::Point*>* stations(
252 _geo_objs.getStationVec(export_name));
253 bool const is_borehole =
254 dynamic_cast<GeoLib::StationBorehole*>((*stations)[0]);
255
256 doc.appendChild(root);
257 QDomElement stationListTag = doc.createElement("stationlist");
258 root.appendChild(stationListTag);
259
260 QDomElement listNameTag = doc.createElement("name");
261 stationListTag.appendChild(listNameTag);
262 QDomText stationListNameText =
263 doc.createTextNode(QString::fromStdString(export_name));
264 listNameTag.appendChild(stationListNameText);
265 QString listType = is_borehole ? "boreholes" : "stations";
266 QDomElement stationsTag = doc.createElement(listType);
267 stationListTag.appendChild(stationsTag);
268
269 bool useStationValue(false);
270 double sValue =
271 static_cast<GeoLib::Station*>((*stations)[0])->getStationValue();
272 std::size_t nStations(stations->size());
273 for (std::size_t i = 1; i < nStations; i++)
274 {
275 if ((static_cast<GeoLib::Station*>((*stations)[i])->getStationValue() -
276 sValue) < std::numeric_limits<double>::epsilon())
277 {
278 useStationValue = true;
279 break;
280 }
281 }
282
283 for (std::size_t i = 0; i < nStations; i++)
284 {
285 QString stationType = is_borehole ? "borehole" : "station";
286 QDomElement stationTag = doc.createElement(stationType);
287 stationTag.setAttribute("id", QString::number(i));
288 stationTag.setAttribute(
289 "x", QString::number((*(*stations)[i])[0], 'f',
290 std::numeric_limits<double>::max_digits10));
291 stationTag.setAttribute(
292 "y", QString::number((*(*stations)[i])[1], 'f',
293 std::numeric_limits<double>::max_digits10));
294 stationTag.setAttribute(
295 "z", QString::number((*(*stations)[i])[2], 'f',
296 std::numeric_limits<double>::max_digits10));
297 stationsTag.appendChild(stationTag);
298
299 QDomElement stationNameTag = doc.createElement("name");
300 stationTag.appendChild(stationNameTag);
301 QDomText stationNameText = doc.createTextNode(QString::fromStdString(
302 static_cast<GeoLib::Station*>((*stations)[i])->getName()));
303 stationNameTag.appendChild(stationNameText);
304
305 if (useStationValue)
306 {
307 QDomElement stationValueTag = doc.createElement("value");
308 stationTag.appendChild(stationValueTag);
309 QDomText stationValueText = doc.createTextNode(
310 QString::number(static_cast<GeoLib::Station*>((*stations)[i])
311 ->getStationValue()));
312 stationValueTag.appendChild(stationValueText);
313 }
314
315 if (is_borehole)
316 {
318 doc, stationTag,
319 static_cast<GeoLib::StationBorehole*>((*stations)[i]));
320 }
321 }
322
323 std::string xml = doc.toString().toStdString();
324 out << xml;
325 return true;
326}
std::string getName(std::string const &line)
Returns the name/title from the "Zone"-description.
std::ostringstream out
The stream to write to.
Definition Writer.h:36
void writeBoreholeData(QDomDocument &doc, QDomElement &boreholeTag, GeoLib::StationBorehole *borehole) const
Writes borehole-specific data to a station-xml-file.

References _geo_objs, ERR(), BaseLib::IO::XMLInterface::export_name, getName(), BaseLib::IO::Writer::out, and writeBoreholeData().

◆ writeBoreholeData()

void GeoLib::IO::XmlStnInterface::writeBoreholeData ( QDomDocument & doc,
QDomElement & boreholeTag,
GeoLib::StationBorehole * borehole ) const
private

Writes borehole-specific data to a station-xml-file.

the first entry in the profile vector is just the position of the borehole

Definition at line 328 of file XmlStnInterface.cpp.

331{
332 QDomElement stationDepthTag = doc.createElement("bdepth");
333 boreholeTag.appendChild(stationDepthTag);
334 QDomText stationDepthText =
335 doc.createTextNode(QString::number(borehole->getDepth(), 'f'));
336 stationDepthTag.appendChild(stationDepthText);
337 if (std::abs(borehole->getDate()) > 0)
338 {
339 QDomElement stationDateTag = doc.createElement("bdate");
340 boreholeTag.appendChild(stationDateTag);
341 QDomText stationDateText = doc.createTextNode(
342 QString::fromStdString(BaseLib::date2string(borehole->getDate())));
343 stationDateTag.appendChild(stationDateText);
344 }
345
346 std::vector<GeoLib::Point*> profile = borehole->getProfile();
347 std::vector<std::string> soilNames = borehole->getSoilNames();
348 std::size_t nHorizons(profile.size());
349
350 if (nHorizons > 1)
351 {
352 QDomElement stratTag = doc.createElement("strat");
353 boreholeTag.appendChild(stratTag);
354
355 for (std::size_t j = 1; j < nHorizons;
356 j++)
358 {
359 QDomElement horizonTag = doc.createElement("horizon");
360 horizonTag.setAttribute("id", QString::number(j));
361 horizonTag.setAttribute("x",
362 QString::number((*profile[j])[0], 'f'));
363 horizonTag.setAttribute("y",
364 QString::number((*profile[j])[1], 'f'));
365 horizonTag.setAttribute("z",
366 QString::number((*profile[j])[2], 'f'));
367 stratTag.appendChild(horizonTag);
368 QDomElement horizonNameTag = doc.createElement("name");
369 horizonTag.appendChild(horizonNameTag);
370 QDomText horizonNameText =
371 doc.createTextNode(QString::fromStdString(soilNames[j]));
372 horizonNameTag.appendChild(horizonNameText);
373 }
374 }
375}
const std::vector< std::string > & getSoilNames() const
double getDate() const
Returns the date entry for the borehole.
const std::vector< Point * > & getProfile() const
std::string date2string(double ddate)
Definition DateTools.cpp:54

References BaseLib::date2string(), GeoLib::StationBorehole::getDate(), GeoLib::StationBorehole::getDepth(), GeoLib::StationBorehole::getProfile(), and GeoLib::StationBorehole::getSoilNames().

Referenced by write().

Member Data Documentation

◆ _geo_objs

GeoLib::GEOObjects& GeoLib::IO::XmlStnInterface::_geo_objs
private

Definition at line 53 of file XmlStnInterface.h.

Referenced by XmlStnInterface(), readFile(), and write().


The documentation for this class was generated from the following files: