OGS
FileIO Namespace Reference

Detailed Description

Contains the interfaces for all file types that can be read and/or written using OpenGeoSys.

See also
GMSH
Legacy

Namespaces

 GMSH
 
 Gocad
 
 Legacy
 

Classes

class  AsciiRasterInterface
 
class  CsvInterface
 
class  FEFLOWGeoInterface
 
class  FEFLOWMeshInterface
 
class  GMSInterface
 Manages the import and export of Aquaveo GMS files into and out of GeoLib. More...
 
class  PetrelInterface
 
class  SHPInterface
 Manages the import of ESRI shape files into GeoLib. More...
 
class  SwmmInterface
 
class  TetGenInterface
 
class  XmlLutReader
 Reader for vtk-Lookup-Tables (in XML / ParaView Format) More...
 
class  XmlPrjInterface
 

Enumerations

enum class  SwmmObject { SUBCATCHMENT = 0 , NODE = 1 , LINK = 2 , SYSTEM = 3 }
 SWMM object types. More...
 

Functions

static double readDoubleFromStream (std::istream &in)
 Reads a double replacing comma by point. More...
 
static std::optional< GeoLib::RasterHeaderreadASCHeader (std::ifstream &in)
 
static std::optional< std::tuple< GeoLib::RasterHeader, double, double > > readSurferHeader (std::ifstream &in)
 
static bool allRastersExist (std::vector< std::string > const &raster_paths)
 Checks if all raster files actually exist. More...
 
std::optional< std::vector< GeoLib::Raster const * > > readRasters (std::vector< std::string > const &raster_paths)
 
bool createSurface (GeoLib::Polyline const &ply, GeoLib::GEOObjects &geometries, std::string const &geometry_name, std::string const &gmsh_binary)
 
std::unique_ptr< GeoLib::SurfacecreateSurfaceWithEarClipping (GeoLib::Polyline const &line)
 
void readGeometryFromFile (std::string const &fname, GeoLib::GEOObjects &geo_objs, std::string const &gmsh_path)
 
void writeGeometryToFile (std::string const &geo_name, GeoLib::GEOObjects &geo_objs, std::string const &fname)
 
void addTextNode (QDomDocument &doc, QDomElement &parent, QString const &node_name, QString const &content)
 
bool PVarExists (std::string const &name, std::vector< DataHolderLib::ProcessVariable > const &p_vars)
 

Variables

const std::array< std::string, 9 > subcatchment_vars
 Variables that always exist for subcatchments. There might be more. More...
 
const std::array< std::string, 7 > node_vars
 Variables that always exist for nodes. There might be more. More...
 
const std::array< std::string, 6 > link_vars
 Variables that always exist for links. There might be more. More...
 
const std::array< std::string, 15 > system_vars
 All variables that exist for the system. More...
 
std::array< std::size_t, 4 > const n_obj_params = {8, 6, 5, 15}
 

Enumeration Type Documentation

◆ SwmmObject

enum FileIO::SwmmObject
strong

SWMM object types.

Enumerator
SUBCATCHMENT 
NODE 
LINK 
SYSTEM 

Definition at line 34 of file SWMMInterface.h.

35 {
36  SUBCATCHMENT = 0,
37  NODE = 1,
38  LINK = 2,
39  SYSTEM = 3
40 };

Function Documentation

◆ addTextNode()

void FileIO::addTextNode ( QDomDocument &  doc,
QDomElement &  parent,
QString const &  node_name,
QString const &  content 
)

Definition at line 426 of file XmlPrjInterface.cpp.

430 {
431  QDomElement tag = doc.createElement(node_name);
432  parent.appendChild(tag);
433  QDomText order_text = doc.createTextNode(content);
434  tag.appendChild(order_text);
435 }

Referenced by FileIO::XmlPrjInterface::writeCondition(), and FileIO::XmlPrjInterface::writeProcessVariables().

◆ allRastersExist()

static bool FileIO::allRastersExist ( std::vector< std::string > const &  raster_paths)
static

Checks if all raster files actually exist.

Definition at line 276 of file AsciiRasterInterface.cpp.

277 {
278  for (const auto& raster_path : raster_paths)
279  {
280  std::ifstream file_stream(raster_path, std::ifstream::in);
281  if (!file_stream.good())
282  {
283  return false;
284  }
285  file_stream.close();
286  }
287  return true;
288 }

Referenced by readRasters().

◆ createSurface()

bool FileIO::createSurface ( GeoLib::Polyline const &  ply,
GeoLib::GEOObjects geometries,
std::string const &  geometry_name,
std::string const &  gmsh_binary 
)

Creates a plane surface from the given polyline. The polyline has to be closed, i.e. the first and the last point have to be the identical. The triangulation of the polyline is done by the finite element meshing tool Gmsh. Finally, the resulting mesh is converted into a GeoLib::Surface which is inserted into the GeoLib::GEOObjects instance geometries using the name geometry_name.

Definition at line 33 of file createSurface.cpp.

37 {
38  if (!ply.isClosed())
39  {
40  WARN("Error in createSurface() - Polyline is not closed.");
41  return false;
42  }
43 
44  if (ply.getNumberOfPoints() <= 2)
45  {
46  WARN(
47  "Error in createSurface() - Polyline consists of less "
48  "than three points and therefore cannot be triangulated.");
49  return false;
50  }
51 
52  // create new GEOObjects and insert a copy of the polyline
53  auto polyline_points = std::make_unique<std::vector<GeoLib::Point*>>();
55  auto ply_points = ply.getPointsVec();
56  std::transform(ply_points.begin(), ply_points.end(),
57  std::back_inserter(*polyline_points),
58  [](auto const* p) { return new GeoLib::Point(*p); });
59  std::string ply_name = "temporary_polyline_name";
60  geo.addPointVec(std::move(polyline_points), ply_name);
61  auto polyline =
62  std::make_unique<GeoLib::Polyline>(*geo.getPointVec(ply_name));
63  for (std::size_t k(0); k < ply.getNumberOfPoints(); ++k)
64  {
65  polyline->addPoint(ply.getPointID(k));
66  }
67  auto polylines = std::make_unique<std::vector<GeoLib::Polyline*>>();
68  polylines->push_back(polyline.release());
69  geo.addPolylineVec(std::move(polylines), ply_name);
70 
71  // use GMSHInterface to create a mesh from the closed polyline
72  auto const geo_names = geo.getGeometryNames();
75  0.0, 0, geo_names, false, false);
76 
77  // write to random file in temp directory
78  auto geo_file = fs::temp_directory_path() /= BaseLib::randomString(32);
79  auto msh_file = fs::temp_directory_path() /= BaseLib::randomString(32);
80 
81  BaseLib::IO::writeStringToFile(gmsh_io.writeToString(), geo_file);
82  // Using GMSH's vtk output here so we don't have to deal with GMSH and it's
83  // various file format versions here
84  std::string gmsh_command = "\"" + gmsh_binary +
85  "\" -2 -algo meshadapt -format vtk -o " +
86  msh_file.string() + " " + geo_file.string();
87 
88  int const gmsh_return_value = std::system(gmsh_command.c_str());
89  if (gmsh_return_value != 0)
90  {
91  WARN("Call to '{:s}' returned non-zero value {:d}.", gmsh_command,
92  gmsh_return_value);
93  }
94  auto surface_mesh = MeshLib::IO::readMeshFromFile(msh_file.string());
95  if (!surface_mesh)
96  {
97  WARN("The surface mesh could not be created.");
98  return false;
99  }
100  if (!(fs::remove(geo_file) && fs::remove(msh_file)))
101  {
102  WARN("Could not remove temporary files in createSurface.");
103  }
104 
105  // convert the surface mesh into a geometric surface
106  if (!MeshLib::convertMeshToGeo(*surface_mesh, geometries,
107  std::numeric_limits<double>::epsilon()))
108  {
109  WARN("The surface mesh could not be converted to a geometry.");
110  return false;
111  }
112  std::string merged_geometry_name("geometry_with_surfaces");
113  geometries.mergeGeometries({geometry_name, surface_mesh->getName()},
114  merged_geometry_name);
115  geometries.removeSurfaceVec(geometry_name);
116  geometries.removePolylineVec(geometry_name);
117  geometries.removePointVec(geometry_name);
118  geometries.removeSurfaceVec(surface_mesh->getName());
119  geometries.removePolylineVec(surface_mesh->getName());
120  geometries.removePointVec(surface_mesh->getName());
121  geometries.renameGeometry(merged_geometry_name, geometry_name);
122 
123  return true;
124 }
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
Reads and writes GMSH-files to and from OGS data structures.
Definition: GMSHInterface.h:46
Container class for geometric objects.
Definition: GEOObjects.h:61
void renameGeometry(std::string const &old_name, std::string const &new_name)
Definition: GEOObjects.cpp:630
bool removePointVec(const std::string &name)
Definition: GEOObjects.cpp:97
bool removeSurfaceVec(const std::string &name)
Definition: GEOObjects.cpp:323
int mergeGeometries(std::vector< std::string > const &geo_names, std::string &merged_geo_name)
Definition: GEOObjects.cpp:435
bool removePolylineVec(const std::string &name)
Definition: GEOObjects.cpp:243
int writeStringToFile(std::string content, std::filesystem::path const &file_path)
Definition: Writer.cpp:45
std::string randomString(std::size_t const length)
Returns a random string of the given length containing just a-z,A-Z,0-9.
@ FixedMeshDensity
set the parameter with a fixed value
static const double p
MeshLib::Mesh * readMeshFromFile(const std::string &file_name)
bool convertMeshToGeo(const MeshLib::Mesh &mesh, GeoLib::GEOObjects &geo_objects, double const eps)

References MeshLib::convertMeshToGeo(), FileIO::GMSH::FixedMeshDensity, GeoLib::Polyline::getNumberOfPoints(), GeoLib::Polyline::getPointID(), GeoLib::Polyline::getPointsVec(), GeoLib::Polyline::isClosed(), GeoLib::GEOObjects::mergeGeometries(), BaseLib::randomString(), MeshLib::IO::readMeshFromFile(), GeoLib::GEOObjects::removePointVec(), GeoLib::GEOObjects::removePolylineVec(), GeoLib::GEOObjects::removeSurfaceVec(), GeoLib::GEOObjects::renameGeometry(), WARN(), BaseLib::IO::writeStringToFile(), and BaseLib::IO::Writer::writeToString().

Referenced by main(), FileIO::SHPInterface::readPolygons(), and FileIO::Legacy::readSurfaces().

◆ createSurfaceWithEarClipping()

std::unique_ptr< GeoLib::Surface > FileIO::createSurfaceWithEarClipping ( GeoLib::Polyline const &  line)

Creates a plane surface from the given polyline. The polyline has to be closed, i.e. the first and the last point have to be the identical. The triangulation of the polyline is done via a simple ear clipping algorithm.

Definition at line 126 of file createSurface.cpp.

128 {
129  if (!line.isClosed())
130  {
131  WARN("Error in Surface::createSurface() - Polyline is not closed.");
132  return nullptr;
133  }
134 
135  if (line.getNumberOfPoints() <= 2)
136  {
137  WARN(
138  "Error in Surface::createSurface() - Polyline consists of less "
139  "than three points and therefore cannot be triangulated.");
140  return nullptr;
141  }
142 
143  // create empty surface
144  auto sfc = std::make_unique<GeoLib::Surface>(line.getPointsVec());
145  auto polygon = std::make_unique<GeoLib::Polygon>(GeoLib::Polygon(line));
146  std::list<GeoLib::Polygon*> const& list_of_simple_polygons =
147  polygon->computeListOfSimplePolygons();
148 
149  for (auto const& simple_polygon : list_of_simple_polygons)
150  {
151  std::list<GeoLib::Triangle> triangles;
152  GeoLib::EarClippingTriangulation(*simple_polygon, triangles);
153 
154  // add Triangles to Surface
155  for (auto const& t : triangles)
156  {
157  sfc->addTriangle(t[0], t[1], t[2]);
158  }
159  }
160  if (sfc->getNumberOfTriangles() == 0)
161  {
162  WARN(
163  "Surface::createSurface(): Triangulation does not contain any "
164  "triangles.");
165  return nullptr;
166  }
167  return sfc;
168 }

References GeoLib::Polyline::getNumberOfPoints(), GeoLib::Polyline::getPointsVec(), GeoLib::Polyline::isClosed(), and WARN().

Referenced by GEOModels::connectPolylineSegments().

◆ PVarExists()

bool FileIO::PVarExists ( std::string const &  name,
std::vector< DataHolderLib::ProcessVariable > const &  p_vars 
)

Definition at line 437 of file XmlPrjInterface.cpp.

439 {
440  return std::any_of(p_vars.begin(), p_vars.end(),
441  [&](auto const& p_var) { return p_var.name == name; });
442 }

Referenced by FileIO::XmlPrjInterface::getPrimaryVariableVec().

◆ readASCHeader()

static std::optional<GeoLib::RasterHeader> FileIO::readASCHeader ( std::ifstream &  in)
static

Reads the header of a Esri asc-file. If the return value is empty, reading was not successful.

Definition at line 50 of file AsciiRasterInterface.cpp.

51 {
52  GeoLib::RasterHeader header;
53 
54  std::string tag;
55  std::string value;
56 
57  in >> tag;
58  if (tag == "ncols")
59  {
60  in >> value;
61  header.n_cols = atoi(value.c_str());
62  }
63  else
64  {
65  return {};
66  }
67 
68  in >> tag;
69  if (tag == "nrows")
70  {
71  in >> value;
72  header.n_rows = atoi(value.c_str());
73  }
74  else
75  {
76  return {};
77  }
78 
79  header.n_depth = 1;
80 
81  in >> tag;
82  if (tag == "xllcorner" || tag == "xllcenter")
83  {
84  header.origin[0] = readDoubleFromStream(in);
85  }
86  else
87  {
88  return {};
89  }
90 
91  in >> tag;
92  if (tag == "yllcorner" || tag == "yllcenter")
93  {
94  header.origin[1] = readDoubleFromStream(in);
95  }
96  else
97  {
98  return {};
99  }
100  header.origin[2] = 0;
101 
102  in >> tag;
103  if (tag == "cellsize")
104  {
105  header.cell_size = readDoubleFromStream(in);
106  }
107  else
108  {
109  return {};
110  }
111 
112  in >> tag;
113  if (tag == "NODATA_value" || tag == "nodata_value")
114  {
115  header.no_data = readDoubleFromStream(in);
116  }
117  else
118  {
119  return {};
120  }
121 
122  return header;
123 }
static double readDoubleFromStream(std::istream &in)
Reads a double replacing comma by point.
Contains the relevant information when storing a geoscientific raster data.
Definition: Raster.h:25
std::size_t n_depth
Definition: Raster.h:28
double cell_size
Definition: Raster.h:30
MathLib::Point3d origin
Definition: Raster.h:29
std::size_t n_cols
Definition: Raster.h:26
std::size_t n_rows
Definition: Raster.h:27

References GeoLib::RasterHeader::cell_size, GeoLib::RasterHeader::n_cols, GeoLib::RasterHeader::n_depth, GeoLib::RasterHeader::n_rows, GeoLib::RasterHeader::no_data, GeoLib::RasterHeader::origin, and readDoubleFromStream().

Referenced by FileIO::AsciiRasterInterface::getRasterFromASCFile().

◆ readDoubleFromStream()

static double FileIO::readDoubleFromStream ( std::istream &  in)
static

Reads a double replacing comma by point.

Definition at line 40 of file AsciiRasterInterface.cpp.

41 {
42  std::string value;
43  in >> value;
44  return std::strtod(BaseLib::replaceString(",", ".", value).c_str(),
45  nullptr);
46 }
std::string replaceString(const std::string &searchString, const std::string &replaceString, std::string stringToReplace)
Definition: StringTools.cpp:50

References BaseLib::replaceString().

Referenced by FileIO::AsciiRasterInterface::getRasterFromASCFile(), FileIO::AsciiRasterInterface::getRasterFromSurferFile(), and readASCHeader().

◆ readGeometryFromFile()

void FileIO::readGeometryFromFile ( std::string const &  fname,
GeoLib::GEOObjects geo_objs,
std::string const &  gmsh_path 
)

Definition at line 23 of file readGeometryFromFile.cpp.

26 {
27  if (BaseLib::getFileExtension(fname) == ".gml")
28  {
30  xml.readFile(fname);
31  }
32  else
33  {
34  std::vector<std::string> errors;
35  std::string geo_name; // geo_name is output of the reading function
37  fname, geo_objs, geo_name, errors, gmsh_path);
38  }
39 
40  if (geo_objs.getGeometryNames().empty())
41  {
42  OGS_FATAL(
43  "GEOObjects has no geometry name after reading the geometry file. "
44  "Something is wrong in the reading function.");
45  }
46 }
#define OGS_FATAL(...)
Definition: Error.h:26
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
Definition: GEOObjects.cpp:401
std::string getFileExtension(const std::string &path)
Definition: FileTools.cpp:186
bool readGLIFileV4(const std::string &fname, GeoLib::GEOObjects &geo, std::string &unique_name, std::vector< std::string > &errors, std::string const &gmsh_path)
Definition: OGSIOVer4.cpp:506

References BaseLib::getFileExtension(), GeoLib::GEOObjects::getGeometryNames(), OGS_FATAL, GeoLib::IO::BoostXmlGmlInterface::readFile(), and FileIO::Legacy::readGLIFileV4().

Referenced by main().

◆ readRasters()

std::optional< std::vector< GeoLib::Raster const * > > FileIO::readRasters ( std::vector< std::string > const &  raster_paths)

Reads a vector of rasters given by file names. On error nothing is returned, otherwise the returned vector contains pointers to the read rasters.

Definition at line 290 of file AsciiRasterInterface.cpp.

292 {
293  if (!allRastersExist(raster_paths))
294  {
295  return std::nullopt;
296  }
297 
298  std::vector<GeoLib::Raster const*> rasters;
299  rasters.reserve(raster_paths.size());
300  std::transform(raster_paths.begin(), raster_paths.end(),
301  std::back_inserter(rasters),
302  [](auto const& path)
303  { return FileIO::AsciiRasterInterface::readRaster(path); });
304  return std::make_optional(rasters);
305 }
static bool allRastersExist(std::vector< std::string > const &raster_paths)
Checks if all raster files actually exist.

References allRastersExist().

Referenced by MeshLayerEditDialog::createPrismMesh(), MeshLayerEditDialog::createTetMesh(), and main().

◆ readSurferHeader()

static std::optional<std::tuple<GeoLib::RasterHeader, double, double> > FileIO::readSurferHeader ( std::ifstream &  in)
static

Reads the header of a Surfer grd-file with minimum and maximum values. If the return value is empty, reading was not successful.

Definition at line 164 of file AsciiRasterInterface.cpp.

165 {
166  std::string tag;
167 
168  in >> tag;
169 
170  if (tag != "DSAA")
171  {
172  ERR("Error in readSurferHeader() - No Surfer file.");
173  return {};
174  }
175 
176  GeoLib::RasterHeader header;
177  in >> header.n_cols >> header.n_rows;
178  double min, max;
179  in >> min >> max;
180  header.origin[0] = min;
181  header.cell_size = (max - min) / static_cast<double>(header.n_cols);
182 
183  in >> min >> max;
184  header.origin[1] = min;
185  header.origin[2] = 0;
186 
187  if (ceil((max - min) / static_cast<double>(header.n_rows)) ==
188  ceil(header.cell_size))
189  {
190  header.cell_size = ceil(header.cell_size);
191  }
192  else
193  {
194  ERR("Error in readSurferHeader() - Anisotropic cellsize detected.");
195  return {};
196  }
197  header.n_depth = 1;
198  header.no_data = min - 1;
199  in >> min >> max;
200 
201  return {{header, min, max}};
202 }
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42

References GeoLib::RasterHeader::cell_size, ERR(), GeoLib::RasterHeader::n_cols, GeoLib::RasterHeader::n_depth, GeoLib::RasterHeader::n_rows, GeoLib::RasterHeader::no_data, and GeoLib::RasterHeader::origin.

Referenced by FileIO::AsciiRasterInterface::getRasterFromSurferFile().

◆ writeGeometryToFile()

void FileIO::writeGeometryToFile ( std::string const &  geo_name,
GeoLib::GEOObjects geo_objs,
std::string const &  fname 
)

Write geometry given by the geo_objs object and specified by the name stored in param geo_name either to a gml or a gli file. If the extension given in the fname parameter is "gml" or "GML" a gml file is written. In case the extension is "gli" or "GLI" a gli file is written.

Definition at line 20 of file writeGeometryToFile.cpp.

22 {
23  std::string const extension(BaseLib::getFileExtension(fname));
24  if (extension == ".gml" || extension == ".GML")
25  {
27  xml.export_name = geo_name;
28  BaseLib::IO::writeStringToFile(xml.writeToString(), fname);
29  }
30  else if (extension == "gli" || extension == "GLI")
31  {
32  FileIO::Legacy::writeGLIFileV4(fname, geo_name, geo_objs);
33  }
34  else
35  {
36  ERR("Writing of geometry failed, since it was not possible to determine"
37  " the required format from file extension.");
38  }
39 }
void writeGLIFileV4(const std::string &fname, const std::string &geo_name, const GeoLib::GEOObjects &geo)
Definition: OGSIOVer4.cpp:648

References ERR(), BaseLib::IO::XMLInterface::export_name, BaseLib::getFileExtension(), FileIO::Legacy::writeGLIFileV4(), BaseLib::IO::writeStringToFile(), and BaseLib::IO::Writer::writeToString().

Referenced by main(), and writeBCsAndGeometry().

Variable Documentation

◆ link_vars

const std::array<std::string, 6> FileIO::link_vars
Initial value:
= {"flow rate",
"flow depth",
"flow velocity",
"flow volume",
"fraction conduit/non-conduit",
"concentration of pollutant"}

Variables that always exist for links. There might be more.

Definition at line 55 of file SWMMInterface.cpp.

Referenced by FileIO::SwmmInterface::getArrayName().

◆ n_obj_params

std::array<std::size_t, 4> const FileIO::n_obj_params = {8, 6, 5, 15}

Number of base variables for the four object types (subcatchments/nodes/links/system).

Definition at line 82 of file SWMMInterface.cpp.

Referenced by FileIO::SwmmInterface::getArrayAtTimeStep(), FileIO::SwmmInterface::getArrayForObject(), FileIO::SwmmInterface::getArrayName(), and FileIO::SwmmInterface::getNumberOfParameters().

◆ node_vars

const std::array<std::string, 7> FileIO::node_vars
Initial value:
= {"water depth",
"hydraulic head",
"volume of stored water",
"lateral inflow",
"total inflow",
"flow lost to flooding",
"concentration of pollutant"}

Variables that always exist for nodes. There might be more.

Definition at line 46 of file SWMMInterface.cpp.

Referenced by FileIO::SwmmInterface::getArrayName().

◆ subcatchment_vars

const std::array<std::string, 9> FileIO::subcatchment_vars
Initial value:
= {
"rainfall",
"snow depth",
"evaporation loss",
"infiltration losses",
"runoff rate",
"groundwater outflow",
"groundwater head",
"moisture content",
"concentration of pollutant"}

Variables that always exist for subcatchments. There might be more.

Definition at line 34 of file SWMMInterface.cpp.

Referenced by FileIO::SwmmInterface::getArrayName().

◆ system_vars

const std::array<std::string, 15> FileIO::system_vars
Initial value:
= {
"air temperature",
"rainfall",
"snow depth",
"evaporation + infiltration loss",
"runoff flow",
"dry weather inflow",
"groundwater inflow",
"RDII inflow",
"direct inflow",
"total lateral inflow",
"flow lost to flooding",
"flow leaving through outfalls",
"volume of stored water",
"actual evaporation rate",
"potential evaporation rate"}

All variables that exist for the system.

Definition at line 63 of file SWMMInterface.cpp.

Referenced by FileIO::SwmmInterface::getArrayName().