OGS
GeoLib::Raster Class Reference

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 42 of file Raster.h.

#include <Raster.h>

Collaboration diagram for GeoLib::Raster:
[legend]

Public Types

using const_iterator = const double *
 
using iterator = double *
 

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. More...
 
 Raster (Raster const &)=delete
 
 Raster (Raster &&)=delete
 
Rasteroperator= (Raster const &)=delete
 
Rasteroperator= (Raster &&)=delete
 
RasterHeader const & getHeader () const
 Returns the complete header information. More...
 
void refineRaster (std::size_t scaling)
 
const_iterator begin () const
 
const_iterator end () const
 
double getValueAtPoint (const MathLib::Point3d &pnt) const
 
double 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. More...
 
bool isPntOnRaster (MathLib::Point3d const &pnt) const
 Checks if the given point is located within the (x,y)-extension of the raster. More...
 
 ~Raster ()
 

Private Member Functions

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

Private Attributes

GeoLib::RasterHeader _header
 
double * _raster_data
 

Member Typedef Documentation

◆ const_iterator

using GeoLib::Raster::const_iterator = const double*

Definition at line 44 of file Raster.h.

◆ iterator

using GeoLib::Raster::iterator = double*

Definition at line 45 of file Raster.h.

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 57 of file Raster.h.

58  : _header(std::move(header)),
60  {
62  }
GeoLib::RasterHeader _header
Definition: Raster.h:105
double * _raster_data
Definition: Raster.h:106
const_iterator begin() const
Definition: Raster.h:81
const_iterator end() const
Definition: Raster.h:86
void copy(PETScVector const &x, PETScVector &y)
Definition: LinAlg.cpp:37
std::size_t n_cols
Definition: Raster.h:26
std::size_t n_rows
Definition: Raster.h:27

References _raster_data, begin(), MathLib::LinAlg::copy(), and end().

◆ Raster() [2/3]

GeoLib::Raster::Raster ( Raster const &  )
delete

◆ Raster() [3/3]

GeoLib::Raster::Raster ( Raster &&  )
delete

◆ ~Raster()

GeoLib::Raster::~Raster ( )

Definition at line 58 of file Raster.cpp.

59 {
60  delete[] _raster_data;
61 }

References _raster_data.

Member Function Documentation

◆ begin()

const_iterator GeoLib::Raster::begin ( ) const
inline

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

Returns
constant iterator

Definition at line 81 of file Raster.h.

81 { return _raster_data; }

References _raster_data.

Referenced by Raster(), LayeredMeshGenerator::calcEpsilon(), MeshLib::RasterToMesh::convert(), and FileIO::AsciiRasterInterface::writeRasterAsASC().

◆ end()

const_iterator GeoLib::Raster::end ( ) const
inline

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

Returns
constant iterator

Definition at line 86 of file Raster.h.

References _header, _raster_data, GeoLib::RasterHeader::n_cols, and GeoLib::RasterHeader::n_rows.

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 63 of file Raster.cpp.

64 {
65  if (pnt[0] >= _header.origin[0] &&
66  pnt[0] < (_header.origin[0] + (_header.cell_size * _header.n_cols)) &&
67  pnt[1] >= _header.origin[1] &&
68  pnt[1] < (_header.origin[1] + (_header.cell_size * _header.n_rows)))
69  {
70  auto cell_x = static_cast<int>(
71  std::floor((pnt[0] - _header.origin[0]) / _header.cell_size));
72  auto cell_y = static_cast<int>(
73  std::floor((pnt[1] - _header.origin[1]) / _header.cell_size));
74 
75  // use raster boundary values if node is outside raster due to rounding
76  // errors or floating point arithmetic
77  cell_x = (cell_x < 0) ? 0
78  : ((cell_x > static_cast<int>(_header.n_cols))
79  ? static_cast<int>(_header.n_cols - 1)
80  : cell_x);
81  cell_y = (cell_y < 0) ? 0
82  : ((cell_y > static_cast<int>(_header.n_rows))
83  ? static_cast<int>(_header.n_rows - 1)
84  : cell_y);
85 
86  const std::size_t index = cell_y * _header.n_cols + cell_x;
87  return _raster_data[index];
88  }
89  return _header.no_data;
90 }
double cell_size
Definition: Raster.h:30
MathLib::Point3d origin
Definition: Raster.h:29

References _header, _raster_data, GeoLib::RasterHeader::cell_size, GeoLib::RasterHeader::n_cols, GeoLib::RasterHeader::n_rows, GeoLib::RasterHeader::no_data, and GeoLib::RasterHeader::origin.

Referenced by DirectConditionGenerator::directToSurfaceNodes(), and MeshLib::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 92 of file Raster.cpp.

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

References _header, _raster_data, GeoLib::RasterHeader::cell_size, GeoLib::RasterHeader::n_cols, GeoLib::RasterHeader::n_rows, GeoLib::RasterHeader::no_data, and GeoLib::RasterHeader::origin.

Referenced by VtkImageDataToSurfacePointsFilter::createPointSurface(), LayeredMeshGenerator::getNewLayerNode(), and MeshLib::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 158 of file Raster.cpp.

159 {
160  return !(
161  (pnt[0] < _header.origin[0]) ||
162  (pnt[0] > _header.origin[0] + (_header.n_cols * _header.cell_size)) ||
163  (pnt[1] < _header.origin[1]) ||
164  (pnt[1] > _header.origin[1] + (_header.n_rows * _header.cell_size)));
165 }

References _header, GeoLib::RasterHeader::cell_size, GeoLib::RasterHeader::n_cols, GeoLib::RasterHeader::n_rows, and GeoLib::RasterHeader::origin.

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

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ refineRaster()

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

Refine the raster using scaling as a refinement parameter.

Definition at line 25 of file Raster.cpp.

26 {
27  auto* new_raster_data(
28  new double[_header.n_rows * _header.n_cols * scaling * scaling]);
29 
30  for (std::size_t row(0); row < _header.n_rows; row++)
31  {
32  for (std::size_t col(0); col < _header.n_cols; col++)
33  {
34  const std::size_t idx(row * _header.n_cols + col);
35  for (std::size_t new_row(row * scaling);
36  new_row < (row + 1) * scaling;
37  new_row++)
38  {
39  const std::size_t idx0(new_row * _header.n_cols * scaling);
40  for (std::size_t new_col(col * scaling);
41  new_col < (col + 1) * scaling;
42  new_col++)
43  {
44  new_raster_data[idx0 + new_col] = _raster_data[idx];
45  }
46  }
47  }
48  }
49 
50  std::swap(_raster_data, new_raster_data);
51  _header.cell_size /= scaling;
52  _header.n_cols *= scaling;
53  _header.n_rows *= scaling;
54 
55  delete[] new_raster_data;
56 }

References _header, _raster_data, GeoLib::RasterHeader::cell_size, GeoLib::RasterHeader::n_cols, and GeoLib::RasterHeader::n_rows.

◆ 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

double* GeoLib::Raster::_raster_data
private

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