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

262{
263 std::ifstream in(fname.c_str());
264 if (!in.is_open())
265 {
266 ERR("Raster::getRasterFromXyzFile() - Could not open file {:s}", fname);
267 return nullptr;
268 }
269
270 auto coords = readCoordinates(in);
271 if (coords == std::nullopt)
272 {
273 return nullptr;
274 }
275
276 std::vector<double> values;
277 values.push_back((*coords)[2]);
278
279 auto coords2 = readCoordinates(in);
280 if (coords2 == std::nullopt)
281 {
282 return nullptr;
283 }
284 values.push_back((*coords2)[2]);
286 0, 0, 1, GeoLib::Point(*coords), (*coords2)[0] - (*coords)[0], -9999};
287
288 std::size_t n_cols = 2, n_rows = 1;
289 while ((coords = readCoordinates(in)))
290 {
291 values.push_back((*coords)[2]);
292 if ((*coords)[0] > (*coords2)[0])
293 {
294 if ((*coords)[0] - (*coords2)[0] != header.cell_size)
295 {
296 ERR("Varying cell sizes or unordered pixel values found. "
297 "Aborting...");
298 return nullptr;
299 }
300 n_cols++;
301 }
302 else // new line
303 {
304 if ((*coords)[1] - (*coords2)[1] != header.cell_size)
305 {
306 ERR("Varying cell sizes or unordered pixel values found. "
307 "Aborting...");
308 return nullptr;
309 }
310 n_rows++;
311 // define #columns
312 if (header.n_cols == 0)
313 {
314 header.n_cols = n_cols;
315 }
316 // just check if #columns is consistent
317 else
318 {
319 if (n_cols != header.n_cols)
320 {
321 ERR("Different number of pixels per line. Aborting!");
322 return nullptr;
323 }
324 }
325 n_cols = 1;
326 }
327 coords2 = coords;
328 }
329 header.n_rows = n_rows;
330 if (header.n_cols == 0)
331 {
332 ERR("Could not determine raster size. Note that minimum allowed raster "
333 "size is 2 x 2 pixels.");
334 return nullptr;
335 }
336 return new GeoLib::Raster(header, values.begin(), values.end());
337}
std::optional< std::array< double, 3 > > readCoordinates(std::istream &in)
constexpr ranges::views::view_closure coords
Definition Mesh.h:232
Contains the relevant information when storing a geoscientific raster data.
Definition Raster.h:28

References ERR(), and FileIO::readCoordinates().

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

341{
342 GeoLib::RasterHeader header(raster.getHeader());
343 MathLib::Point3d const& origin(header.origin);
344 unsigned const nCols(header.n_cols);
345 unsigned const nRows(header.n_rows);
346
347 // write header
348 std::ofstream out(file_name);
349 out << "ncols " << nCols << "\n";
350 out << "nrows " << nRows << "\n";
351 auto const default_precision = out.precision();
352 out.precision(std::numeric_limits<double>::digits10);
353 out << "xllcorner " << origin[0] << "\n";
354 out << "yllcorner " << origin[1] << "\n";
355 out << "cellsize " << header.cell_size << "\n";
356 out.precision(default_precision);
357 out << "NODATA_value " << header.no_data << "\n";
358
359 // write data
360 for (unsigned row(0); row < nRows; ++row)
361 {
362 for (unsigned col(0); col < nCols - 1; ++col)
363 {
364 out << raster.data()[(nRows - row - 1) * nCols + col] << " ";
365 }
366 out << raster.data()[(nRows - row) * nCols - 1] << "\n";
367 }
368 out.close();
369}

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: