diff --git a/Applications/Utils/MeshGeoTools/AssignRasterDataToMesh.cpp b/Applications/Utils/MeshGeoTools/AssignRasterDataToMesh.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca4fb30fa01a9ced36d70f7493963d2b7e7d334c --- /dev/null +++ b/Applications/Utils/MeshGeoTools/AssignRasterDataToMesh.cpp @@ -0,0 +1,133 @@ +/** + * \file + * \copyright + * Copyright (c) 2012-2020, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + */ + +#include <memory> +#include <string> + +// ThirdParty +#include <tclap/CmdLine.h> + +#include "InfoLib/GitInfo.h" + +#include "GeoLib/Raster.h" +#include "MeshLib/Mesh.h" +#include "MeshLib/IO/readMeshFromFile.h" +//#include "MeshLib/MeshEnums.h" +#include "MeshLib/IO/VtkIO/VtuInterface.h" +#include "MeshLib/MeshEditing/RasterDataToMesh.h" +#include "Applications/FileIO/AsciiRasterInterface.h" + +int main(int argc, char *argv[]) +{ + TCLAP::CmdLine cmd( + "Assigns the pixel information of a raster file to a scalar array of a " + "specified 2D mesh. Data will be assigned to a node array by default. " + "Adding information to cell arrays is also possible, pixel values at " + "the centre of the cell will be used in this case. Note that large " + "differences in resolution between cell size of the mesh and pixel " + "size of the raster can give unexpected results. A no-data value will " + "be added in case of missing or transparent values.\n\n" + "OpenGeoSys-6 software, version " + + GitInfoLib::GitInfo::ogs_version + + ".\n" + "Copyright (c) 2012-2020, OpenGeoSys Community " + "(http://www.opengeosys.org)", + ' ', GitInfoLib::GitInfo::ogs_version); + + TCLAP::ValueArg<double> nodata_arg("e", "nodata", + "The no data value used for missing values", + false, 0, "a number"); + cmd.add(nodata_arg); + + TCLAP::SwitchArg set_cells_arg("c", "cell-array", + "Assigns raster data to cell array"); + cmd.add(set_cells_arg); + + TCLAP::SwitchArg set_nodes_arg("n", "node-array", + "Assigns raster data to node array (default)"); + cmd.add(set_nodes_arg); + + TCLAP::ValueArg<std::string> array_name_arg( + "s", "scalar", "The name of the newly created scalar array.", true, "", + "scalar array name"); + cmd.add(array_name_arg); + TCLAP::ValueArg<std::string> raster_arg("r", "raster", + "Name of the input raster (*.asc)", + true, "", "raster file name"); + cmd.add(raster_arg); + + TCLAP::ValueArg<std::string> output_arg("o", "output", + "Name of the output mesh (*.vtu)", + true, "", "output file name"); + cmd.add(output_arg); + TCLAP::ValueArg<std::string> input_arg("i", "input", + "Name of the input mesh (*.vtu)", + true, "", "input file name"); + cmd.add(input_arg); + cmd.parse(argc, argv); + + bool create_node_array(true); + bool create_cell_array(false); + if (set_cells_arg.isSet()) + { + create_cell_array = true; + create_node_array = false; + } + + if (set_nodes_arg.isSet()) + { + create_node_array = true; + } + + std::string const mesh_name = input_arg.getValue().c_str(); + std::string const output_name = output_arg.getValue().c_str(); + std::string const raster_name = raster_arg.getValue().c_str(); + + std::unique_ptr<MeshLib::Mesh> mesh( + MeshLib::IO::readMeshFromFile(mesh_name)); + if (mesh->getDimension() > 2) + { + ERR("Method can only be applied to 2D meshes."); + return EXIT_FAILURE; + } + + std::unique_ptr<GeoLib::Raster> const raster( + FileIO::AsciiRasterInterface::getRasterFromASCFile(raster_name)); + + bool assigned(false); + if (create_node_array) + { + assigned = MeshLib::RasterDataToMesh::projectToNodes( + *mesh, *raster, nodata_arg.getValue(), array_name_arg.getValue()); + + if (!assigned) + { + ERR("Error assigning raster data to scalar node array"); + return EXIT_FAILURE; + } + INFO("Created node array {:s}", array_name_arg.getValue()); + } + + if (create_cell_array) + { + assigned = MeshLib::RasterDataToMesh::projectToElements( + *mesh, *raster, nodata_arg.getValue(), array_name_arg.getValue()); + + if (!assigned) + { + ERR("Error assigning raster data to scalar cell array"); + return EXIT_FAILURE; + } + INFO("Created cell array {:s}", array_name_arg.getValue()); + } + + MeshLib::IO::VtuInterface vtu(mesh.get()); + vtu.writeToFile(output_name); + return EXIT_SUCCESS; +} diff --git a/Applications/Utils/MeshGeoTools/CMakeLists.txt b/Applications/Utils/MeshGeoTools/CMakeLists.txt index 32ace6f4721dfc256ea0b4fc449b42a79dbe4e6f..4f190a271576857bed8592b549a78a83545cb3e7 100644 --- a/Applications/Utils/MeshGeoTools/CMakeLists.txt +++ b/Applications/Utils/MeshGeoTools/CMakeLists.txt @@ -1,4 +1,5 @@ set(TOOLS + AssignRasterDataToMesh computeSurfaceNodeIDsInPolygonalRegion constructMeshesFromGeometry createIntermediateRasters