FireSTARR
Loading...
Searching...
No Matches
ConstantGrid.h
1/* SPDX-License-Identifier: AGPL-3.0-or-later */
2#ifndef FS_CONSTANTGRID_H
3#define FS_CONSTANTGRID_H
4#include "stdafx.h"
5#include "Grid.h"
6#include "Location.h"
7#include "Util.h"
8namespace fs
9{
10template <class T, class V>
11class ConstantGrid;
12;
13[[nodiscard]] ConstantGrid<int, int> readTiffInt(const string_view filename, const Point& point);
19template <class T, class V = T>
20class ConstantGrid : public GridData<T, V, vector<T>>
21{
22public:
23 ConstantGrid() = default;
24 [[nodiscard]] constexpr T at(const XYIdx& location) const noexcept override
25 {
26 // constant grids would use index not hash
27 return this->data.at(to_index(location));
28 }
32 // ! @cond Doxygen_Suppress
33 void set(const XYIdx&, const T) override
34 // ! @endcond
35 {
36 throw runtime_error("Cannot change ConstantGrid");
37 }
38 ~ConstantGrid() override = default;
39 ConstantGrid(const ConstantGrid& rhs) noexcept = default;
40 ConstantGrid(ConstantGrid&& rhs) noexcept = default;
41 ConstantGrid& operator=(const ConstantGrid& rhs) noexcept = default;
42 ConstantGrid& operator=(ConstantGrid&& rhs) noexcept = default;
44 const MathSize cell_size,
45 const Idx width,
46 const Idx height,
47 const V nodata_input,
48 const T nodata_value,
49 const MathSize xllcorner,
50 const MathSize yllcorner,
51 const MathSize xurcorner,
52 const MathSize yurcorner,
53 const string_view proj4,
54 vector<T>&& data
55 )
56 : GridData<T, V, vector<T>>(
57 cell_size,
58 width,
59 height,
60 nodata_input,
61 nodata_value,
66 proj4,
67 std::move(data)
68 )
69 { }
71 const MathSize cell_size,
72 const Idx width,
73 const Idx height,
74 const V nodata_input,
75 const T nodata_value,
76 const MathSize xllcorner,
77 const MathSize yllcorner,
78 const string_view proj4,
79 const T& initialization_value
80 ) noexcept
82 cell_size,
83 width,
84 height,
85 nodata_input,
86 nodata_value,
89 proj4,
90 std::move(vector<T>(static_cast<size_t>(MAX_HEIGHT) * MAX_WIDTH, initialization_value))
91 )
92 { }
100 [[nodiscard]] static ConstantGrid<T, V> readTiff(
101 const string_view filename,
102 const Point& point,
103 std::function<T(V, V)> convert
104 )
105 {
106 logging::debug("Reading a raster where T = {:s}, V = {:s}", typeid(T).name(), typeid(V).name());
107 auto grid = readTiffInt(filename, point);
108 const auto nodata_input = grid.nodataInput();
109 T nodata_value = convert(nodata_input, nodata_input);
110 logging::check_fatal(
111 convert(nodata_input, nodata_input) != nodata_value,
112 "Expected nodata value to be returned from convert()"
113 );
114 vector<T> values(static_cast<size_t>(MAX_HEIGHT) * MAX_WIDTH, nodata_value);
115 std::transform(grid.data.begin(), grid.data.end(), values.begin(), [&](const int v) {
116 return convert(static_cast<V>(v), nodata_input);
117 });
118 ConstantGrid<T, V> result{
119 grid.cellSize(),
120 grid.width(),
121 grid.height(),
122 static_cast<V>(grid.nodataInput()),
123 nodata_value,
124 grid.xllcorner(),
125 grid.yllcorner(),
126 grid.xurcorner(),
127 grid.yurcorner(),
128 grid.proj4(),
129 std::move(values)
130 };
131 return result;
132 }
139 [[nodiscard]] static ConstantGrid<T, T> readTiff(const string_view filename, const Point& point)
140 {
141 return readTiff(filename, point, no_convert<T>);
142 }
143
144protected:
145 tuple<Idx, Idx, Idx, Idx> dataBounds() const override
146 {
147 const Idx min_y = 0;
148 const Idx max_y = this->height();
149 const Idx min_x = 0;
150 const Idx max_x = this->width();
151 return {min_x, min_y, max_x, max_y};
152 }
153
154private:
163 const GridBase& grid_info,
164 const V nodata_input,
165 const T nodata_value,
166 vector<T>&& values
167 )
168 : ConstantGrid(
169 grid_info.cellSize(),
170 nodata_input,
171 nodata_value,
172 grid_info.xllcorner(),
173 grid_info.yllcorner(),
174 grid_info.xurcorner(),
175 grid_info.yurcorner(),
176 string(grid_info.proj4()),
177 std::move(values)
178 )
179 {
180#ifdef DEBUG_GRIDS
181 logging::check_fatal(
182 this->data.size() != static_cast<size_t>(MAX_HEIGHT) * MAX_WIDTH, "Invalid grid size"
183 );
184#endif
185 }
186};
187class FuelType;
188class Cell;
189using FuelGrid = ConstantGrid<const FuelType*, FuelSize>;
190using ElevationGrid = ConstantGrid<ElevationSize>;
191using CellGrid = ConstantGrid<Cell, SpreadKey>;
192}
193#endif
A GridData<T, V, vector<T>> that cannot change once initialized.
Definition ConstantGrid.h:21
~ConstantGrid() override=default
Throw an error because ConstantGrid can't change values.
ConstantGrid(const GridBase &grid_info, const V nodata_input, const T nodata_value, vector< T > &&values)
Constructor.
Definition ConstantGrid.h:162
static ConstantGrid< T, V > readTiff(const string_view filename, const Point &point, std::function< T(V, V)> convert)
Read a section of a TIFF into a ConstantGrid.
Definition ConstantGrid.h:100
static ConstantGrid< T, T > readTiff(const string_view filename, const Point &point)
Read a section of a TIFF into a ConstantGrid.
Definition ConstantGrid.h:139
The base class with information for a grid of data with geographic coordinates.
Definition Grid.h:23
constexpr const string & proj4() const noexcept
Proj4 string defining coordinate system for this grid. Must be a UTM projection.
Definition Grid.h:67
constexpr MathSize xllcorner() const noexcept
Lower left corner X coordinate in metres.
Definition Grid.h:47
constexpr MathSize xurcorner() const noexcept
Upper right corner X coordinate in metres.
Definition Grid.h:57
constexpr MathSize yllcorner() const noexcept
Lower left corner Y coordinate in metres.
Definition Grid.h:52
constexpr MathSize yurcorner() const noexcept
Upper right corner Y coordinate in metres.
Definition Grid.h:62
constexpr MathSize cellSize() const noexcept
Cell size used for GridBase.
Definition Grid.h:34
A Grid that defines the data structure used for storing values.
Definition Grid.h:267
vector< T > data
Definition Grid.h:327
A geographic location in lat/long coordinates.
Definition Point.h:11
Definition Location.h:81