OGS
GeoLib::Raster Class Referencefinal

Detailed Description

Class Raster is used for managing raster data.

A raster consists of the meta data like number of columns and rows, the lower left point, the size of a raster pixel and a value for invalid data pixels. Additional the object needs the raster data itself. The raster data will be copied from the constructor. The destructor will release the memory.

Definition at line 38 of file Raster.h.

#include <Raster.h>

Collaboration diagram for GeoLib::Raster:
[legend]

Public Member Functions

template<typename InputIterator>
 Raster (RasterHeader header, InputIterator begin, InputIterator end)
 Constructor for an object of class Raster. The raster data have to be handed over via input iterators. Deploying iterators has the advantage that the use of the class is independent from the input container.
 Raster (Raster const &)=default
 Raster (Raster &&)=default
Rasteroperator= (Raster const &)=default
Rasteroperator= (Raster &&)=default
 ~Raster ()=default
RasterHeader const & getHeader () const
 Returns the complete header information.
void refineRaster (std::size_t scaling)
std::vector< double >::const_iterator begin () const
std::vector< double >::const_iterator end () const
double const * data () const
double const & operator() (std::size_t const row, std::size_t const col) const
double & operator() (std::size_t const row, std::size_t const col)
double getValueAtPoint (const MathLib::Point3d &pnt) const
double interpolateValueAtPoint (const MathLib::Point3d &pnt) const
bool isPntOnRaster (MathLib::Point3d const &pnt) const
bool operator== (Raster const &) const =default

Private Member Functions

void setCellSize (double cell_size)
void setNoDataVal (double no_data_val)

Private Attributes

GeoLib::RasterHeader _header
std::vector< double > _raster_data

Constructor & Destructor Documentation

◆ Raster() [1/3]

template<typename InputIterator>
GeoLib::Raster::Raster ( RasterHeader header,
InputIterator begin,
InputIterator end )
inline

Constructor for an object of class Raster. The raster data have to be handed over via input iterators. Deploying iterators has the advantage that the use of the class is independent from the input container.

Parameters
headermeta-information about the raster (height, width, etc.)
begininput iterator pointing to the first element of the data
endinput iterator pointing to the last element of the data, end have to be reachable from begin

Definition at line 52 of file Raster.h.

53 : _header(std::move(header)),
54 _raster_data(_header.n_cols * _header.n_rows)
55 {
56 unsigned long const number_of_input_values =
57 static_cast<unsigned long>(std::distance(begin, end));
58 if (number_of_input_values != _header.n_cols * _header.n_rows)
59 {
60 throw std::out_of_range(
61 "Number of raster data mismatch, need " +
62 std::to_string(_header.n_cols * _header.n_rows) +
63 " values, but got " + std::to_string(number_of_input_values));
64 }
65 std::copy(begin, end, _raster_data.begin());
66 }
GeoLib::RasterHeader _header
Definition Raster.h:140
std::vector< double > _raster_data
Definition Raster.h:141
std::vector< double >::const_iterator begin() const
Definition Raster.h:86
std::vector< double >::const_iterator end() const
Definition Raster.h:96

References _header, _raster_data, begin(), and end().

Referenced by Raster(), Raster(), operator=(), operator=(), and operator==().

◆ Raster() [2/3]

GeoLib::Raster::Raster ( Raster const & )
default

References Raster().

◆ Raster() [3/3]

GeoLib::Raster::Raster ( Raster && )
default

References Raster().

◆ ~Raster()

GeoLib::Raster::~Raster ( )
default

Member Function Documentation

◆ begin()

std::vector< double >::const_iterator GeoLib::Raster::begin ( ) const
inline

Constant iterator that is pointing to the first raster pixel value.

Returns
constant iterator

Definition at line 86 of file Raster.h.

87 {
88 return _raster_data.begin();
89 }

References _raster_data.

Referenced by Raster(), and LayeredMeshGenerator::calcEpsilon().

◆ data()

double const * GeoLib::Raster::data ( ) const
inline

Definition at line 101 of file Raster.h.

101{ return _raster_data.data(); }

References _raster_data.

Referenced by MeshToolsLib::RasterToMesh::convert(), and FileIO::AsciiRasterInterface::writeRasterAsASC().

◆ end()

std::vector< double >::const_iterator GeoLib::Raster::end ( ) const
inline

