OGS
AssignRasterDataToMesh.cpp File Reference

Detailed Description

Definition in file AssignRasterDataToMesh.cpp.

#include <memory>
#include <string>
#include <tclap/CmdLine.h>
#include "Applications/FileIO/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 "MeshLib/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 24 of file AssignRasterDataToMesh.cpp.

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

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