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 30 of file AsciiRasterInterface.h.

#include <AsciiRasterInterface.h>

Static Public Member Functions

static GeoLib::RasterreadRaster (std::string const &fname)
 Reads raster file by detecting type based on extension and then calling the appropriate method. More...
 
static GeoLib::RastergetRasterFromASCFile (std::string const &fname)
 Reads an ArcGis ASC raster file. More...
 
static GeoLib::RastergetRasterFromSurferFile (std::string const &fname)
 Reads a Surfer GRD raster file. More...
 
static void writeRasterAsASC (GeoLib::Raster const &raster, std::string const &file_name)
 Writes an Esri asc-file. More...
 

Member Function Documentation

◆ getRasterFromASCFile()

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

Reads an ArcGis ASC raster file.

Definition at line 125 of file AsciiRasterInterface.cpp.

127{
128 std::ifstream in(fname.c_str());
129
130 if (!in.is_open())
131 {
132 WARN("Raster::getRasterFromASCFile(): Could not open file {:s}.",
133 fname);
134 return nullptr;
135 }
136
137 auto const header = readASCHeader(in);
138 if (!header)
139 {
140 WARN(
141 "Raster::getRasterFromASCFile(): Could not read header of file "
142 "{:s}",
143 fname);
144 return nullptr;
145 }
146
147 std::vector<double> values(header->n_cols * header->n_rows);
148 // read the data into the double-array
149 for (std::size_t j(0); j < header->n_rows; ++j)
150 {
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)
153 {
154 values[idx + i] = readDoubleFromStream(in);
155 }
156 }
157
158 return new GeoLib::Raster(*header, values.begin(), values.end());
159}
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:39
Class Raster is used for managing raster data.
Definition: Raster.h:45
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 204 of file AsciiRasterInterface.cpp.

206{
207 std::ifstream in(fname.c_str());
208
209 if (!in.is_open())
210 {
211 ERR("Raster::getRasterFromSurferFile() - Could not open file {:s}",
212 fname);
213 return nullptr;
214 }
215
216 auto const optional_header = readSurferHeader(in);
217 if (!optional_header)
218 {
219 ERR("Raster::getRasterFromASCFile() - could not read header of file "
220 "{:s}",
221 fname);
222 return nullptr;
223 }
224
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);
228 // read the data into the double-array
229 for (std::size_t j(0); j < header.n_rows; ++j)
230 {
231 const std::size_t idx(j * header.n_cols);
232 for (std::size_t i(0); i < header.n_cols; ++i)
233 {
234 const double val = readDoubleFromStream(in);
235 values[idx + i] = (val > max || val < min) ? no_data_val : val;
236 }
237 }
238
239 return new GeoLib::Raster(header, values.begin(), values.end());
240}
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:44
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().

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

25{
26 std::string ext(BaseLib::getFileExtension(fname));
27 std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
28 if (ext == ".asc")
29 {
30 return getRasterFromASCFile(fname);
31 }
32 if (ext == ".grd")
33 {
34 return getRasterFromSurferFile(fname);
35 }
36 return nullptr;
37}
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)
Definition: FileTools.cpp:186

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

Referenced by DirectConditionGenerator::directToSurfaceNodes(), DirectConditionGenerator::directWithSurfaceIntegration(), main(), MeshView::openMap2dMeshDialog(), and MeshView::openRasterDataToMeshDialog().

◆ writeRasterAsASC()

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

Writes an Esri asc-file.

Definition at line 242 of file AsciiRasterInterface.cpp.

244{
245 GeoLib::RasterHeader header(raster.getHeader());
246 MathLib::Point3d const& origin(header.origin);
247 unsigned const nCols(header.n_cols);
248 unsigned const nRows(header.n_rows);
249
250 // write header
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";
261
262 // write data
263 double const* const elevation(raster.begin());
264 for (unsigned row(0); row < nRows; ++row)
265 {
266 for (unsigned col(0); col < nCols - 1; ++col)
267 {
268 out << elevation[(nRows - row - 1) * nCols + col] << " ";
269 }
270 out << elevation[(nRows - row) * nCols - 1] << "\n";
271 }
272 out.close();
273}
Contains the relevant information when storing a geoscientific raster data.
Definition: Raster.h:27

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

Referenced by convert(), and main().


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