OGS
FileIO::AsciiRasterInterface Class Reference

Detailed Description

Interface for reading and writing a number of ASCII raster formats. Currently supported are reading and writing of Esri asc-files and reading of Surfer grd-files.

Definition at line 29 of file AsciiRasterInterface.h.

#include <AsciiRasterInterface.h>

Static Public Member Functions

static GeoLib::RasterreadRaster (std::string const &fname)
 
static GeoLib::RastergetRasterFromASCFile (std::string const &fname)
 Reads an ArcGis ASC raster file.
 
static GeoLib::RastergetRasterFromSurferFile (std::string const &fname)
 Reads a Surfer GRD raster file.
 
static GeoLib::RastergetRasterFromXyzFile (std::string const &fname)
 Reads a XYZ raster file.
 
static void writeRasterAsASC (GeoLib::Raster const &raster, std::string const &file_name)
 Writes an Esri asc-file.
 

Member Function Documentation

◆ getRasterFromASCFile()

GeoLib::Raster * FileIO::AsciiRasterInterface::getRasterFromASCFile ( std::string const & fname)
static

Reads an ArcGis ASC raster file.

Definition at line 131 of file AsciiRasterInterface.cpp.

133{
134 std::ifstream in(fname.c_str());
135
136 if (!in.is_open())
137 {
138 WARN("Raster::getRasterFromASCFile(): Could not open file {:s}.",
139 fname);
140 return nullptr;
141 }
142
143 auto const header = readASCHeader(in);
144 if (!header)
145 {
146 WARN(
147 "Raster::getRasterFromASCFile(): Could not read header of file "
148 "{:s}",
149 fname);
150 return nullptr;
151 }
152
153 std::vector<double> values(header->n_cols * header->n_rows);
154 // read the data into the double-array
155 for (std::size_t j(0); j < header->n_rows; ++j)
156 {
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)
159 {
160 values[idx + i] = readDoubleFromStream(in);
161 }
162 }
163
164 return new GeoLib::Raster(*header, values.begin(), values.end());
165}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Class Raster is used for managing raster data.
Definition Raster.h:49
static std::optional< GeoLib::RasterHeader > readASCHeader(std::ifstream &in)
static double readDoubleFromStream(std::istream &in)
Reads a double replacing comma by point.

References FileIO::readASCHeader(), FileIO::readDoubleFromStream(), and WARN().

Referenced by VtkRaster::loadImage(), main(), MainWindow::mapGeometry(), and readRaster().

◆ getRasterFromSurferFile()

GeoLib::Raster * FileIO::AsciiRasterInterface::getRasterFromSurferFile ( std::string const & fname)
static

Reads a Surfer GRD raster file.

Definition at line 210 of file AsciiRasterInterface.cpp.

212{
213 std::ifstream in(fname.c_str());
214
215 if (!in.is_open())
216 {
217 ERR("Raster::getRasterFromSurferFile() - Could not open file {:s}",
218 fname);
219 return nullptr;
220 }
221
222 auto const optional_header = readSurferHeader(in);
223 if (!optional_header)
224 {
225 ERR("Raster::getRasterFromASCFile() - could not read header of file "
226 "{:s}",
227 fname);
228 return nullptr;
229 }
230
231 auto const [header, min, max] = *optional_header;
232 std::vector<double> values(header.n_cols * header.n_rows);
233 // read the data into the double-array
234 for (std::size_t j(0); j < header.n_rows; ++j)
235 {
236 const std::size_t idx(j * header.n_cols);
237 for (std::size_t i(0); i < header.n_cols; ++i)
238 {
239 const double val = readDoubleFromStream(in);
240 values[idx + i] = (val > max || val < min) ? header.no_data : val;
241 }
242 }
243
244 return new GeoLib::Raster(header, values.begin(), values.end());
245}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
static std::optional< std::tuple< GeoLib::RasterHeader, double, double > > readSurferHeader(std::ifstream &in)

References ERR(), FileIO::readDoubleFromStream(), and FileIO::readSurferHeader().

Referenced by VtkRaster::loadImage(), and readRaster().

◆ getRasterFromXyzFile()

GeoLib::Raster * FileIO::AsciiRasterInterface::getRasterFromXyzFile ( std::string const & fname)
static

Reads a XYZ raster file.

Definition at line 314 of file AsciiRasterInterface.cpp.

316{
317 std::ifstream in(fname.c_str());
318 if (!in.is_open())
319 {
320 ERR("Raster::getRasterFromXyzFile() - Could not open file {:s}", fname);
321 return nullptr;
322 }
323
324 auto const string_lines = readFile(in);
325 in.close();
326
327 auto const header = getXyzHeader(string_lines);
328 if (header.n_cols == 0 && header.n_rows == 0)
329 {
330 return nullptr;
331 }
332
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)
337 {
338 coords = readXyzCoordinates(string_lines[i]);
339 std::size_t const idx = static_cast<std::size_t>(
340 (header.n_cols *
341 (((*coords)[1] - header.origin[1]) / header.cell_size)) +
342 (((*coords)[0] - header.origin[0]) / header.cell_size));
343 values[idx] = (*coords)[2];
344 }
345 return new GeoLib::Raster(header, values.begin(), values.end());
346}
std::optional< std::array< double, 3 > > readXyzCoordinates(std::string const &line)
std::vector< std::string > readFile(std::istream &in)
GeoLib::RasterHeader getXyzHeader(std::vector< std::string > const &lines)
constexpr ranges::views::view_closure coords
Definition Mesh.h:232

References ERR(), FileIO::getXyzHeader(), FileIO::readFile(), and FileIO::readXyzCoordinates().

Referenced by VtkRaster::loadImage(), and readRaster().

◆ readRaster()

GeoLib::Raster * FileIO::AsciiRasterInterface::readRaster ( std::string const & fname)
static

Reads raster file by detecting type based on extension and then calling the appropriate method

Definition at line 26 of file AsciiRasterInterface.cpp.

27{
28 std::string ext(BaseLib::getFileExtension(fname));
29 std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
30 if (ext == ".asc")
31 {
32 return getRasterFromASCFile(fname);
33 }
34 if (ext == ".grd")
35 {
36 return getRasterFromSurferFile(fname);
37 }
38 if (ext == ".xyz")
39 {
40 return getRasterFromXyzFile(fname);
41 }
42 return nullptr;
43}
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 GeoLib::Raster * getRasterFromASCFile(std::string const &fname)
Reads an ArcGis ASC raster file.
std::string getFileExtension(const std::string &path)

References BaseLib::getFileExtension(), getRasterFromASCFile(), getRasterFromSurferFile(), and getRasterFromXyzFile().

Referenced by DirectConditionGenerator::directToSurfaceNodes(), DirectConditionGenerator::directWithSurfaceIntegration(), main(), MeshView::openMap2dMeshDialog(), MeshView::openRasterDataToMeshDialog(), and anonymous_namespace{NetCDFRasterReader.cpp}::readRasterFromFile().

◆ writeRasterAsASC()

void FileIO::AsciiRasterInterface::writeRasterAsASC ( GeoLib::Raster const & raster,
std::string const & file_name )
static

Writes an Esri asc-file.

Definition at line 348 of file AsciiRasterInterface.cpp.

350{
351 GeoLib::RasterHeader header(raster.getHeader());
352 MathLib::Point3d const& origin(header.origin);
353 unsigned const nCols(header.n_cols);
354 unsigned const nRows(header.n_rows);
355
356 // write header
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";
367
368 // write data
369 for (unsigned row(0); row < nRows; ++row)
370 {
371 for (unsigned col(0); col < nCols - 1; ++col)
372 {
373 out << raster.data()[(nRows - row - 1) * nCols + col] << " ";
374 }
375 out << raster.data()[(nRows - row) * nCols - 1] << "\n";
376 }
377 out.close();
378}
Contains the relevant information when storing a geoscientific raster data.
Definition Raster.h:28

References GeoLib::RasterHeader::cell_size, GeoLib::Raster::data(), GeoLib::Raster::getHeader(), GeoLib::RasterHeader::n_cols, GeoLib::RasterHeader::n_rows, GeoLib::RasterHeader::no_data, and GeoLib::RasterHeader::origin.

Referenced by convert(), main(), and VtkVisImageItem::writeAsRaster().


The documentation for this class was generated from the following files: