OGS
AssignRasterDataToMesh.cpp
Go to the documentation of this file.
1
10#include <tclap/CmdLine.h>
11
12#include <memory>
13#include <string>
14
15#include "BaseLib/Logging.h"
16#include "BaseLib/MPI.h"
19#include "GeoLib/Raster.h"
20#include "InfoLib/GitInfo.h"
23#include "MeshLib/Mesh.h"
25
26int main(int argc, char* argv[])
27{
28 TCLAP::CmdLine cmd(
29 "Assigns the pixel information of a raster file to a scalar array of a "
30 "specified 2D mesh. Data will be assigned to a node array by default. "
31 "Adding information to cell arrays is also possible, pixel values at "
32 "the centre of the cell will be used in this case. Note that large "
33 "differences in resolution between cell size of the mesh and pixel "
34 "size of the raster can give unexpected results. A no-data value will "
35 "be added in case of missing or transparent values.\n\n"
36 "OpenGeoSys-6 software, version " +
38 ".\n"
39 "Copyright (c) 2012-2025, OpenGeoSys Community "
40 "(http://www.opengeosys.org)",
42
43 TCLAP::ValueArg<double> nodata_arg(
44 "e", "nodata",
45 "The no data value used for missing values "
46 "(min = 0)",
47 false, 0, "NO_DATA");
48 cmd.add(nodata_arg);
49
50 TCLAP::SwitchArg set_cells_arg("c", "cell-array",
51 "Assigns raster data to cell array");
52 cmd.add(set_cells_arg);
53
54 TCLAP::SwitchArg set_nodes_arg(
55 "n", "node-array", "Assigns raster data to node array (default)");
56 cmd.add(set_nodes_arg);
57
58 TCLAP::ValueArg<std::string> array_name_arg(
59 "s", "scalar-name", "The name of the newly created scalar array.", true,
60 "", "SCALAR_NAME");
61 cmd.add(array_name_arg);
62 TCLAP::ValueArg<std::string> raster_arg(
63 "r", "raster", "Input (.asc). Name of the input raster file", true, "",
64 "INPUT_FILE");
65 cmd.add(raster_arg);
66
67 TCLAP::ValueArg<std::string> output_arg(
68 "o", "output", "Output (.vtu). Name of the output mesh file", true, "",
69 "OUTPUT_FILE");
70 cmd.add(output_arg);
71 TCLAP::ValueArg<std::string> input_arg(
72 "i", "input", "Input (.vtu). Name of the input mesh file", true, "",
73 "INPUT_FILE");
74 cmd.add(input_arg);
75 auto log_level_arg = BaseLib::makeLogLevelArg();
76 cmd.add(log_level_arg);
77 cmd.parse(argc, argv);
78
79 BaseLib::MPI::Setup mpi_setup(argc, argv);
80 BaseLib::initOGSLogger(log_level_arg.getValue());
81
82 bool const create_cell_array(set_cells_arg.isSet());
83 bool const create_node_array =
84 (create_cell_array) ? set_nodes_arg.isSet() : true;
85
86 std::string const& mesh_name = input_arg.getValue();
87 std::string const& output_name = output_arg.getValue();
88 std::string const& raster_name = raster_arg.getValue();
89
90 std::unique_ptr<MeshLib::Mesh> const mesh(
92 if (mesh->getDimension() > 2)
93 {
94 ERR("Method can not be applied to 3D meshes.");
95 return EXIT_FAILURE;
96 }
97
98 std::unique_ptr<GeoLib::Raster> const raster(
100
101 if (create_node_array)
102 {
104 *mesh, *raster, nodata_arg.getValue(), array_name_arg.getValue());
105
106 if (!assigned)
107 {
108 ERR("Error assigning raster data to scalar node array");
109 return EXIT_FAILURE;
110 }
111 INFO("Created node array {:s}", array_name_arg.getValue());
112 }
113
114 if (create_cell_array)
115 {
117 *mesh, *raster, nodata_arg.getValue(), array_name_arg.getValue());
118
119 if (!assigned)
120 {
121 ERR("Error assigning raster data to scalar cell array");
122 return EXIT_FAILURE;
123 }
124 INFO("Created cell array {:s}", array_name_arg.getValue());
125 }
126
127 MeshLib::IO::VtuInterface vtu(mesh.get());
128 vtu.writeToFile(output_name);
129 return EXIT_SUCCESS;
130}
Definition of the AsciiRasterInterface class.
int main(int argc, char *argv[])
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
Definition of the Mesh class.
Definition of the GeoLib::Raster class.
Implementation of the VtuInterface class.
static GeoLib::Raster * getRasterFromASCFile(std::string const &fname)
Reads an ArcGis ASC raster file.
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
bool projectToNodes(MeshLib::Mesh &mesh, GeoLib::Raster const &raster, double const default_replacement, std::string const &array_name)
bool projectToElements(MeshLib::Mesh &mesh, GeoLib::Raster const &raster, double const default_replacement, std::string const &array_name)
Definition of readMeshFromFile function.