OGS
addDataToRaster.cpp
Go to the documentation of this file.
1
12#include <tclap/CmdLine.h>
13
14#ifdef USE_PETSC
15#include <mpi.h>
16#endif
17
18#include <algorithm>
19#include <cmath>
20#include <memory>
21#include <numbers>
22#include <numeric>
23
24#include "GeoLib/AABB.h"
26#include "GeoLib/Point.h"
27#include "GeoLib/Raster.h"
28#include "InfoLib/GitInfo.h"
29
31 GeoLib::AABB const& aabb)
32{
33 auto const sigma_x = (aabb.getMaxPoint() - aabb.getMinPoint())[0] / 3;
34 auto const sigma_y = (aabb.getMaxPoint() - aabb.getMinPoint())[1] / 3;
35
36 auto const mid_point = (aabb.getMaxPoint() + aabb.getMinPoint()) / 2;
37
38 return std::exp(
39 -0.5 * std::pow((point[0] - mid_point[0]), 2) / std::pow(sigma_x, 2) -
40 0.5 * std::pow((point[1] - mid_point[1]), 2) / std::pow(sigma_y, 2));
41}
42
43double computeSinXSinY(GeoLib::Point const& point, GeoLib::AABB const& aabb)
44{
45 auto const aabb_size = aabb.getMaxPoint() - aabb.getMinPoint();
46 auto const offset = aabb.getMinPoint();
47
48 return std::sin((point[0] - offset[0]) / aabb_size[0] * std::numbers::pi) *
49 std::sin((point[1] - offset[1]) / aabb_size[1] * std::numbers::pi);
50}
51
52double computeStepFunction(GeoLib::Point const& point, GeoLib::AABB const& aabb)
53{
54 auto const aabb_size = aabb.getMaxPoint() - aabb.getMinPoint();
55 auto const min = aabb.getMinPoint();
56
57 if ((point[0] - min[0]) / aabb_size[0] < 0.5 &&
58 (point[1] - min[1]) / aabb_size[1] < 0.5)
59 {
60 return 1;
61 }
62 if ((point[0] - min[0]) / aabb_size[0] >= 0.5 &&
63 (point[1] - min[1]) / aabb_size[1] >= 0.5)
64 {
65 return 1;
66 }
67 return 0;
68}
69
70int main(int argc, char* argv[])
71{
72 TCLAP::CmdLine cmd(
73 "Add values to raster.\n\n"
74 "OpenGeoSys-6 software, version " +
76 ".\n"
77 "Copyright (c) 2012-2024, OpenGeoSys Community "
78 "(http://www.opengeosys.org)",
80
81 TCLAP::ValueArg<std::string> out_raster_arg(
82 "o",
83 "output_raster",
84 "the output raster is stored to a file of this name",
85 true,
86 "",
87 "filename for raster output");
88 cmd.add(out_raster_arg);
89
90 TCLAP::ValueArg<double> scaling_arg(
91 "",
92 "scaling_value",
93 "value the function sin(x pi) sin(y pi) will be scaled with",
94 false,
95 1,
96 "double value");
97 cmd.add(scaling_arg);
98
99 TCLAP::ValueArg<double> offset_arg(
100 "",
101 "offset_value",
102 "constant added to the function 'scaling * sin(x pi) * sin(y pi)'",
103 false,
104 0,
105 "double value");
106 cmd.add(offset_arg);
107
108 TCLAP::ValueArg<double> ll_x_arg(
109 "",
110 "ll_x",
111 "x coordinate of lower left point of axis aligned rectangular region",
112 false,
113 0,
114 "double value");
115 cmd.add(ll_x_arg);
116 TCLAP::ValueArg<double> ll_y_arg(
117 "",
118 "ll_y",
119 "y coordinate of lower left point of axis aligned rectangular region",
120 false,
121 0,
122 "double value");
123 cmd.add(ll_y_arg);
124 TCLAP::ValueArg<double> ur_x_arg("",
125 "ur_x",
126 "x coordinate of the upper right point of "
127 "axis aligned rectangular region",
128 false,
129 0,
130 "double value");
131 cmd.add(ur_x_arg);
132 TCLAP::ValueArg<double> ur_y_arg("",
133 "ur_y",
134 "y coordinate of the upper right point of "
135 "axis aligned rectangular region",
136 false,
137 0,
138 "double value");
139
140 cmd.add(ur_y_arg);
141 std::vector<std::string> allowed_functions_vector{"sinxsiny", "exp",
142 "step"};
143 TCLAP::ValuesConstraint<std::string> allowed_functions(
144 allowed_functions_vector);
145 TCLAP::ValueArg<std::string> function_arg(
146 "f", "function", "Name of the function used to modify the raster", true,
147 "", &allowed_functions);
148 cmd.add(function_arg);
149 TCLAP::ValueArg<std::string> input_arg("i", "input",
150 "Name of the input raster (*.asc)",
151 true, "", "input file name");
152 cmd.add(input_arg);
153
154 cmd.parse(argc, argv);
155
156#ifdef USE_PETSC
157 MPI_Init(&argc, &argv);
158#endif
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#ifdef USE_PETSC
214 MPI_Finalize();
215#endif
216 return EXIT_SUCCESS;
217}
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
GITINFOLIB_EXPORT const std::string ogs_version