Skip to content
Snippets Groups Projects
Commit c04b0b7e authored by Dmitri Naumov's avatar Dmitri Naumov
Browse files

[App/IO] Surfer raster header returns an optional.

parent b94ce776
No related branches found
No related tags found
No related merge requests found
......@@ -168,13 +168,9 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromSurferFile(
return nullptr;
}
// header information
GeoLib::RasterHeader header;
double min(0.0);
double max(0.0);
if (readSurferHeader(in, header, min, max))
if (auto const optional_header = readSurferHeader(in))
{
auto const [header, min, max] = *optional_header;
const double no_data_val(min - 1);
std::vector<double> values(header.n_cols * header.n_rows);
std::string s;
......@@ -191,17 +187,15 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromSurferFile(
}
}
in.close();
return new GeoLib::Raster(std::move(header), values.begin(),
values.end());
return new GeoLib::Raster(header, values.begin(), values.end());
}
ERR("Raster::getRasterFromASCFile() - could not read header of file {:s}",
fname);
return nullptr;
}
bool AsciiRasterInterface::readSurferHeader(std::ifstream& in,
GeoLib::RasterHeader& header,
double& min, double& max)
std::optional<std::tuple<GeoLib::RasterHeader, double, double>>
AsciiRasterInterface::readSurferHeader(std::ifstream& in)
{
std::string tag;
......@@ -210,10 +204,12 @@ bool AsciiRasterInterface::readSurferHeader(std::ifstream& in,
if (tag != "DSAA")
{
ERR("Error in readSurferHeader() - No Surfer file.");
return false;
return {};
}
GeoLib::RasterHeader header;
in >> header.n_cols >> header.n_rows;
double min, max;
in >> min >> max;
header.origin[0] = min;
header.cell_size = (max - min) / static_cast<double>(header.n_cols);
......@@ -230,13 +226,13 @@ bool AsciiRasterInterface::readSurferHeader(std::ifstream& in,
else
{
ERR("Error in readSurferHeader() - Anisotropic cellsize detected.");
return false;
return {};
}
header.n_depth = 1;
header.no_data = min - 1;
in >> min >> max;
return true;
return {{header, min, max}};
}
void AsciiRasterInterface::writeRasterAsASC(GeoLib::Raster const& raster,
......
......@@ -16,6 +16,7 @@
#include <fstream>
#include <optional>
#include <string>
#include <tuple>
#include <vector>
#include "GeoLib/Raster.h"
......@@ -47,9 +48,10 @@ private:
/// If the return value is empty, reading was not successful.
static std::optional<GeoLib::RasterHeader> readASCHeader(std::ifstream& in);
/// Reads the header of a Surfer grd-file.
static bool readSurferHeader(std::ifstream &in, GeoLib::RasterHeader &header,
double &min, double &max);
/// Reads the header of a Surfer grd-file with minimum and maximum values.
/// If the return value is empty, reading was not successful.
static std::optional<std::tuple<GeoLib::RasterHeader, double, double>>
readSurferHeader(std::ifstream& in);
};
/// Reads a vector of rasters given by file names. On error nothing is returned,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment