OGS
Raster.h
Go to the documentation of this file.
1
14#pragma once
15
16#include <array>
17#include <stdexcept>
18#include <utility>
19
20#include "BaseLib/Error.h"
21#include "MathLib/Point3d.h"
22
23namespace GeoLib {
24
26struct RasterHeader final
27{
28 std::size_t n_cols; // width
29 std::size_t n_rows; // height
30 std::size_t n_depth; // depth (for 3d image)
31 MathLib::Point3d origin; // lower left corner
32 double cell_size; // edge length of each pixel
33 double no_data; // no data value
34};
35
44class Raster final
45{
46public:
47 using const_iterator = const double*;
48 using iterator = double*;
49
59 template <typename InputIterator>
60 Raster(RasterHeader header, InputIterator begin, InputIterator end)
61 : _header(std::move(header)),
62 _raster_data(new double[_header.n_cols * _header.n_rows])
63 {
64 unsigned long const number_of_input_values =
65 static_cast<unsigned long>(std::distance(begin, end));
66 if (number_of_input_values != _header.n_cols * _header.n_rows)
67 {
68 delete[] _raster_data;
69 throw std::out_of_range(
70 "Number of raster data mismatch, need " +
71 std::to_string(_header.n_cols * _header.n_rows) +
72 " values, but got " + std::to_string(number_of_input_values));
73 }
75 }
76
77 Raster(Raster const&) = delete;
78 Raster(Raster&&) = delete;
79 Raster& operator=(Raster const&) = delete;
80 Raster& operator=(Raster&&) = delete;
81
83 RasterHeader const& getHeader() const { return _header; }
84
88 void refineRaster(std::size_t scaling);
89
94 const_iterator begin() const { return _raster_data; }
95
101
105 double const& operator()(std::size_t const row, std::size_t const col) const
106 {
107 if (row >= _header.n_rows || col >= _header.n_cols)
108 {
109 OGS_FATAL(
110 "Raster pixel ({}, {}) doesn't exist. Raster size is {} x {}.",
111 row, col, _header.n_rows, _header.n_cols);
112 }
113 return _raster_data[(_header.n_rows - 1 - row) * _header.n_cols + col];
114 }
115 double& operator()(std::size_t const row, std::size_t const col)
116 {
117 return const_cast<double&>(std::as_const(*this)(row, col));
118 }
119
123 double getValueAtPoint(const MathLib::Point3d &pnt) const;
124
126 double interpolateValueAtPoint(const MathLib::Point3d &pnt) const;
127
129 bool isPntOnRaster(MathLib::Point3d const& pnt) const;
130
131 ~Raster();
132
133private:
134 void setCellSize(double cell_size);
135 void setNoDataVal (double no_data_val);
136
139};
140
141} // namespace GeoLib
#define OGS_FATAL(...)
Definition: Error.h:26
Definition of the Point3d class.
Class Raster is used for managing raster data.
Definition: Raster.h:45
double & operator()(std::size_t const row, std::size_t const col)
Definition: Raster.h:115
GeoLib::RasterHeader _header
Definition: Raster.h:137
double * iterator
Definition: Raster.h:48
const double * const_iterator
Definition: Raster.h:47
Raster & operator=(Raster const &)=delete
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...
Definition: Raster.h:60
double const & operator()(std::size_t const row, std::size_t const col) const
Definition: Raster.h:105
void setNoDataVal(double no_data_val)
void refineRaster(std::size_t scaling)
Definition: Raster.cpp:25
double * _raster_data
Definition: Raster.h:138
Raster(Raster &&)=delete
double getValueAtPoint(const MathLib::Point3d &pnt) const
Definition: Raster.cpp:63
bool isPntOnRaster(MathLib::Point3d const &pnt) const
Checks if the given point is located within the (x,y)-extension of the raster.
Definition: Raster.cpp:158
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 l...
Definition: Raster.cpp:92
RasterHeader const & getHeader() const
Returns the complete header information.
Definition: Raster.h:83
Raster(Raster const &)=delete
const_iterator begin() const
Definition: Raster.h:94
Raster & operator=(Raster &&)=delete
void setCellSize(double cell_size)
const_iterator end() const
Definition: Raster.h:100
void copy(PETScVector const &x, PETScVector &y)
Definition: LinAlg.cpp:37
Contains the relevant information when storing a geoscientific raster data.
Definition: Raster.h:27
std::size_t n_depth
Definition: Raster.h:30
double cell_size
Definition: Raster.h:32
MathLib::Point3d origin
Definition: Raster.h:31
std::size_t n_cols
Definition: Raster.h:28
std::size_t n_rows
Definition: Raster.h:29