Iterator to the element following the last pixel element of the raster

Returns
constant iterator

Definition at line 96 of file Raster.h.

97 {
98 return _raster_data.end();
99 }

References _raster_data.

Referenced by Raster(), and LayeredMeshGenerator::calcEpsilon().

◆ getHeader()

◆ getValueAtPoint()

double GeoLib::Raster::getValueAtPoint ( const MathLib::Point3d & pnt) const

Returns the raster value at the position of the given point.

Definition at line 46 of file Raster.cpp.

47{
48 if (pnt[0] >= _header.origin[0] &&
49 pnt[0] < (_header.origin[0] + (_header.cell_size * _header.n_cols)) &&
50 pnt[1] >= _header.origin[1] &&
51 pnt[1] < (_header.origin[1] + (_header.cell_size * _header.n_rows)))
52 {
53 auto cell_x = static_cast<int>(
54 std::floor((pnt[0] - _header.origin[0]) / _header.cell_size));
55 auto cell_y = static_cast<int>(
56 std::floor((pnt[1] - _header.origin[1]) / _header.cell_size));
57
58 // use raster boundary values if node is outside raster due to rounding
59 // errors or floating point arithmetic
60 cell_x = (cell_x < 0) ? 0
61 : ((cell_x > static_cast<int>(_header.n_cols))
62 ? static_cast<int>(_header.n_cols - 1)
63 : cell_x);
64 cell_y = (cell_y < 0) ? 0
65 : ((cell_y > static_cast<int>(_header.n_rows))
66 ? static_cast<int>(_header.n_rows - 1)
67 : cell_y);
68
69 const std::size_t index = cell_y * _header.n_cols + cell_x;
70 return _raster_data[index];
71 }
72 return _header.no_data;
73}

References _header, and _raster_data.

Referenced by DirectConditionGenerator::directToSurfaceNodes(), and MeshToolsLib::MeshLayerMapper::layerMapping().

◆ interpolateValueAtPoint()

double GeoLib::Raster::interpolateValueAtPoint ( const MathLib::Point3d & pnt) const

Interpolates the elevation of the given point based on the 8-neighbourhood of the raster cell it is located on

Definition at line 75 of file Raster.cpp.

76{
77 // position in raster
78 double const xPos((pnt[0] - _header.origin[0]) / _header.cell_size);
79 double const yPos((pnt[1] - _header.origin[1]) / _header.cell_size);
80 // raster cell index
81 double const xIdx(std::floor(xPos)); // carry out computions in double
82 double const yIdx(std::floor(yPos)); // so not to over- or underflow.
83
84 // weights for bilinear interpolation
85 double const xShift = std::abs((xPos - xIdx) - 0.5);
86 double const yShift = std::abs((yPos - yIdx) - 0.5);
87 Eigen::Vector4d weight = {(1 - xShift) * (1 - yShift),
88 xShift * (1 - yShift), xShift * yShift,
89 (1 - xShift) * yShift};
90
91 // neighbors to include in interpolation
92 int const xShiftIdx = (xPos - xIdx >= 0.5) ? 1 : -1;
93 int const yShiftIdx = (yPos - yIdx >= 0.5) ? 1 : -1;
94 std::array<int, 4> const x_nb = {{0, xShiftIdx, xShiftIdx, 0}};
95 std::array<int, 4> const y_nb = {{0, 0, yShiftIdx, yShiftIdx}};
96
97 // get pixel values
98 Eigen::Vector4d pix_val{};
99 unsigned no_data_count(0);
100 for (unsigned j = 0; j < 4; ++j)
101 {
102 // check if neighbour pixel is still on the raster, otherwise substitute
103 // a no data value. This also allows the cast to unsigned type.
104 if ((xIdx + x_nb[j]) < 0 || (yIdx + y_nb[j]) < 0 ||
105 (xIdx + x_nb[j]) > (_header.n_cols - 1) ||
106 (yIdx + y_nb[j]) > (_header.n_rows - 1))
107 {
108 pix_val[j] = _header.no_data;
109 }
110 else
111 {
112 pix_val[j] = _raster_data[static_cast<std::size_t>(yIdx + y_nb[j]) *
113 _header.n_cols +
114 static_cast<std::size_t>(xIdx + x_nb[j])];
115 }
116
117 // remove no data values
118 if (std::abs(pix_val[j] - _header.no_data) <
119 std::numeric_limits<double>::epsilon())
120 {
121 weight[j] = 0;
122 no_data_count++;
123 }
124 }
125
126 // adjust weights if necessary
127 if (no_data_count > 0)
128 {
129 if (no_data_count == 4 || weight.sum() == 0)
130 { // if there is absolutely no data just use the default value
131 return _header.no_data;
132 }
133
134 weight /= weight.sum();
135 }
136
137 // new value
138 return weight.dot(pix_val);
139}

