OGS
createMeshElemPropertiesFromASCRaster.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include <tclap/CmdLine.h>
5
6#include <algorithm>
7#include <cmath>
8#include <memory>
9#include <numeric>
10
11#include "BaseLib/FileTools.h"
12#include "BaseLib/Logging.h"
13#include "BaseLib/MPI.h"
15#include "BaseLib/quicksort.h"
17#include "GeoLib/Raster.h"
18#include "InfoLib/GitInfo.h"
19#include "MathLib/MathTools.h"
24#include "MeshLib/Mesh.h"
25#include "MeshLib/MeshEnums.h"
28
29int main(int argc, char* argv[])
30{
31 TCLAP::CmdLine cmd(
32 "Generates properties for mesh elements of an input mesh deploying a "
33 "ASC raster file.\n\n"
34 "OpenGeoSys-6 software, version " +
36 ".\n"
37 "Copyright (c) 2012-2026, OpenGeoSys Community "
38 "(http://www.opengeosys.org)",
40
41 TCLAP::ValueArg<std::string> out_mesh_arg(
42 "o",
43 "out-mesh",
44 "Output (.vtu). The output mesh is stored to a file of this name",
45 false,
46 "",
47 "OUTPUT_FILE");
48 cmd.add(out_mesh_arg);
49
50 TCLAP::SwitchArg refinement_raster_output_arg(
51 "", "output-refined-raster",
52 "Write refined raster to an additional, new ASC file.");
53 cmd.add(refinement_raster_output_arg);
54
55 TCLAP::ValueArg<unsigned> refinement_arg(
56 "r",
57 "refine",
58 "refinement factor that raises the resolution of the raster data",
59 false,
60 1,
61 "REFINEMENT_FACTOR");
62 cmd.add(refinement_arg);
63
64 TCLAP::ValueArg<std::string> raster_arg(
65 "",
66 "raster-file",
67 "Input (.asc). The name of the input "
68 "ASC raster file",
69 true,
70 "",
71 "INPUT_FILE");
72 cmd.add(raster_arg);
73
74 TCLAP::ValueArg<std::string> property_arg(
75 "p",
76 "property-name",
77 "the name of the property the values are stored for",
78 true,
79 "",
80 "PROPERTY_NAME");
81 cmd.add(property_arg);
82
83 TCLAP::ValueArg<std::string> mesh_arg(
84 "m", "mesh", "Input (.vtu). The input mesh is read from this file",
85 true, "", "INPUT_FILE");
86 cmd.add(mesh_arg);
87
88 auto log_level_arg = BaseLib::makeLogLevelArg();
89 cmd.add(log_level_arg);
90 cmd.parse(argc, argv);
91
92 BaseLib::MPI::Setup mpi_setup(argc, argv);
93 BaseLib::initOGSLogger(log_level_arg.getValue());
94
95 // read mesh
96 std::unique_ptr<MeshLib::Mesh> dest_mesh(
97 MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
98
99 // read raster and if required manipulate it
100 auto raster = std::unique_ptr<GeoLib::Raster>(
102 raster_arg.getValue()));
103 GeoLib::RasterHeader header(raster->getHeader());
104 if (refinement_arg.getValue() > 1)
105 {
106 raster->refineRaster(refinement_arg.getValue());
107 if (refinement_raster_output_arg.getValue())
108 {
109 // write new asc file
110 std::string new_raster_fname(
111 BaseLib::dropFileExtension(raster_arg.getValue()));
112 new_raster_fname += "-" + std::to_string(header.n_rows) + "x" +
113 std::to_string(header.n_cols) + ".asc";
115 new_raster_fname);
116 }
117 }
118
119 std::unique_ptr<MeshLib::Mesh> src_mesh(
123 property_arg.getValue()));
124
125 // do the interpolation
127 *src_mesh, property_arg.getValue());
128 mesh_interpolation.setPropertiesForMesh(*dest_mesh);
129
130 if (!out_mesh_arg.getValue().empty())
131 {
132 MeshLib::IO::writeMeshToFile(*dest_mesh, out_mesh_arg.getValue());
133 }
134
135 return EXIT_SUCCESS;
136}
static void writeRasterAsASC(GeoLib::Raster const &raster, std::string const &file_name)
Writes an Esri asc-file.
static GeoLib::Raster * getRasterFromASCFile(std::string const &fname)
Reads an ArcGis ASC raster file.
static std::unique_ptr< MeshLib::Mesh > convert(GeoLib::Raster const &raster, MeshLib::MeshElemType elem_type, MeshLib::UseIntensityAs intensity_type, std::string const &array_name="Colour")
int main(int argc, char *argv[])
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
std::string dropFileExtension(std::string const &filename)
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)
Contains the relevant information when storing a geoscientific raster data.
Definition Raster.h:18
std::size_t n_cols
Definition Raster.h:19
std::size_t n_rows
Definition Raster.h:20