29 std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
56static std::optional<GeoLib::RasterHeader>
readASCHeader(std::ifstream& in)
67 header.
n_cols = atoi(value.c_str());
78 header.
n_rows = atoi(value.c_str());
88 if (tag ==
"xllcorner" || tag ==
"xllcenter")
98 if (tag ==
"yllcorner" || tag ==
"yllcenter")
109 if (tag ==
"cellsize")
119 if (tag ==
"NODATA_value" || tag ==
"nodata_value")
132 std::string
const& fname)
134 std::ifstream in(fname.c_str());
138 WARN(
"Raster::getRasterFromASCFile(): Could not open file {:s}.",
147 "Raster::getRasterFromASCFile(): Could not read header of file "
153 std::vector<double> values(header->n_cols * header->n_rows);
155 for (std::size_t j(0); j < header->n_rows; ++j)
157 const std::size_t idx((header->n_rows - j - 1) * header->n_cols);
158 for (std::size_t i(0); i < header->n_cols; ++i)
169static std::optional<std::tuple<GeoLib::RasterHeader, double, double>>
178 ERR(
"Error in readSurferHeader() - No Surfer file.");
193 if (ceil((max - min) /
static_cast<double>(header.
n_rows)) ==
200 ERR(
"Error in readSurferHeader() - Anisotropic cellsize detected.");
207 return {{header, min, max}};
211 std::string
const& fname)
213 std::ifstream in(fname.c_str());
217 ERR(
"Raster::getRasterFromSurferFile() - Could not open file {:s}",
223 if (!optional_header)
225 ERR(
"Raster::getRasterFromASCFile() - could not read header of file "
231 auto const [header, min, max] = *optional_header;
232 std::vector<double> values(header.n_cols * header.n_rows);
234 for (std::size_t j(0); j < header.n_rows; ++j)
236 const std::size_t idx(j * header.n_cols);
237 for (std::size_t i(0); i < header.n_cols; ++i)
240 values[idx + i] = (val > max || val < min) ? header.no_data : val;
249 std::vector<std::string> lines;
250 std::string line(
"");
251 while (std::getline(in, line))
253 lines.emplace_back(line);
260 std::array<double, 3> coords = {0, 0, 0};
261 if (
auto const n = std::sscanf(line.c_str(),
"%lf %lf %lf", &coords[0],
262 &coords[1], &coords[2]);
265 ERR(
"Raster::readXyzCoordinates() - Read {:d} doubles out of 3 "
266 "expected from the following line:\n{:s}",
275 double x_min = std::numeric_limits<double>::max();
276 double x_max = -std::numeric_limits<double>::max();
277 double y_min = std::numeric_limits<double>::max();
278 double y_max = -std::numeric_limits<double>::max();
279 double cellsize = std::numeric_limits<double>::max();
280 std::optional<std::array<double, 3>> coords;
282 std::size_t
const n_lines(lines.size());
283 for (std::size_t i = 0; i < n_lines; ++i)
286 if (coords == std::nullopt)
292 double const diff = (*coords)[0] - x_min;
295 cellsize = std::min(cellsize, diff);
297 x_min = std::min((*coords)[0], x_min);
298 x_max = std::max((*coords)[0], x_max);
299 y_min = std::min((*coords)[1], y_min);
300 y_max = std::max((*coords)[1], y_max);
306 header.
n_cols =
static_cast<std::size_t
>(((x_max - x_min) / cellsize) + 1);
307 header.
n_rows =
static_cast<std::size_t
>(((y_max - y_min) / cellsize) + 1);
315 std::string
const& fname)
317 std::ifstream in(fname.c_str());
320 ERR(
"Raster::getRasterFromXyzFile() - Could not open file {:s}", fname);
324 auto const string_lines =
readFile(in);
328 if (header.n_cols == 0 && header.n_rows == 0)
333 std::optional<std::array<double, 3>> coords;
334 std::vector<double> values(header.n_cols * header.n_rows, -9999);
335 std::size_t
const n_lines(string_lines.size());
336 for (std::size_t i = 0; i < n_lines; ++i)
339 std::size_t
const idx =
static_cast<std::size_t
>(
341 (((*coords)[1] - header.origin[1]) / header.cell_size)) +
342 (((*coords)[0] - header.origin[0]) / header.cell_size));
343 values[idx] = (*coords)[2];
349 std::string
const& file_name)
353 unsigned const nCols(header.
n_cols);
354 unsigned const nRows(header.
n_rows);
357 std::ofstream out(file_name);
358 out <<
"ncols " << nCols <<
"\n";
359 out <<
"nrows " << nRows <<
"\n";
360 auto const default_precision = out.precision();
361 out.precision(std::numeric_limits<double>::max_digits10);
362 out <<
"xllcorner " << origin[0] <<
"\n";
363 out <<
"yllcorner " << origin[1] <<
"\n";
364 out <<
"cellsize " << header.
cell_size <<
"\n";
365 out.precision(default_precision);
366 out <<
"NODATA_value " << header.
no_data <<
"\n";
369 for (
unsigned row(0); row < nRows; ++row)
371 for (
unsigned col(0); col < nCols - 1; ++col)
373 out << raster.
data()[(nRows - row - 1) * nCols + col] <<
" ";
375 out << raster.
data()[(nRows - row) * nCols - 1] <<
"\n";
383 return std::all_of(raster_paths.begin(), raster_paths.end(),
384 [](std::string
const& raster_path)
386 if (BaseLib::IsFileExisting(raster_path))
390 ERR(
"Opening raster file {} failed.", raster_path);
395std::optional<std::vector<GeoLib::Raster const*>> readRasters(
396 std::vector<std::string>
const& raster_paths)
403 std::vector<GeoLib::Raster const*> rasters;
404 rasters.reserve(raster_paths.size());
405 std::transform(raster_paths.begin(), raster_paths.end(),
406 std::back_inserter(rasters),
408 { return FileIO::AsciiRasterInterface::readRaster(path); });
409 return std::make_optional(rasters);
Definition of the AsciiRasterInterface class.
Definition of the Point class.
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)
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.