FireSTARR
Loading...
Searching...
No Matches
GridMap.h
1/* SPDX-License-Identifier: AGPL-3.0-or-later */
2#ifndef FS_GRIDMAP_H
3#define FS_GRIDMAP_H
4#include "stdafx.h"
5#include "Grid.h"
6#include "Location.h"
7namespace fs
8{
14template <class T, class V = T>
15class GridMap : public GridData<T, V, map<XYIdx, T>>
16{
17public:
18 [[nodiscard]] bool contains(const XYIdx& location) const
19 {
20 return this->data.end() != this->data.find(location);
21 }
22 [[nodiscard]] T at(const XYIdx& location) const override
23 {
24 const auto value = this->data.find(location);
25 if (value == this->data.end())
26 {
27 return this->nodataValue();
28 }
29 return get<1>(*value);
30 }
36 void set(const XYIdx& location, const T value) override
37 {
38 this->data[location] = value;
39 assert(at(location) == value);
40 }
41 GridMap() noexcept = default;
42 ~GridMap() override = default;
43 GridMap(
44 const MathSize cell_size,
45 const Idx width,
46 const Idx height,
47 T no_data,
48 const int nodata,
49 const MathSize xllcorner,
50 const MathSize yllcorner,
51 const MathSize xurcorner,
52 const MathSize yurcorner,
53 const string_view proj4
54 )
55 : GridData<T, V, map<XYIdx, T>>(
56 cell_size,
57 width,
58 height,
59 no_data,
60 nodata,
65 proj4,
66 map<XYIdx, T>()
67 )
68 {
69 constexpr auto max_hash = numeric_limits<HashSize>::max();
70 // HACK: we don't want overflow errors, but we want to play with the hash size
71 const auto MAX_WIDTH = static_cast<MathSize>(max_hash) / static_cast<MathSize>(this->height());
72 logging::check_fatal(
73 this->width() >= MAX_WIDTH,
74 "Grid is too big for cells to be hashed - "
75 "recompile with a larger HashSize value"
76 );
77#ifdef DEBUG_GRIDS
78 // enforce converting to an int and back produces same V
79 const auto n0 = this->nodataInput();
80 const auto n1 = static_cast<NodataIntType>(n0);
81 const auto n2 = static_cast<V>(n1);
82 const auto n3 = static_cast<NodataIntType>(n2);
83 logging::check_equal(n1, n3, "nodata_input_ as int");
84 logging::check_equal(n0, n2, "nodata_input_ from int");
85#endif
86 }
91 explicit GridMap(const Grid<T, V>& grid)
92 : GridMap<T, V>(
93 grid.cellSize(),
94 grid.noData(),
95 grid.nodata(),
96 grid.xllcorner(),
97 grid.yllcorner(),
98 grid.xurcorner(),
99 grid.yurcorner(),
100 grid.proj4()
101 )
102 { }
108 GridMap(const GridBase& grid_info, T no_data)
109 : GridMap<T, V>(
110 grid_info.cellSize(),
111 static_cast<Idx>(grid_info.calculateWidth()),
112 static_cast<Idx>(grid_info.calculateHeight()),
113 no_data,
114 static_cast<int>(no_data),
115 grid_info.xllcorner(),
116 grid_info.yllcorner(),
117 grid_info.xurcorner(),
118 grid_info.yurcorner(),
119 string(grid_info.proj4())
120 )
121 { }
122 GridMap(GridMap&& rhs) noexcept : GridData<T, V, map<XYIdx, T>>(std::move(rhs))
123 {
124 this->data = std::move(rhs.data);
125 }
126 GridMap(const GridMap& rhs) : GridData<T, V, map<XYIdx, T>>(rhs) { this->data = rhs.data; }
127 GridMap& operator=(GridMap&& rhs) noexcept
128 {
129 if (this != &rhs)
130 {
131 this->data = std::move(rhs.data);
132 }
133 return *this;
134 }
135 GridMap& operator=(const GridMap& rhs)
136 {
137 if (this != &rhs)
138 {
139 this->data = rhs.data;
140 }
141 return *this;
142 }
146 void clear() noexcept { this->data = {}; }
147
148protected:
149 tuple<Idx, Idx, Idx, Idx> dataBounds() const override
150 {
151 Idx min_y = this->height();
152 Idx max_y = 0;
153 Idx min_x = this->width();
154 Idx max_x = 0;
155 for (const auto& kv : this->data)
156 {
157 const Idx r = kv.first.y_value();
158 const Idx c = kv.first.x_value();
159 min_y = min(min_y, r);
160 max_y = max(max_y, r);
161 min_x = min(min_x, c);
162 max_x = max(max_x, c);
163 }
164 // do this so that we take the center point when there's no data since it should
165 // stay the same if the grid is centered on the fire
166 if (min_y > max_y)
167 {
168 min_y = max_y = this->height() / 2;
169 }
170 if (min_x > max_x)
171 {
172 min_x = max_x = this->width() / 2;
173 }
174 return {min_x, min_y, max_x, max_y};
175 }
176
177public:
185 template <class R>
187 const string_view dir,
188 const string_view base_name,
189 const R divisor
190 ) const
191 {
192 auto div = [=](T value) -> R { return static_cast<R>(value / divisor); };
193 return this->template saveToFile<R>(dir, base_name, div);
194 }
199 [[nodiscard]] MathSize fireSize() const noexcept
200 {
201 // we know that every cell is a key, so we convert that to hectares
202 const MathSize per_width = (this->cellSize() / 100.0);
203 // cells might have 0 as a value, but those shouldn't affect size
204 return static_cast<MathSize>(this->data.size()) * per_width * per_width;
205 }
210 [[nodiscard]] list<XYIdx> makeEdge() const
211 {
212 list<XYIdx> edge{};
213 for (const auto& kv : this->data)
214 {
215 auto loc = kv.first;
216 // FIX: any way to avoid decomposing?
217 auto [x_loc, y_loc] = hash_to_xy(loc);
218 auto on_edge = false;
219 for (Idx r = -1; !on_edge && r <= 1; ++r)
220 {
221 const Idx y = y_loc.value + r;
222 if (!(y < 0 || y >= this->height()))
223 {
224 for (Idx c = -1; !on_edge && c <= 1; ++c)
225 {
226 const Idx x = x_loc.value + c;
227 if (!(x < 0 || x >= this->width()) && this->data.find(XYIdx{x, y}) == this->data.end())
228 {
229 on_edge = true;
230 }
231 }
232 }
233 }
234 if (on_edge)
235 {
236 edge.push_back(loc);
237 }
238 }
239 logging::info(
240 "Created edge for perimeter with length {:d} m",
241 static_cast<size_t>(this->cellSize() * edge.size())
242 );
243 return edge;
244 }
249 [[nodiscard]] list<XYIdx> makeList() const
250 {
251 list<XYIdx> result{this->data.size()};
252 std::transform(
253 this->data.begin(),
254 this->data.end(),
255 result.begin(),
256 [](const pair<const XYIdx, const T>& kv) { return kv.first; }
257 );
258 return result;
259 }
260};
261}
262#endif
Definition Util.h:10
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
FileList saveToFile(const string_view dir, const string_view base_name, std::function< R(T value)> convert, const R no_data) const
Definition Grid.h:537
map< XYIdx, T > data
Definition Grid.h:327
A GridData that uses an unordered_map for storage.
Definition GridMap.h:16
list< XYIdx > makeEdge() const
Make a list of all Locations that are on the edge of cells with a value.
Definition GridMap.h:210
GridMap(const Grid< T, V > &grid)
Construct empty GridMap with same extent as given Grid.
Definition GridMap.h:91
void clear() noexcept
Clear data from GridMap.
Definition GridMap.h:146
FileList saveToProbabilityFile(const string_view dir, const string_view base_name, const R divisor) const
Save GridMap contents to .asc file as probability.
Definition GridMap.h:186
GridMap(const GridBase &grid_info, T no_data)
Construct empty GridMap with same extent as given Grid.
Definition GridMap.h:108
MathSize fireSize() const noexcept
Calculate area for cells that have a value (ha)
Definition GridMap.h:199
void set(const XYIdx &location, const T value) override
Set value at Location.
Definition GridMap.h:36
list< XYIdx > makeList() const
Make a list of all Locations that have a value.
Definition GridMap.h:249
A GridBase with an associated type of data.
Definition Grid.h:175
constexpr T nodataInput() const noexcept
Definition Grid.h:183
constexpr T nodataValue() const noexcept
Definition Grid.h:188
Definition Location.h:81