Skip to content
Snippets Groups Projects
Commit 8f63182c authored by Karsten Rink's avatar Karsten Rink Committed by Dmitri Naumov
Browse files

xyz file is only read once and values are stored in memory

asdf
parent 726735c6
No related branches found
No related tags found
No related merge requests found
...@@ -244,23 +244,30 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromSurferFile( ...@@ -244,23 +244,30 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromSurferFile(
return new GeoLib::Raster(header, values.begin(), values.end()); return new GeoLib::Raster(header, values.begin(), values.end());
} }
std::optional<std::array<double, 3>> readCoordinates(std::istream& in) std::vector<std::string> readFile(std::istream& in)
{ {
std::vector<std::string> lines;
std::string line(""); std::string line("");
if (std::getline(in, line)) while (std::getline(in, line))
{ {
std::array<double, 3> coords; lines.emplace_back(line);
if (std::sscanf(line.c_str(), "%lf %lf %lf", &coords[0], &coords[1], }
&coords[2]) == 3) return lines;
{ }
return coords;
} std::optional<std::array<double, 3>> readCoordinates(std::string const& line)
else {
{ std::array<double, 3> coords;
OGS_FATAL("Could not parse coordinates in readCoordinates()."); if (std::sscanf(line.c_str(), "%lf %lf %lf", &coords[0], &coords[1],
} &coords[2]) == 3)
{
return coords;
}
else
{
ERR("Raster::getRasterFromXyzFile() - Unexpected file format:\n{:s}, line");
return std::nullopt;
} }
return std::nullopt;
} }
GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile( GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile(
...@@ -273,7 +280,10 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile( ...@@ -273,7 +280,10 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile(
return nullptr; return nullptr;
} }
auto coords = readCoordinates(in); auto const string_lines = readFile(in);
in.close();
auto coords = readCoordinates(string_lines[0]);
if (coords == std::nullopt) if (coords == std::nullopt)
{ {
return nullptr; return nullptr;
...@@ -284,17 +294,24 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile( ...@@ -284,17 +294,24 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile(
double y_max = (*coords)[1]; double y_max = (*coords)[1];
double cellsize = std::numeric_limits<double>::max(); double cellsize = std::numeric_limits<double>::max();
while ((coords = readCoordinates(in))) std::size_t const n_lines(string_lines.size());
for (std::size_t i = 1; i < n_lines; ++i)
{ {
coords = readCoordinates(string_lines[i]);
if (coords == std::nullopt)
{
return nullptr;
}
double const diff = (*coords)[0] - x_min; double const diff = (*coords)[0] - x_min;
if (diff > 0) if (diff > 0)
{
cellsize = std::min(cellsize, diff); cellsize = std::min(cellsize, diff);
}
x_min = std::min((*coords)[0], x_min); x_min = std::min((*coords)[0], x_min);
x_max = std::max((*coords)[0], x_max); x_max = std::max((*coords)[0], x_max);
y_min = std::min((*coords)[1], y_min); y_min = std::min((*coords)[1], y_min);
y_max = std::max((*coords)[1], y_max); y_max = std::max((*coords)[1], y_max);
} }
in.close();
GeoLib::RasterHeader header; GeoLib::RasterHeader header;
header.cell_size = cellsize; header.cell_size = cellsize;
...@@ -306,15 +323,14 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile( ...@@ -306,15 +323,14 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile(
header.origin[1] = y_min; header.origin[1] = y_min;
std::vector<double> values(header.n_cols * header.n_rows, -9999); std::vector<double> values(header.n_cols * header.n_rows, -9999);
in.open(fname); for (std::size_t i = 0; i < n_lines; ++i)
while ((coords = readCoordinates(in)))
{ {
std::size_t idx = static_cast<std::size_t>( coords = readCoordinates(string_lines[i]);
std::size_t const idx = static_cast<std::size_t>(
(header.n_cols * (((*coords)[1] - y_min) / cellsize)) + (header.n_cols * (((*coords)[1] - y_min) / cellsize)) +
(((*coords)[0] - x_min) / cellsize)); (((*coords)[0] - x_min) / cellsize));
values[idx] = (*coords)[2]; values[idx] = (*coords)[2];
} }
in.close();
return new GeoLib::Raster(header, values.begin(), values.end()); return new GeoLib::Raster(header, values.begin(), values.end());
} }
......
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