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

generalised interface for xyz raster files

parent 0f84d3c6
No related branches found
No related tags found
No related merge requests found
...@@ -272,67 +272,43 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile( ...@@ -272,67 +272,43 @@ GeoLib::Raster* AsciiRasterInterface::getRasterFromXyzFile(
{ {
return nullptr; 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();
std::vector<double> values; while ((coords = readCoordinates(in)))
values.push_back((*coords)[2]);
auto coords2 = readCoordinates(in);
if (coords2 == std::nullopt)
{ {
return nullptr; double const diff = (*coords)[0] - x_min;
if (diff > 0)
cellsize = std::min(cellsize, diff);
x_min = std::min((*coords)[0], x_min);
x_max = std::max((*coords)[0], x_max);
y_min = std::min((*coords)[1], y_min);
y_max = std::max((*coords)[1], y_max);
} }
values.push_back((*coords2)[2]); in.close();
GeoLib::RasterHeader header{
0, 0, 1, GeoLib::Point(*coords), (*coords2)[0] - (*coords)[0], -9999}; GeoLib::RasterHeader header;
header.cell_size = cellsize;
header.no_data = -9999;
header.n_cols = static_cast<std::size_t>(((x_max - x_min) / cellsize) + 1);
header.n_rows = static_cast<std::size_t>(((y_max - y_min) / cellsize) + 1);
header.n_depth = 1;
header.origin[0] = x_min;
header.origin[1] = y_min;
std::size_t n_cols = 2, n_rows = 1; std::vector<double> values(header.n_cols * header.n_rows, -9999);
in.open(fname);
while ((coords = readCoordinates(in))) while ((coords = readCoordinates(in)))
{ {
values.push_back((*coords)[2]); std::size_t idx = static_cast<std::size_t>(
if ((*coords)[0] > (*coords2)[0]) (header.n_cols * (((*coords)[1] - y_min) / cellsize)) +
{ (((*coords)[0] - x_min) / cellsize));
if ((*coords)[0] - (*coords2)[0] != header.cell_size) values[idx] = (*coords)[2];
{
ERR("Varying cell sizes or unordered pixel values found. "
"Aborting...");
return nullptr;
}
n_cols++;
}
else // new line
{
if ((*coords)[1] - (*coords2)[1] != header.cell_size)
{
ERR("Varying cell sizes or unordered pixel values found. "
"Aborting...");
return nullptr;
}
n_rows++;
// define #columns
if (header.n_cols == 0)
{
header.n_cols = n_cols;
}
// just check if #columns is consistent
else
{
if (n_cols != header.n_cols)
{
ERR("Different number of pixels per line. Aborting!");
return nullptr;
}
}
n_cols = 1;
}
coords2 = coords;
}
header.n_rows = n_rows;
if (header.n_cols == 0)
{
ERR("Could not determine raster size. Note that minimum allowed raster "
"size is 2 x 2 pixels.");
return nullptr;
} }
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