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 315 of file AsciiRasterInterface.cpp.

317{
318 std::ifstream in(fname.c_str());
319 if (!in.is_open())
320 {
321 ERR("Raster::getRasterFromXyzFile() - Could not open file {:s}", fname);
322 return nullptr;
323 }
324
325 auto const string_lines = readFile(in);
326 in.close();
327
328 auto const header = getXyzHeader(string_lines);
329 if (header.n_cols == 0 && header.n_rows == 0)
330 {
331 return nullptr;
332 }
333
334 std::optional<std::array<double, 3>> coords;
335 std::vector<double> values(header.n_cols * header.n_rows, -9999);
336 std::size_t const n_lines(string_lines.size());
337 for (std::size_t i = 0; i < n_lines; ++i)
338 {
339 coords = readXyzCoordinates(string_lines[i]);
340 std::size_t const idx = static_cast<std::size_t>(
341 (header.n_cols *
342 (((*coords)[1] - header.origin[1]) / header.cell_size)) +
343 (((*coords)[0] - header.origin[0]) / header.cell_size));
344 values[idx] = (*coords)[2];
345 }
346 return new GeoLib::Raster(header, values.begin(), values.end());
347}
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 349 of file AsciiRasterInterface.cpp.

351{
352 GeoLib::RasterHeader header(raster.getHeader());
353 MathLib::Point3d const& origin(header.origin);
354 unsigned const nCols(header.n_cols);
355 unsigned const nRows(header.n_rows);
356
357 // write header
358 std::ofstream out(file_name);
359 out << "ncols " << nCols << "\n";
360 out << "nrows " << nRows << "\n";
361 auto const default_precision = out.precision();
362 out.precision(std::numeric_limits<double>::max_digits10);
363 out << "xllcorner " << origin[0] << "\n";
364 out << "yllcorner " << origin[1] << "\n";
365 out << "cellsize " << header.cell_size << "\n";
366 out.precision(default_precision);
367 out << "NODATA_value " << header.no_data << "\n";
368
369 // write data
370 for (unsigned row(0); row < nRows; ++row)
371 {
372 for (unsigned col(0); col < nCols - 1; ++col)
373 {
374 out << raster.data()[(nRows - row - 1) * nCols + col] << " ";
375 }
376 out << raster.data()[(nRows - row) * nCols - 1] << "\n";
377 }
378 out.close();
379}
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: