OGS
createIntermediateRasters.cpp
Go to the documentation of this file.
1 
10 #include <tclap/CmdLine.h>
11 
12 #include <memory>
13 #include <string>
14 
16 #include "BaseLib/FileTools.h"
17 #include "GeoLib/Raster.h"
18 #include "InfoLib/GitInfo.h"
19 
20 int main(int argc, char* argv[])
21 {
22  TCLAP::CmdLine cmd(
23  "Takes two DEMs located at the exact same spatial position (but at "
24  "different elevation) and calculates n raster DEMs located at "
25  "equidistant intervals between them (i.e. for n=1, one new raster "
26  "located precisely in the middle will be created).\n\n"
27  "OpenGeoSys-6 software, version " +
29  ".\n"
30  "Copyright (c) 2012-2021, OpenGeoSys Community "
31  "(http://www.opengeosys.org)",
33  TCLAP::ValueArg<std::size_t> number_arg(
34  "n", "number", "number of rasters to be calculated", false, 1, "int");
35  cmd.add(number_arg);
36  TCLAP::ValueArg<std::string> output_arg("o", "output-file",
37  "Raster output file (*.asc)", true,
38  "", "output.asc");
39  cmd.add(output_arg);
40  TCLAP::ValueArg<std::string> input2_arg(
41  "", "file2", "Second DEM-raster file", true, "", "file2.asc");
42  cmd.add(input2_arg);
43  TCLAP::ValueArg<std::string> input1_arg(
44  "", "file1", "First DEM-raster file", true, "", "file1.asc");
45  cmd.add(input1_arg);
46 
47  cmd.parse(argc, argv);
48 
49  std::unique_ptr<GeoLib::Raster> dem1(
50  FileIO::AsciiRasterInterface::readRaster(input1_arg.getValue()));
51  std::unique_ptr<GeoLib::Raster> dem2(
52  FileIO::AsciiRasterInterface::readRaster(input2_arg.getValue()));
53 
54  if (dem1 == nullptr || dem2 == nullptr)
55  {
56  return 1;
57  }
58 
59  GeoLib::RasterHeader const& h1 = dem1->getHeader();
60  GeoLib::RasterHeader const& h2 = dem2->getHeader();
61 
62  bool errors_found(false);
63  if (h1.origin[0] != h2.origin[0])
64  {
65  ERR("Origin x-coordinate is not the same in both raster files.\n");
66  errors_found = true;
67  }
68  if (h1.origin[1] != h2.origin[1])
69  {
70  ERR("Origin y-coordinate is not the same in both raster files.\n");
71  errors_found = true;
72  }
73  if (h1.cell_size != h2.cell_size)
74  {
75  ERR("Cellsize is not the same in both raster files.\n");
76  errors_found = true;
77  }
78  if (h1.n_cols != h2.n_cols)
79  {
80  ERR("Raster width is not the same in both raster files.\n");
81  errors_found = true;
82  }
83  if (h1.n_rows != h2.n_rows)
84  {
85  ERR("Raster height is not the same in both raster files.\n");
86  errors_found = true;
87  }
88 
89  if (errors_found)
90  {
91  return 2;
92  }
93 
94  std::size_t const n = number_arg.getValue();
95  std::vector<std::vector<double>> raster;
96  for (std::size_t i = 0; i < n; ++i)
97  {
98  std::vector<double> r;
99  r.reserve(h1.n_cols * h1.n_rows);
100  raster.push_back(r);
101  }
102 
103  auto it2 = dem2->begin();
104  for (auto it1 = dem1->begin(); it1 != dem1->end(); ++it1)
105  {
106  if (it2 == dem2->end())
107  {
108  ERR("Error: File 2 is shorter than File 1.");
109  return 3;
110  }
111  if (*it1 == h1.no_data || *it2 == h2.no_data)
112  {
113  for (std::size_t i = 0; i < n; ++i)
114  {
115  raster[i].push_back(h1.no_data);
116  }
117  }
118  else
119  {
120  double const min = std::min(*it1, *it2);
121  double const max = std::max(*it1, *it2);
122  double const step = (max - min) / static_cast<double>(n + 1);
123  for (std::size_t i = 0; i < n; ++i)
124  {
125  raster[i].push_back(max - ((i + 1) * step));
126  }
127  }
128  it2++;
129  }
130  if (it2 != dem2->end())
131  {
132  ERR("Error: File 1 is shorter than File 2.");
133  return 3;
134  }
135 
136  std::string const filename = output_arg.getValue();
137  for (std::size_t i = 0; i < n; ++i)
138  {
139  std::string const basename = BaseLib::dropFileExtension(filename);
140  std::string const ext = BaseLib::getFileExtension(filename);
141 
142  GeoLib::Raster r(h1, raster[i].begin(), raster[i].end());
144  r, basename + std::to_string(i) + ext);
145  INFO("Layer {:d} written.", i + 1);
146  }
147  return EXIT_SUCCESS;
148 }
Definition of the AsciiRasterInterface class.
Filename manipulation routines.
Git information.
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
Definition of the GeoLib::Raster class.
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)
Reads raster file by detecting type based on extension and then calling the appropriate method.
Class Raster is used for managing raster data.
Definition: Raster.h:42
int main(int argc, char *argv[])
std::string getFileExtension(const std::string &path)
Definition: FileTools.cpp:186
std::string dropFileExtension(std::string const &filename)
Definition: FileTools.cpp:169
GITINFOLIB_EXPORT const std::string ogs_version
static const double r
Contains the relevant information when storing a geoscientific raster data.
Definition: Raster.h:25
double cell_size
Definition: Raster.h:30
MathLib::Point3d origin
Definition: Raster.h:29
std::size_t n_cols
Definition: Raster.h:26
std::size_t n_rows
Definition: Raster.h:27