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{
25
27struct RasterHeader final
28{
29 std::size_t n_cols; // width
30 std::size_t n_rows; // height
31 std::size_t n_depth; // depth (for 3d image)
32 MathLib::Point3d origin; // lower left corner
33 double cell_size; // edge length of each pixel
34 double no_data; // no data value
35
36 bool operator==(RasterHeader const&) const = default;
37};
38
48class Raster final
49{
50public:
61 template <typename InputIterator>
62 Raster(RasterHeader header, InputIterator begin, InputIterator end)
63 : _header(std::move(header)),
64 _raster_data(_header.n_cols * _header.n_rows)
65 {
66 unsigned long const number_of_input_values =
67 static_cast<unsigned long>(std::distance(begin, end));
68 if (number_of_input_values != _header.n_cols * _header.n_rows)
69 {
70 throw std::out_of_range(
71 "Number of raster data mismatch, need " +
72 std::to_string(_header.n_cols * _header.n_rows) +
73 " values, but got " + std::to_string(number_of_input_values));
74 }
75 std::copy(begin, end, _raster_data.begin());
76 }
77
78 Raster(Raster const&) = default;
79 Raster(Raster&&) = default;
80 Raster& operator=(Raster const&) = default;
81 Raster& operator=(Raster&&) = default;
82 ~Raster() = default;
83
85 RasterHeader const& getHeader() const { return _header; }
86
90 void refineRaster(std::size_t scaling);
91
96 std::vector<double>::const_iterator begin() const
97 {
98 return _raster_data.begin();
99 }
100
106 std::vector<double>::const_iterator end() const
107 {
108 return _raster_data.end();
109 }
110
111 double const* data() const { return _raster_data.data(); }
112
116 double const& operator()(std::size_t const row, std::size_t const col) const
117 {
118 if (row >= _header.n_rows || col >= _header.n_cols)
119 {
120 OGS_FATAL(
121 "Raster pixel ({}, {}) doesn't exist. Raster size is {} x {}.",
122 row, col, _header.n_rows, _header.n_cols);
123 }
124 return _raster_data[(_header.n_rows - 1 - row) * _header.n_cols + col];
125 }
126 double& operator()(std::size_t const row, std::size_t const col)
127 {
128 return const_cast<double&>(std::as_const(*this)(row, col));
129 }
130
134 double getValueAtPoint(const MathLib::Point3d& pnt) const;
135
138 double interpolateValueAtPoint(const MathLib::Point3d& pnt) const;
139
142 bool isPntOnRaster(MathLib::Point3d const& pnt) const;
143
144 bool operator==(Raster const&) const = default;
145
146private:
147 void setCellSize(double cell_size);
148 void setNoDataVal(double no_data_val);
149
151 std::vector<double> _raster_data;
152};
153
155{
156 std::string raster_name;
157 std::unique_ptr<GeoLib::Raster> raster;
158};
159
160} // 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:49
Raster & operator=(Raster &&)=default
double & operator()(std::size_t const row, std::size_t const col)
Definition Raster.h:126
Raster(Raster &&)=default
GeoLib::RasterHeader _header
Definition Raster.h:150
std::vector< double > _raster_data
Definition Raster.h:151
std::vector< double >::const_iterator begin() const
Definition Raster.h:96
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:62
double const & operator()(std::size_t const row, std::size_t const col) const
Definition Raster.h:116
void setNoDataVal(double no_data_val)
void refineRaster(std::size_t scaling)
Definition Raster.cpp:25
bool operator==(Raster const &) const =default
double getValueAtPoint(const MathLib::Point3d &pnt) const
Definition Raster.cpp:56
double const * data() const
Definition Raster.h:111
bool isPntOnRaster(MathLib::Point3d const &pnt) const
Definition Raster.cpp:151
Raster & operator=(Raster const &)=default
double interpolateValueAtPoint(const MathLib::Point3d &pnt) const
Definition Raster.cpp:85
RasterHeader const & getHeader() const
Returns the complete header information.
Definition Raster.h:85
std::vector< double >::const_iterator end() const
Definition Raster.h:106
void setCellSize(double cell_size)
Raster(Raster const &)=default
~Raster()=default
std::unique_ptr< GeoLib::Raster > raster
Definition Raster.h:157
std::string raster_name
Definition Raster.h:156
Contains the relevant information when storing a geoscientific raster data.
Definition Raster.h:28
bool operator==(RasterHeader const &) const =default
std::size_t n_depth
Definition Raster.h:31
MathLib::Point3d origin
Definition Raster.h:32
std::size_t n_cols
Definition Raster.h:29
std::size_t n_rows
Definition Raster.h:30