OGS
AssignRasterDataToMesh.cpp
Go to the documentation of this file.
1
10#include <memory>
11#include <string>
12
13// ThirdParty
14#include <tclap/CmdLine.h>
15
16#ifdef USE_PETSC
17#include <mpi.h>
18#endif
19
21#include "GeoLib/Raster.h"
22#include "InfoLib/GitInfo.h"
25#include "MeshLib/Mesh.h"
27
28int main(int argc, char* argv[])
29{
30 TCLAP::CmdLine cmd(
31 "Assigns the pixel information of a raster file to a scalar array of a "
32 "specified 2D mesh. Data will be assigned to a node array by default. "
33 "Adding information to cell arrays is also possible, pixel values at "
34 "the centre of the cell will be used in this case. Note that large "
35 "differences in resolution between cell size of the mesh and pixel "
36 "size of the raster can give unexpected results. A no-data value will "
37 "be added in case of missing or transparent values.\n\n"
38 "OpenGeoSys-6 software, version " +
40 ".\n"
41 "Copyright (c) 2012-2024, OpenGeoSys Community "
42 "(http://www.opengeosys.org)",
44
45 TCLAP::ValueArg<double> nodata_arg(
46 "e", "nodata", "The no data value used for missing values", false, 0,
47 "a number");
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 array name");
61 cmd.add(array_name_arg);
62 TCLAP::ValueArg<std::string> raster_arg("r", "raster",
63 "Name of the input raster (*.asc)",
64 true, "", "raster file name");
65 cmd.add(raster_arg);
66
67 TCLAP::ValueArg<std::string> output_arg("o", "output",
68 "Name of the output mesh (*.vtu)",
69 true, "", "output file name");
70 cmd.add(output_arg);
71 TCLAP::ValueArg<std::string> input_arg("i", "input",
72 "Name of the input mesh (*.vtu)",
73 true, "", "input file name");
74 cmd.add(input_arg);
75 cmd.parse(argc, argv);
76
77#ifdef USE_PETSC
78 MPI_Init(&argc, &argv);
79#endif
80
81 bool const create_cell_array(set_cells_arg.isSet());
82 bool const create_node_array =
83 (create_cell_array) ? set_nodes_arg.isSet() : true;
84
85 std::string const& mesh_name = input_arg.getValue();
86 std::string const& output_name = output_arg.getValue();
87 std::string const& raster_name = raster_arg.getValue();
88
89 std::unique_ptr<MeshLib::Mesh> const mesh(
91 if (mesh->getDimension() > 2)
92 {
93 ERR("Method can not be applied to 3D meshes.");
94#ifdef USE_PETSC
95 MPI_Finalize();
96#endif
97 return EXIT_FAILURE;
98 }
99
100 std::unique_ptr<GeoLib::Raster> const raster(
102
103 if (create_node_array)
104 {
106 *mesh, *raster, nodata_arg.getValue(), array_name_arg.getValue());
107
108 if (!assigned)
109 {
110 ERR("Error assigning raster data to scalar node array");
111#ifdef USE_PETSC
112 MPI_Finalize();
113#endif
114 return EXIT_FAILURE;
115 }
116 INFO("Created node array {:s}", array_name_arg.getValue());
117 }
118
119 if (create_cell_array)
120 {
122 *mesh, *raster, nodata_arg.getValue(), array_name_arg.getValue());
123
124 if (!assigned)
125 {
126 ERR("Error assigning raster data to scalar cell array");
127#ifdef USE_PETSC
128 MPI_Finalize();
129#endif
130 return EXIT_FAILURE;
131 }
132 INFO("Created cell array {:s}", array_name_arg.getValue());
133 }
134
135 MeshLib::IO::VtuInterface vtu(mesh.get());
136 vtu.writeToFile(output_name);
137#ifdef USE_PETSC
138 MPI_Finalize();
139#endif
140 return EXIT_SUCCESS;
141}
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:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
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)
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.