OGS
addDataToRaster.cpp
Go to the documentation of this file.
1
11#include <tclap/CmdLine.h>
12
13#include <algorithm>
14#include <cmath>
15#include <memory>
16#include <numbers>
17#include <numeric>
18
19#include "BaseLib/Logging.h"
20#include "BaseLib/MPI.h"
22#include "GeoLib/AABB.h"
24#include "GeoLib/Point.h"
25#include "GeoLib/Raster.h"
26#include "InfoLib/GitInfo.h"
27
29 GeoLib::AABB const& aabb)
30{
31 auto const sigma_x = (aabb.getMaxPoint() - aabb.getMinPoint())[0] / 3;
32 auto const sigma_y = (aabb.getMaxPoint() - aabb.getMinPoint())[1] / 3;
33
34 auto const mid_point = (aabb.getMaxPoint() + aabb.getMinPoint()) / 2;
35
36 return std::exp(
37 -0.5 * std::pow((point[0] - mid_point[0]), 2) / std::pow(sigma_x, 2) -
38 0.5 * std::pow((point[1] - mid_point[1]), 2) / std::pow(sigma_y, 2));
39}
40
41double computeSinXSinY(GeoLib::Point const& point, GeoLib::AABB const& aabb)
42{
43 auto const aabb_size = aabb.getMaxPoint() - aabb.getMinPoint();
44 auto const offset = aabb.getMinPoint();
45
46 return std::sin((point[0] - offset[0]) / aabb_size[0] * std::numbers::pi) *
47 std::sin((point[1] - offset[1]) / aabb_size[1] * std::numbers::pi);
48}
49
50double computeStepFunction(GeoLib::Point const& point, GeoLib::AABB const& aabb)
51{
52 auto const aabb_size = aabb.getMaxPoint() - aabb.getMinPoint();
53 auto const min = aabb.getMinPoint();
54
55 if ((point[0] - min[0]) / aabb_size[0] < 0.5 &&
56 (point[1] - min[1]) / aabb_size[1] < 0.5)
57 {
58 return 1;
59 }
60 if ((point[0] - min[0]) / aabb_size[0] >= 0.5 &&
61 (point[1] - min[1]) / aabb_size[1] >= 0.5)
62 {
63 return 1;
64 }
65 return 0;
66}
67
68int main(int argc, char* argv[])
69{
70 TCLAP::CmdLine cmd(
71 "Add values to raster.\n\n"
72 "OpenGeoSys-6 software, version " +
74 ".\n"
75 "Copyright (c) 2012-2025, OpenGeoSys Community "
76 "(http://www.opengeosys.org)",
78
79 TCLAP::ValueArg<std::string> out_raster_arg(
80 "o",
81 "output_raster",
82 "Output (.asc). The output raster is stored to a file of this name",
83 true,
84 "",
85 "OUTPUT_FILE");
86 cmd.add(out_raster_arg);
87
88 TCLAP::ValueArg<double> scaling_arg(
89 "",
90 "scaling_value",
91 "value the function sin(x pi) sin(y pi) will be scaled with",
92 false,
93 1,
94 "SCALING");
95 cmd.add(scaling_arg);
96
97 TCLAP::ValueArg<double> offset_arg(
98 "",
99 "offset_value",
100 "constant added to the function 'scaling * sin(x pi) * sin(y pi)'",
101 false,
102 0,
103 "OFFSET");
104 cmd.add(offset_arg);
105
106 TCLAP::ValueArg<double> ll_x_arg(
107 "",
108 "ll_x",
109 "x coordinate of lower left point of axis aligned rectangular region",
110 false,
111 0,
112 "LL_X");
113 cmd.add(ll_x_arg);
114 TCLAP::ValueArg<double> ll_y_arg(
115 "",
116 "ll_y",
117 "y coordinate of lower left point of axis aligned rectangular region",
118 false,
119 0,
120 "LL_Y");
121 cmd.add(ll_y_arg);
122 TCLAP::ValueArg<double> ur_x_arg("",
123 "ur_x",
124 "x coordinate of the upper right point of "
125 "axis aligned rectangular region",
126 false,
127 0,
128 "UR_X");
129 cmd.add(ur_x_arg);
130 TCLAP::ValueArg<double> ur_y_arg("",
131 "ur_y",
132 "y coordinate of the upper right point of "
133 "axis aligned rectangular region",
134 false,
135 0,
136 "UR_Y");
137
138 cmd.add(ur_y_arg);
139 std::vector<std::string> allowed_functions_vector{"sinxsiny", "exp",
140 "step"};
141 TCLAP::ValuesConstraint<std::string> allowed_functions(
142 allowed_functions_vector);
143 TCLAP::ValueArg<std::string> function_arg(
144 "f", "function", "Name of the function used to modify the raster", true,
145 "", &allowed_functions);
146 cmd.add(function_arg);
147 TCLAP::ValueArg<std::string> input_arg("i", "input",
148 "Input (.asc). "
149 "Name of the input raster file",
150 true, "", "INPUT_FILE");
151 cmd.add(input_arg);
152
153 auto log_level_arg = BaseLib::makeLogLevelArg();
154 cmd.add(log_level_arg);
155 cmd.parse(argc, argv);
156
157 BaseLib::MPI::Setup mpi_setup(argc, argv);
158 BaseLib::initOGSLogger(log_level_arg.getValue());
159
160 std::array input_points = {
161 GeoLib::Point{{ll_x_arg.getValue(), ll_y_arg.getValue(), 0}},
162 GeoLib::Point{{ur_x_arg.getValue(), ur_y_arg.getValue(), 0}}};
163 GeoLib::AABB const aabb{std::begin(input_points), std::end(input_points)};
164
165 auto const s = scaling_arg.getValue();
166 auto const offset = offset_arg.getValue();
167
168 std::unique_ptr<GeoLib::Raster> const raster(
170 input_arg.getValue()));
171 auto const& header = raster->getHeader();
172 auto const& origin = header.origin;
173
174 auto function_selector = [](std::string const& function_string)
175 -> std::function<double(GeoLib::Point const& p,
176 GeoLib::AABB const& aabb)>
177 {
178 if (function_string == "sinxsiny")
179 {
180 return computeSinXSinY;
181 }
182 if (function_string == "exp")
183 {
185 }
186 if (function_string == "step")
187 {
188 return computeStepFunction;
189 }
190 OGS_FATAL("Function '{}' isn't implemented.", function_string);
191 };
192 std::function<double(GeoLib::Point const& p, GeoLib::AABB const& aabb)>
193 computeFunctionValue = function_selector(function_arg.getValue());
194
195 for (std::size_t r = 0; r < header.n_rows; r++)
196 {
197 for (std::size_t c = 0; c < header.n_cols; c++)
198 {
199 GeoLib::Point const p{{origin[0] + header.cell_size * (c + 0.5),
200 origin[1] + header.cell_size * (r + 0.5),
201 0.0}};
202 if (!aabb.containsPoint(p, std::numeric_limits<double>::epsilon()))
203 {
204 continue;
205 }
206
207 (*raster)(r, c) += offset + s * computeFunctionValue(p, aabb);
208 }
209 }
210
212 out_raster_arg.getValue());
213 return EXIT_SUCCESS;
214}
Definition of the AABB class.
Definition of the AsciiRasterInterface class.
#define OGS_FATAL(...)
Definition Error.h:26
Definition of the Point class.
Git information.
Definition of the GeoLib::Raster class.
int main(int argc, char *argv[])
double computeSinXSinY(GeoLib::Point const &point, GeoLib::AABB const &aabb)
double compute2DGaussBellCurveValues(GeoLib::Point const &point, GeoLib::AABB const &aabb)
double computeStepFunction(GeoLib::Point const &point, GeoLib::AABB const &aabb)
static void writeRasterAsASC(GeoLib::Raster const &raster, std::string const &file_name)
Writes an Esri asc-file.
static GeoLib::Raster * getRasterFromASCFile(std::string const &fname)
Reads an ArcGis ASC raster file.
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
Definition AABB.h:56
Eigen::Vector3d const & getMaxPoint() const
Definition AABB.h:187
bool containsPoint(T const &pnt, double eps) const
Definition AABB.h:143
Eigen::Vector3d const & getMinPoint() const
Definition AABB.h:180
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
GITINFOLIB_EXPORT const std::string ogs_version