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

moved xyz header construction into separate method

asdf
parent 8f63182c
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "BaseLib/FileTools.h" #include "BaseLib/FileTools.h"
#include "BaseLib/Logging.h" #include "BaseLib/Logging.h"
#include "BaseLib/StringTools.h" #include "BaseLib/StringTools.h"
#include "MathLib/Point3d.h"
#include "GeoLib/Point.h" #include "GeoLib/Point.h"
namespace FileIO namespace FileIO
...@@ -270,37 +271,24 @@ std::optional<std::array<double, 3>> readCoordinates(std::string const& line) ...@@ -270,37 +271,24 @@ std::optional<std::array<double, 3>> readCoordinates(std::string const& line)
} }
} }
GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile( GeoLib::RasterHeader getXyzHeader(std::vector<std::string> lines)
std::string const& fname)
{ {
std::ifstream in(fname.c_str()); double x_min = std::numeric_limits<double>::max();
if (!in.is_open()) double x_max = -std::numeric_limits<double>::max();
{ double y_min = std::numeric_limits<double>::max();
ERR("Raster::getRasterFromXyzFile() - Could not open file {:s}", fname); double y_max = -std::numeric_limits<double>::max();
return nullptr;
}
auto const string_lines = readFile(in);
in.close();
auto coords = readCoordinates(string_lines[0]);
if (coords == std::nullopt)
{
return nullptr;
}
double x_min = (*coords)[0];
double x_max = (*coords)[0];
double y_min = (*coords)[1];
double y_max = (*coords)[1];
double cellsize = std::numeric_limits<double>::max(); double cellsize = std::numeric_limits<double>::max();
std::optional<std::array<double, 3>> coords;
std::size_t const n_lines(string_lines.size()); std::size_t const n_lines(lines.size());
for (std::size_t i = 1; i < n_lines; ++i) for (std::size_t i = 0; i < n_lines; ++i)
{ {
coords = readCoordinates(string_lines[i]); coords = readCoordinates(lines[i]);
if (coords == std::nullopt) if (coords == std::nullopt)
{ {
return nullptr; MathLib::Point3d org(std::array<double, 3>{{0, 0, 0}});
GeoLib::RasterHeader fail = {0, 0, 0, org, 0, 0};
return fail;
} }
double const diff = (*coords)[0] - x_min; double const diff = (*coords)[0] - x_min;
if (diff > 0) if (diff > 0)
...@@ -321,14 +309,37 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile( ...@@ -321,14 +309,37 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile(
header.n_depth = 1; header.n_depth = 1;
header.origin[0] = x_min; header.origin[0] = x_min;
header.origin[1] = y_min; header.origin[1] = y_min;
return header;
}
GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile(
std::string const& fname)
{
std::ifstream in(fname.c_str());
if (!in.is_open())
{
ERR("Raster::getRasterFromXyzFile() - Could not open file {:s}", fname);
return nullptr;
}
auto const string_lines = readFile(in);
in.close();
auto const header = getXyzHeader(string_lines);
if (header.n_cols == 0 && header.n_rows == 0)
{
return nullptr;
}
std::optional<std::array<double, 3>> coords;
std::vector<double> values(header.n_cols * header.n_rows, -9999); std::vector<double> values(header.n_cols * header.n_rows, -9999);
std::size_t const n_lines(string_lines.size());
for (std::size_t i = 0; i < n_lines; ++i) for (std::size_t i = 0; i < n_lines; ++i)
{ {
coords = readCoordinates(string_lines[i]); coords = readCoordinates(string_lines[i]);
std::size_t const idx = static_cast<std::size_t>( std::size_t const idx = static_cast<std::size_t>(
(header.n_cols * (((*coords)[1] - y_min) / cellsize)) + (header.n_cols * (((*coords)[1] - header.origin[1]) / header.cell_size)) +
(((*coords)[0] - x_min) / cellsize)); (((*coords)[0] - header.origin[0]) / header.cell_size));
values[idx] = (*coords)[2]; values[idx] = (*coords)[2];
} }
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