OGS
createIntermediateRasters.cpp File Reference

Detailed Description

Definition in file createIntermediateRasters.cpp.

#include <tclap/CmdLine.h>
#include <memory>
#include <string>
#include "BaseLib/FileTools.h"
#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 dependency graph for createIntermediateRasters.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 23 of file createIntermediateRasters.cpp.

24{
25 TCLAP::CmdLine cmd(
26 "Takes two DEMs located at the exact same spatial position (but at "
27 "different elevation) and calculates n raster DEMs located at "
28 "equidistant intervals between them (i.e. for n=1, one new raster "
29 "located precisely in the middle will be created).\n\n"
30 "OpenGeoSys-6 software, version " +
32 ".\n"
33 "Copyright (c) 2012-2025, OpenGeoSys Community "
34 "(http://www.opengeosys.org)",
36 TCLAP::ValueArg<std::size_t> number_arg(
37 "n", "number",
38 "number of rasters to be calculated, "
39 "(min = 0)",
40 false, 1, "int");
41 cmd.add(number_arg);
42 TCLAP::ValueArg<std::string> output_arg("o", "output-file",
43 "Output (.asc). Raster output file",
44 true, "", "OUTPUT_FILE");
45 cmd.add(output_arg);
46 TCLAP::ValueArg<std::string> input2_arg(
47 "", "file2", "Input (.asc). Second DEM-raster input file", true, "",
48 "INPUT_FILE");
49 cmd.add(input2_arg);
50 TCLAP::ValueArg<std::string> input1_arg(
51 "", "file1", "Input (.asc) First DEM-raster input file", true, "",
52 "INPUT_FILE");
53 cmd.add(input1_arg);
54 auto log_level_arg = BaseLib::makeLogLevelArg();
55 cmd.add(log_level_arg);
56
57 cmd.parse(argc, argv);
58
59 BaseLib::MPI::Setup mpi_setup(argc, argv);
60 BaseLib::initOGSLogger(log_level_arg.getValue());
61
62 std::unique_ptr<GeoLib::Raster> dem1(
63 FileIO::AsciiRasterInterface::readRaster(input1_arg.getValue()));
64 std::unique_ptr<GeoLib::Raster> dem2(
65 FileIO::AsciiRasterInterface::readRaster(input2_arg.getValue()));
66
67 if (dem1 == nullptr || dem2 == nullptr)
68 {
69 return 1;
70 }
71
72 GeoLib::RasterHeader const& h1 = dem1->getHeader();
73 GeoLib::RasterHeader const& h2 = dem2->getHeader();
74
75 bool errors_found(false);
76 if (std::abs(h1.origin[0] - h2.origin[0]) > (h1.cell_size / 100.0))
77 {
78 ERR("Origin x-coordinate is not the same in both raster files.\n");
79 errors_found = true;
80 }
81 if (std::abs(h1.origin[1] - h2.origin[1]) > (h1.cell_size / 100.0))
82 {
83 ERR("Origin y-coordinate is not the same in both raster files.\n");
84 errors_found = true;
85 }
86 if (h1.cell_size != h2.cell_size)
87 {
88 ERR("Cellsize is not the same in both raster files.\n");
89 errors_found = true;
90 }
91 if (h1.n_cols != h2.n_cols)
92 {
93 ERR("Raster width is not the same in both raster files.\n");
94 errors_found = true;
95 }
96 if (h1.n_rows != h2.n_rows)
97 {
98 ERR("Raster height is not the same in both raster files.\n");
99 errors_found = true;
100 }
101
102 if (errors_found)
103 {
104 return 2;
105 }
106
107 std::size_t const n = number_arg.getValue();
108 std::vector<std::vector<double>> raster;
109 for (std::size_t i = 0; i < n; ++i)
110 {
111 std::vector<double> r;
112 r.reserve(h1.n_cols * h1.n_rows);
113 raster.push_back(r);
114 }
115
116 auto it2 = dem2->begin();
117 for (auto it1 = dem1->begin(); it1 != dem1->end(); ++it1)
118 {
119 if (it2 == dem2->end())
120 {
121 ERR("Error: File 2 is shorter than File 1.");
122 return 3;
123 }
124 if (*it1 == h1.no_data || *it2 == h2.no_data)
125 {
126 for (std::size_t i = 0; i < n; ++i)
127 {
128 raster[i].push_back(h1.no_data);
129 }
130 }
131 else
132 {
133 double const min = std::min(*it1, *it2);
134 double const max = std::max(*it1, *it2);
135 double const step = (max - min) / static_cast<double>(n + 1);
136 for (std::size_t i = 0; i < n; ++i)
137 {
138 raster[i].push_back(max - ((i + 1) * step));
139 }
140 }
141 it2++;
142 }
143 if (it2 != dem2->end())
144 {
145 ERR("Error: File 1 is shorter than File 2.");
146 return 3;
147 }
148
149 std::string const filename = output_arg.getValue();
150 for (std::size_t i = 0; i < n; ++i)
151 {
152 std::string const basename = BaseLib::dropFileExtension(filename);
153 std::string const ext = BaseLib::getFileExtension(filename);
154
155 GeoLib::Raster r(h1, raster[i].begin(), raster[i].end());
157 r, basename + std::to_string(i) + ext);
158 INFO("Layer {:d} written.", i + 1);
159 }
160 return EXIT_SUCCESS;
161}
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
static void writeRasterAsASC(GeoLib::Raster const &raster, std::string const &file_name)
Writes an Esri asc-file.
static GeoLib::Raster * readRaster(std::string const &fname)
Class Raster is used for managing raster data.
Definition Raster.h:49
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
std::string getFileExtension(const std::string &path)
std::string dropFileExtension(std::string const &filename)
GITINFOLIB_EXPORT const std::string ogs_version
Contains the relevant information when storing a geoscientific raster data.
Definition Raster.h:28
MathLib::Point3d origin
Definition Raster.h:32
std::size_t n_cols
Definition Raster.h:29
std::size_t n_rows
Definition Raster.h:30

References GeoLib::RasterHeader::cell_size, BaseLib::dropFileExtension(), ERR(), BaseLib::getFileExtension(), INFO(), BaseLib::initOGSLogger(), BaseLib::makeLogLevelArg(), GeoLib::RasterHeader::n_cols, GeoLib::RasterHeader::n_rows, GeoLib::RasterHeader::no_data, GitInfoLib::GitInfo::ogs_version, GeoLib::RasterHeader::origin, FileIO::AsciiRasterInterface::readRaster(), and FileIO::AsciiRasterInterface::writeRasterAsASC().