References _header, and _raster_data.

Referenced by VtkImageDataToSurfacePointsFilter::createPointSurface(), LayeredMeshGenerator::getNewLayerNode(), and MeshToolsLib::MeshLayerMapper::layerMapping().

◆ isPntOnRaster()

bool GeoLib::Raster::isPntOnRaster ( MathLib::Point3d const & pnt) const

Checks if the given point is located within the (x,y)-extension of the raster.

Definition at line 141 of file Raster.cpp.

142{
143 return !(
144 (pnt[0] < _header.origin[0]) ||
145 (pnt[0] > _header.origin[0] + (_header.n_cols * _header.cell_size)) ||
146 (pnt[1] < _header.origin[1]) ||
147 (pnt[1] > _header.origin[1] + (_header.n_rows * _header.cell_size)));
148}

References _header.

Referenced by MeshToolsLib::MeshLayerMapper::layerMapping().

◆ operator()() [1/2]

double & GeoLib::Raster::operator() ( std::size_t const row,
std::size_t const col )
inline

Definition at line 116 of file Raster.h.

117 {
118 return const_cast<double&>(std::as_const(*this)(row, col));
119 }

◆ operator()() [2/2]

double const & GeoLib::Raster::operator() ( std::size_t const row,
std::size_t const col ) const
inline

Access the pixel specified by row, col.

Definition at line 106 of file Raster.h.

107 {
108 if (row >= _header.n_rows || col >= _header.n_cols)
109 {
110 OGS_FATAL(
111 "Raster pixel ({}, {}) doesn't exist. Raster size is {} x {}.",
112 row, col, _header.n_rows, _header.n_cols);
113 }
114 return _raster_data[(_header.n_rows - 1 - row) * _header.n_cols + col];
115 }
#define OGS_FATAL(...)
Definition Error.h:19

References _header, _raster_data, and OGS_FATAL.

◆ operator=() [1/2]

Raster & GeoLib::Raster::operator= ( Raster && )
default

References Raster().

◆ operator=() [2/2]

Raster & GeoLib::Raster::operator= ( Raster const & )
default

References Raster().

◆ operator==()

bool GeoLib::Raster::operator== ( Raster const & ) const
default

References Raster().

◆ refineRaster()

void GeoLib::Raster::refineRaster ( std::size_t scaling)

Refine the raster using scaling as a refinement parameter.

Definition at line 15 of file Raster.cpp.

16{
17 std::vector<double> new_raster_data(_header.n_rows * _header.n_cols *
18 scaling * scaling);
19
20 for (std::size_t row(0); row < _header.n_rows; row++)
21 {
22 for (std::size_t col(0); col < _header.n_cols; col++)
23 {
24 const std::size_t idx(row * _header.n_cols + col);
25 for (std::size_t new_row(row * scaling);
26 new_row < (row + 1) * scaling;
27 new_row++)
28 {
29 const std::size_t idx0(new_row * _header.n_cols * scaling);
30 for (std::size_t new_col(col * scaling);
31 new_col < (col + 1) * scaling;
32 new_col++)
33 {
34 new_raster_data[idx0 + new_col] = _raster_data[idx];
35 }
36 }
37 }
38 }
39
40 std::swap(_raster_data, new_raster_data);
41 _header.cell_size /= scaling;
42 _header.n_cols *= scaling;
43 _header.n_rows *= scaling;
44}

References _header, and _raster_data.

◆ setCellSize()

void GeoLib::Raster::setCellSize ( double cell_size)
private

◆ setNoDataVal()

void GeoLib::Raster::setNoDataVal ( double no_data_val)
private

Member Data Documentation

◆ _header

GeoLib::RasterHeader GeoLib::Raster::_header
private

◆ _raster_data

std::vector<double> GeoLib::Raster::_raster_data
private

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