27 std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
50 static std::optional<GeoLib::RasterHeader>
readASCHeader(std::ifstream& in)
61 header.
n_cols = atoi(value.c_str());
72 header.
n_rows = atoi(value.c_str());
82 if (tag ==
"xllcorner" || tag ==
"xllcenter")
92 if (tag ==
"yllcorner" || tag ==
"yllcenter")
103 if (tag ==
"cellsize")
113 if (tag ==
"NODATA_value" || tag ==
"nodata_value")
126 std::string
const& fname)
128 std::ifstream in(fname.c_str());
132 WARN(
"Raster::getRasterFromASCFile(): Could not open file {:s}.",
141 "Raster::getRasterFromASCFile(): Could not read header of file "
147 std::vector<double> values(header->n_cols * header->n_rows);
149 for (std::size_t j(0); j < header->n_rows; ++j)
151 const std::size_t idx((header->n_rows - j - 1) * header->n_cols);
152 for (std::size_t i(0); i < header->n_cols; ++i)
163 static std::optional<std::tuple<GeoLib::RasterHeader, double, double>>
172 ERR(
"Error in readSurferHeader() - No Surfer file.");
187 if (ceil((max - min) /
static_cast<double>(header.
n_rows)) ==
194 ERR(
"Error in readSurferHeader() - Anisotropic cellsize detected.");
201 return {{header, min, max}};
205 std::string
const& fname)
207 std::ifstream in(fname.c_str());
211 ERR(
"Raster::getRasterFromSurferFile() - Could not open file {:s}",
217 if (!optional_header)
219 ERR(
"Raster::getRasterFromASCFile() - could not read header of file "
225 auto const [header, min, max] = *optional_header;
226 const double no_data_val(min - 1);
227 std::vector<double> values(header.n_cols * header.n_rows);
229 for (std::size_t j(0); j < header.n_rows; ++j)
231 const std::size_t idx(j * header.n_cols);
232 for (std::size_t i(0); i < header.n_cols; ++i)
235 values[idx + i] = (val > max || val < min) ? no_data_val : val;
243 std::string
const& file_name)
247 unsigned const nCols(header.
n_cols);
248 unsigned const nRows(header.
n_rows);
251 std::ofstream out(file_name);
252 out <<
"ncols " << nCols <<
"\n";
253 out <<
"nrows " << nRows <<
"\n";
254 auto const default_precision = out.precision();
255 out.precision(std::numeric_limits<double>::digits10);
256 out <<
"xllcorner " << origin[0] <<
"\n";
257 out <<
"yllcorner " << origin[1] <<
"\n";
258 out <<
"cellsize " << header.
cell_size <<
"\n";
259 out.precision(default_precision);
260 out <<
"NODATA_value " << header.
no_data <<
"\n";
263 double const*
const elevation(raster.
begin());
264 for (
unsigned row(0); row < nRows; ++row)
266 for (
unsigned col(0); col < nCols - 1; ++col)
268 out << elevation[(nRows - row - 1) * nCols + col] <<
" ";
270 out << elevation[(nRows - row) * nCols - 1] <<
"\n";
278 for (
const auto& raster_path : raster_paths)
280 std::ifstream file_stream(raster_path, std::ifstream::in);
281 if (!file_stream.good())
291 std::vector<std::string>
const& raster_paths)
298 std::vector<GeoLib::Raster const*> rasters;
299 rasters.reserve(raster_paths.size());
300 std::transform(raster_paths.begin(), raster_paths.end(),
301 std::back_inserter(rasters),
303 { return FileIO::AsciiRasterInterface::readRaster(path); });
304 return std::make_optional(rasters);
Definition of the AsciiRasterInterface class.
void ERR(char const *fmt, Args const &... args)
void WARN(char const *fmt, Args const &... args)
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)
Reads raster file by detecting type based on extension and then calling the appropriate method.
static GeoLib::Raster * getRasterFromASCFile(std::string const &fname)
Reads an ArcGis ASC raster file.
Class Raster is used for managing raster data.
const_iterator begin() 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)
static std::optional< std::tuple< GeoLib::RasterHeader, double, double > > readSurferHeader(std::ifstream &in)
static double readDoubleFromStream(std::istream &in)
Reads a double replacing comma by point.
std::optional< std::vector< GeoLib::Raster const * > > readRasters(std::vector< std::string > const &raster_paths)
static std::optional< GeoLib::RasterHeader > readASCHeader(std::ifstream &in)
static bool allRastersExist(std::vector< std::string > const &raster_paths)
Checks if all raster files actually exist.