OGS
AssignRasterDataToMesh.cpp File Reference
#include <tclap/CmdLine.h>
#include <memory>
#include <string>
#include "BaseLib/Logging.h"
#include "BaseLib/MPI.h"
#include "BaseLib/TCLAPArguments.h"
#include "GeoLib/IO/AsciiRasterInterface.h"
#include "GeoLib/Raster.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/VtkIO/VtuInterface.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/Mesh.h"
#include "MeshToolsLib/MeshEditing/RasterDataToMesh.h"
Include dependency graph for AssignRasterDataToMesh.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 20 of file AssignRasterDataToMesh.cpp.

21{
22 TCLAP::CmdLine cmd(
23 "Assigns the pixel information of a raster file to a scalar array of a "
24 "specified 2D mesh. Data will be assigned to a node array by default. "
25 "Adding information to cell arrays is also possible, pixel values at "
26 "the centre of the cell will be used in this case. Note that large "
27 "differences in resolution between cell size of the mesh and pixel "
28 "size of the raster can give unexpected results. A no-data value will "
29 "be added in case of missing or transparent values.\n\n"
30 "OpenGeoSys-6 software, version " +
32 ".\n"
33 "Copyright (c) 2012-2026, OpenGeoSys Community "
34 "(http://www.opengeosys.org)",
36
37 TCLAP::ValueArg<double> nodata_arg(
38 "e", "nodata",
39 "The no data value used for missing values "
40 "(min = 0)",
41 false, 0, "NO_DATA");
42 cmd.add(nodata_arg);
43
44 TCLAP::SwitchArg set_cells_arg("c", "cell-array",
45 "Assigns raster data to cell array");
46 cmd.add(set_cells_arg);
47
48 TCLAP::SwitchArg set_nodes_arg(
49 "n", "node-array", "Assigns raster data to node array (default)");
50 cmd.add(set_nodes_arg);
51
52 TCLAP::ValueArg<std::string> array_name_arg(
53 "s", "scalar-name", "The name of the newly created scalar array.", true,
54 "", "SCALAR_NAME");
55 cmd.add(array_name_arg);
56 TCLAP::ValueArg<std::string> raster_arg(
57 "r", "raster", "Input (.asc). Name of the input raster file", true, "",
58 "INPUT_FILE");
59 cmd.add(raster_arg);
60
61 TCLAP::ValueArg<std::string> output_arg(
62 "o", "output", "Output (.vtu). Name of the output mesh file", true, "",
63 "OUTPUT_FILE");
64 cmd.add(output_arg);
65 TCLAP::ValueArg<std::string> input_arg(
66 "i", "input", "Input (.vtu). Name of the input mesh file", true, "",
67 "INPUT_FILE");
68 cmd.add(input_arg);
69 auto log_level_arg = BaseLib::makeLogLevelArg();
70 cmd.add(log_level_arg);
71 cmd.parse(argc, argv);
72
73 BaseLib::MPI::Setup mpi_setup(argc, argv);
74 BaseLib::initOGSLogger(log_level_arg.getValue());
75
76 bool const create_cell_array(set_cells_arg.isSet());
77 bool const create_node_array =
78 (create_cell_array) ? set_nodes_arg.isSet() : true;
79
80 std::string const& mesh_name = input_arg.getValue();
81 std::string const& output_name = output_arg.getValue();
82 std::string const& raster_name = raster_arg.getValue();
83
84 std::unique_ptr<MeshLib::Mesh> const mesh(
86 if (mesh->getDimension() > 2)
87 {
88 ERR("Method can not be applied to 3D meshes.");
89 return EXIT_FAILURE;
90 }
91
92 std::unique_ptr<GeoLib::Raster> const raster(
94
95 if (create_node_array)
96 {
98 *mesh, *raster, nodata_arg.getValue(), array_name_arg.getValue());
99
100 if (!assigned)
101 {
102 ERR("Error assigning raster data to scalar node array");
103 return EXIT_FAILURE;
104 }
105 INFO("Created node array {:s}", array_name_arg.getValue());
106 }
107
108 if (create_cell_array)
109 {
111 *mesh, *raster, nodata_arg.getValue(), array_name_arg.getValue());
112
113 if (!assigned)
114 {
115 ERR("Error assigning raster data to scalar cell array");
116 return EXIT_FAILURE;
117 }
118 INFO("Created cell array {:s}", array_name_arg.getValue());
119 }
120
121 MeshLib::IO::VtuInterface vtu(mesh.get());
122 vtu.writeToFile(output_name);
123 return EXIT_SUCCESS;
124}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
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....
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
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)

References ERR(), FileIO::AsciiRasterInterface::getRasterFromASCFile(), INFO(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), GitInfoLib::GitInfo::ogs_version, MeshToolsLib::RasterDataToMesh::projectToElements(), MeshToolsLib::RasterDataToMesh::projectToNodes(), MeshLib::IO::readMeshFromFile(), and MeshLib::IO::VtuInterface::writeToFile().