19 std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
46static std::optional<GeoLib::RasterHeader>
readASCHeader(std::ifstream& in)
57 header.
n_cols = atoi(value.c_str());
68 header.
n_rows = atoi(value.c_str());
78 if (tag ==
"xllcorner" || tag ==
"xllcenter")
88 if (tag ==
"yllcorner" || tag ==
"yllcenter")
99 if (tag ==
"cellsize")
109 if (tag ==
"NODATA_value" || tag ==
"nodata_value")
122 std::string
const& fname)
124 std::ifstream in(fname.c_str());
128 WARN(
"Raster::getRasterFromASCFile(): Could not open file {:s}.",
137 "Raster::getRasterFromASCFile(): Could not read header of file "
143 std::vector<double> values(header->n_cols * header->n_rows);
145 for (std::size_t j(0); j < header->n_rows; ++j)
147 const std::size_t idx((header->n_rows - j - 1) * header->n_cols);
148 for (std::size_t i(0); i < header->n_cols; ++i)
159static std::optional<std::tuple<GeoLib::RasterHeader, double, double>>
168 ERR(
"Error in readSurferHeader() - No Surfer file.");
183 if (ceil((max - min) /
static_cast<double>(header.
n_rows)) ==
190 ERR(
"Error in readSurferHeader() - Anisotropic cellsize detected.");
197 return {{header, min, max}};
201 std::string
const& fname)
203 std::ifstream in(fname.c_str());
207 ERR(
"Raster::getRasterFromSurferFile() - Could not open file {:s}",
213 if (!optional_header)
215 ERR(
"Raster::getRasterFromASCFile() - could not read header of file "
221 auto const [header, min, max] = *optional_header;
222 std::vector<double> values(header.n_cols * header.n_rows);
224 for (std::size_t j(0); j < header.n_rows; ++j)
226 const std::size_t idx(j * header.n_cols);
227 for (std::size_t i(0); i < header.n_cols; ++i)
230 values[idx + i] = (val > max || val < min) ? header.no_data : val;
239 std::vector<std::string> lines;
240 std::string line(
"");
241 while (std::getline(in, line))
243 lines.emplace_back(line);
250 std::array<double, 3> coords = {0, 0, 0};
251 if (
auto const n = std::sscanf(line.c_str(),
"%lf %lf %lf", &coords[0],
252 &coords[1], &coords[2]);
255 ERR(
"Raster::readXyzCoordinates() - Read {:d} doubles out of 3 "
256 "expected from the following line:\n{:s}",
265 double x_min = std::numeric_limits<double>::max();
266 double x_max = -std::numeric_limits<double>::max();
267 double y_min = std::numeric_limits<double>::max();
268 double y_max = -std::numeric_limits<double>::max();
269 double cellsize = std::numeric_limits<double>::max();
270 std::optional<std::array<double, 3>> coords;
272 std::size_t
const n_lines(lines.size());
273 for (std::size_t i = 0; i < n_lines; ++i)
276 if (coords == std::nullopt)
282 double const diff = (*coords)[0] - x_min;
285 cellsize = std::min(cellsize, diff);
287 x_min = std::min((*coords)[0], x_min);
288 x_max = std::max((*coords)[0], x_max);
289 y_min = std::min((*coords)[1], y_min);
290 y_max = std::max((*coords)[1], y_max);
296 header.
n_cols =
static_cast<std::size_t
>(((x_max - x_min) / cellsize) + 1);
297 header.
n_rows =
static_cast<std::size_t
>(((y_max - y_min) / cellsize) + 1);
305 std::string
const& fname)
307 std::ifstream in(fname.c_str());
310 ERR(
"Raster::getRasterFromXyzFile() - Could not open file {:s}", fname);
314 auto const string_lines =
readFile(in);
318 if (header.n_cols == 0 && header.n_rows == 0)
323 std::optional<std::array<double, 3>> coords;
324 std::vector<double> values(header.n_cols * header.n_rows, -9999);
325 std::size_t
const n_lines(string_lines.size());
326 for (std::size_t i = 0; i < n_lines; ++i)
329 std::size_t
const idx =
static_cast<std::size_t
>(
331 (((*coords)[1] - header.origin[1]) / header.cell_size)) +
332 (((*coords)[0] - header.origin[0]) / header.cell_size));
333 values[idx] = (*coords)[2];
339 std::string
const& file_name)
343 unsigned const nCols(header.
n_cols);
344 unsigned const nRows(header.
n_rows);
347 std::ofstream out(file_name);
348 out <<
"ncols " << nCols <<
"\n";
349 out <<
"nrows " << nRows <<
"\n";
350 auto const default_precision = out.precision();
351 out.precision(std::numeric_limits<double>::max_digits10);
352 out <<
"xllcorner " << origin[0] <<
"\n";
353 out <<
"yllcorner " << origin[1] <<
"\n";
354 out <<
"cellsize " << header.
cell_size <<
"\n";
355 out.precision(default_precision);
356 out <<
"NODATA_value " << header.
no_data <<
"\n";
359 for (
unsigned row(0); row < nRows; ++row)
361 for (
unsigned col(0); col < nCols - 1; ++col)
363 out << raster.
data()[(nRows - row - 1) * nCols + col] <<
" ";
365 out << raster.
data()[(nRows - row) * nCols - 1] <<
"\n";
373 return std::all_of(raster_paths.begin(), raster_paths.end(),
374 [](std::string
const& raster_path)
376 if (BaseLib::IsFileExisting(raster_path))
380 ERR(
"Opening raster file {} failed.", raster_path);
386 std::vector<std::string>
const& raster_paths)
393 std::vector<GeoLib::Raster const*> rasters;
394 rasters.reserve(raster_paths.size());
395 std::transform(raster_paths.begin(), raster_paths.end(),
396 std::back_inserter(rasters),
398 { return FileIO::AsciiRasterInterface::readRaster(path); });
399 return std::make_optional(rasters);
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
static GeoLib::Raster * getRasterFromXyzFile(std::string const &fname)
Reads a XYZ raster file.
static GeoLib::Raster * getRasterFromSurferFile(std::string const &fname)
Reads a Surfer GRD raster file.
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)
static GeoLib::Raster * getRasterFromASCFile(std::string const &fname)
Reads an ArcGis ASC raster file.
Class Raster is used for managing raster data.
double const * data() const
RasterHeader const & getHeader() const
Returns the complete header information.
std::string getFileExtension(const std::string &path)
std::string replaceString(const std::string &searchString, const std::string &replaceString, std::string stringToReplace)
std::optional< std::array< double, 3 > > readXyzCoordinates(std::string const &line)
static std::optional< GeoLib::RasterHeader > readASCHeader(std::ifstream &in)
static double readDoubleFromStream(std::istream &in)
Reads a double replacing comma by point.
std::vector< std::string > readFile(std::istream &in)
std::optional< std::vector< GeoLib::Raster const * > > readRasters(std::vector< std::string > const &raster_paths)
GeoLib::RasterHeader getXyzHeader(std::vector< std::string > const &lines)
static std::optional< std::tuple< GeoLib::RasterHeader, double, double > > readSurferHeader(std::ifstream &in)
static bool allRastersExist(std::vector< std::string > const &raster_paths)
Checks if all raster files actually exist.