FireSTARR
Loading...
Searching...
No Matches
Grid.h
1/* SPDX-License-Identifier: AGPL-3.0-or-later */
2#ifndef FS_GRID_H
3#define FS_GRID_H
4#include "stdafx.h"
5#include "Location.h"
6#include "Log.h"
7#include "Point.h"
8#include "Settings.h"
9#include "tiff.h"
10namespace fs
11{
12using namespace logging;
13using NodataIntType = int64_t;
14string create_file_name(
15 const string_view dir,
16 const string_view base_name,
17 const string_view extension
18);
23{
24public:
25 virtual ~GridBase() = default;
26 GridBase(GridBase&& rhs) noexcept = default;
27 GridBase(const GridBase& rhs) = default;
28 GridBase& operator=(const GridBase& rhs) = default;
29 GridBase& operator=(GridBase&& rhs) noexcept = default;
34 [[nodiscard]] constexpr MathSize cellSize() const noexcept { return cell_size_; }
35 [[nodiscard]] constexpr FullIdx calculateHeight() const noexcept
36 {
37 return static_cast<FullIdx>((yurcorner() - yllcorner()) / cellSize()) - 1;
38 }
39 [[nodiscard]] constexpr FullIdx calculateWidth() const noexcept
40 {
41 return static_cast<FullIdx>((xurcorner() - xllcorner()) / cellSize()) - 1;
42 }
47 [[nodiscard]] constexpr MathSize xllcorner() const noexcept { return xllcorner_; }
52 [[nodiscard]] constexpr MathSize yllcorner() const noexcept { return yllcorner_; }
57 [[nodiscard]] constexpr MathSize xurcorner() const noexcept { return xurcorner_; }
62 [[nodiscard]] constexpr MathSize yurcorner() const noexcept { return yurcorner_; }
67 [[nodiscard]] constexpr const string& proj4() const noexcept { return proj4_; }
78 MathSize cell_size,
79 MathSize xllcorner,
80 MathSize yllcorner,
81 MathSize xurcorner,
82 MathSize yurcorner,
83 const string_view proj4
84 ) noexcept;
85 GridBase() noexcept = default;
91 void createPrj(const string_view dir, const string_view base_name) const;
98 [[nodiscard]] std::optional<Coordinates> findCoordinates(const Point& point, bool flipped) const;
105 [[nodiscard]] std::optional<FullCoordinates> findFullCoordinates(const Point& point, bool flipped)
106 const;
107 [[nodiscard]] bool validate(const Point& point) const;
108 string saveToTiffFileInt(
109 const Idx width,
110 const Idx height,
111 const tuple<Idx, Idx, Idx, Idx> bounds,
112 const string_view dir,
113 const string_view base_name,
114 const uint16_t bits_per_sample,
115 const bool is_unsigned,
116 std::function<int(const XYIdx&)> value_at,
117 const int nodata_as_int
118 ) const;
119 string saveToTiffFileFloat(
120 const Idx width,
121 const Idx height,
122 const tuple<Idx, Idx, Idx, Idx> bounds,
123 const string_view dir,
124 const string_view base_name,
125 // const uint16_t bits_per_sample,
126 // const uint16_t sample_format,
127 std::function<double(const XYIdx&)> value_at,
128 const int nodata_as_int
129 ) const;
130
131private:
135 string proj4_{};
139 MathSize cell_size_{-1};
143 MathSize xllcorner_{-1};
147 MathSize yllcorner_{-1};
151 MathSize xurcorner_{-1};
155 MathSize yurcorner_{-1};
156};
157void write_ascii_header(
158 ofstream& out,
159 MathSize width,
160 MathSize height,
161 MathSize xll,
162 MathSize yll,
163 MathSize cell_size,
164 MathSize no_data
165);
166GridBase read_header(GeoTiff& geotiff);
167GridBase read_header(const string_view filename);
173template <class T, class V = T>
174class Grid : public GridBase
175{
176public:
177 [[nodiscard]] constexpr Idx height() const noexcept { return height_; }
178 [[nodiscard]] constexpr Idx width() const noexcept { return width_; }
183 [[nodiscard]] constexpr V nodataInput() const noexcept { return nodata_input_; }
188 constexpr T nodataValue() const noexcept
189 {
190 return nodata_value_;
191 } // NOTE: only use this for simple types because it's returning by value
192 [[nodiscard]] virtual T at(const XYIdx& location) const = 0;
193 // NOTE: use set instead of at to avoid issues with bool
194 virtual void set(const XYIdx& location, T value) = 0;
195
196protected:
197 Grid() = default;
198 Grid(
199 const MathSize cell_size,
200 const Idx width,
201 const Idx height,
202 const V nodata_input,
203 const T nodata_value,
204 const MathSize xllcorner,
205 const MathSize yllcorner,
206 const MathSize xurcorner,
207 const MathSize yurcorner,
208 const string_view proj4
209 ) noexcept
211 nodata_input_(nodata_input), nodata_value_(nodata_value), height_(height), width_(width)
212 {
213#ifdef DEBUG_GRIDS
214 logging::check_fatal(height > MAX_HEIGHT, "Height too large ({:d} > {:d})", height, MAX_HEIGHT);
215 logging::check_fatal(width > MAX_WIDTH, "Width too large ({:d} > {:d})", width, MAX_WIDTH);
216#endif
217#ifdef DEBUG_GRIDS
218 // enforce converting to an int and back produces same V
219 const auto n0 = this->nodata_input_;
220 const auto n1 = static_cast<NodataIntType>(n0);
221 const auto n2 = static_cast<V>(n1);
222 const auto n3 = static_cast<NodataIntType>(n2);
223 logging::check_equal(n1, n3, "nodata_input_ as int");
224 logging::check_equal(n0, n2, "nodata_input_ from int");
225#endif
226 }
232 Grid(const GridBase& grid_info, V no_data) noexcept
233 : Grid(
234 grid_info.cellSize(),
235 static_cast<Idx>(grid_info.calculateHeight()),
236 static_cast<Idx>(grid_info.calculateWidth()),
237 no_data,
238 to_string(no_data),
239 grid_info.xllcorner(),
240 grid_info.yllcorner(),
241 grid_info.xurcorner(),
242 grid_info.yurcorner(),
243 grid_info.proj4()
244 )
245 { }
246
247private:
256 Idx height_{};
257 Idx width_{};
258};
265template <class T, class V, class D>
266class GridData : public Grid<T, V>
267{
268public:
269 using Grid<T, V>::Grid;
270 GridData(
271 const MathSize cell_size,
272 const Idx width,
273 const Idx height,
274 const V nodata_input,
275 const T nodata_value,
276 const MathSize xllcorner,
277 const MathSize yllcorner,
278 const MathSize xurcorner,
279 const MathSize yurcorner,
280 const string_view proj4,
281 D&& data
282 )
283 : Grid<T, V>(
284 cell_size,
285 width,
286 height,
287 nodata_input,
288 nodata_value,
289 xllcorner,
290 yllcorner,
291 xurcorner,
292 yurcorner,
293 proj4
294 ),
295 data(std::forward<D>(data))
296 { }
297 ~GridData() override = default;
298 GridData(const GridData& rhs) : Grid<T, V>(rhs), data(rhs.data) { }
299 GridData(GridData&& rhs) noexcept : Grid<T, V>(rhs), data(std::move(rhs.data)) { }
300 GridData& operator=(const GridData& rhs) noexcept
301 {
302 if (this != &rhs)
303 {
305 data = rhs.data;
306 }
307 return *this;
308 }
309 GridData& operator=(GridData&& rhs) noexcept
310 {
311 if (this != &rhs)
312 {
314 data = std::move(rhs.data);
315 }
316 return *this;
317 }
322 [[nodiscard]] size_t size() const { return data.size(); }
323 // HACK: use public access so that we can get to the keys
327 D data{};
328
329protected:
330 virtual tuple<Idx, Idx, Idx, Idx> dataBounds() const = 0;
339 template <class R>
341 const string_view dir,
342 const string_view base_name,
343 std::function<R(T value)> convert,
344 const R no_data
345 ) const
346 {
347 const auto [min_x, min_y, max_x, max_y] = dataBounds();
348 // auto bounds = dataBounds();
349 // auto min_x = std::get<0>(bounds);
350 // auto min_y = std::get<1>(bounds);
351 // auto max_x = std::get<2>(bounds);
352 // auto max_y = std::get<3>(bounds);
353#ifdef DEBUG_GRIDS
354 logging::debug("Bounds are ({:d}, {:d}), ({:d}, {:d})", min_x, min_y, max_x, max_y);
355#endif
356 logging::extensive("Lower left corner is ({:d}, {:d})", min_x, min_y);
357 logging::extensive("Upper right corner is ({:d}, {:d})", max_x, max_y);
358 const MathSize xll = this->xllcorner() + min_x * this->cellSize();
359 // offset is different for y since it's flipped
360 const MathSize yll = this->yllcorner() + (min_y) * this->cellSize();
361 logging::extensive("Lower left corner is ({:f}, {:f})", xll, yll);
362 // HACK: make sure it's always at least 1
363 const auto height_calc = static_cast<MathSize>(max_y) - min_y + 1;
364 const auto width_calc = static_cast<MathSize>(max_x) - min_x + 1;
365 const auto filename = create_file_name(dir, base_name, "asc");
366 ofstream out{filename};
367 write_ascii_header(
368 out, width_calc, height_calc, xll, yll, this->cellSize(), static_cast<MathSize>(no_data)
369 );
370 for (Idx y0 = 0; y0 < height_calc; ++y0)
371 {
372 // HACK: do this so that we always get at least one pixel in output
373 // need to output in reverse order since (0,0) is bottom left
374 const Idx y1 = static_cast<Idx>(max_y) - y0;
375 for (Idx x0 = 0; x0 < width_calc; ++x0)
376 {
377 const XYIdx idx{static_cast<Idx>(min_x + x0), static_cast<Idx>(y1)};
378 // HACK: use + here so that it gets promoted to a printable number
379 // prevents char type being output as characters
380 out << +(convert(this->at(idx))) << " ";
381 }
382 out << "\n";
383 }
384 out.close();
385 this->createPrj(dir, base_name);
386 return filename;
387 }
395 template <class R>
397 const string_view dir,
398 const string_view base_name,
399 std::function<R(T value)> convert,
400 const R no_data
401 ) const
402 requires(std::is_floating_point_v<R>)
403 {
404 // constexpr auto bps = 16;
405 // auto bps = sizeof(R) * 8;
406 // logging::check_fatal(
407 // bps != 16, "Only float16_t supported for output tiff but BITSPERSAMPLE is {:d}", bps
408 // );
409 // HACK: output is using internal nodata representation right now, but that shouldn't change
410 // anything FIX: use original input nodata value
411 // logging::check_equal(
412 // static_cast<int>(no_data), static_cast<int>(this->nodataInput()), "integer nodata"
413 // );
414 return GridBase::saveToTiffFileFloat(
415 this->width(),
416 this->height(),
417 this->dataBounds(),
418 dir,
419 base_name,
420 // bps,
421 // SAMPLEFORMAT_IEEEFP,
422 [&](const XYIdx& idx) { return static_cast<double>(convert(this->at(idx))); },
423 no_data
424 );
425 }
426 template <class R>
427 string saveToTiffFile(
428 const string_view dir,
429 const string_view base_name,
430 std::function<R(T value)> convert,
431 const R no_data
432 ) const
433 requires(std::is_integral_v<R>)
434 {
435 auto bps = sizeof(R) * 8;
436 // HACK: output is using internal nodata representation right now, but that shouldn't change
437 // anything FIX: use original input nodata value
438 // logging::check_equal(
439 // static_cast<int>(no_data), static_cast<int>(this->nodataInput()), "integer nodata"
440 // );
441 return GridBase::saveToTiffFileInt(
442 this->width(),
443 this->height(),
444 this->dataBounds(),
445 dir,
446 base_name,
447 bps,
448 std::is_unsigned_v<R>,
449 [&](const XYIdx& idx) { return static_cast<int>(convert(this->at(idx))); },
450 no_data
451 );
452 }
453
454public:
464 template <class R>
466 const string_view dir,
467 const string_view base_name,
468 std::function<R(T value)> convert,
469 const R no_data
470 ) const
471 {
472 static const auto& settings = settings::instance();
473 FileList files{};
474 // NOTE: do this instead of function pointer because it's using templates
475 if (settings.save_as_ascii)
476 {
477 files.emplace_back(this->template saveToAsciiFile<R>(dir, base_name, convert, no_data));
478 }
479 // always save at least something
480 if (!settings.save_as_ascii || settings.save_as_tiff)
481 {
482 files.emplace_back(this->template saveToTiffFile<R>(dir, base_name, convert, no_data));
483 }
484 return files;
485 }
495 template <class R>
497 const string_view dir,
498 const string_view base_name,
499 std::function<R(T value)> convert,
500 const R no_data
501 ) const
502 {
503 // HACK: (hopefully) ensure that write works
504 try
505 {
506 // HACK: use different function name to prevent infinite recursion warning
507 return this->template saveToFileWithoutRetry<R>(dir, base_name, convert, no_data);
508 }
509 catch (const std::exception& err)
510 {
511 logging::error(
512 "Error trying to write {:s} to {:s} so retrying\n{:s}", base_name, dir, err.what()
513 );
514 try
515 {
516 return this->template saveToFileWithoutRetry<R>(dir, base_name, convert, no_data);
517 }
518 catch (const std::exception& err_fatal)
519 {
520 // will exit if not supposed to retry
521 exit(logging::fatal(
522 "Error trying to write {:s} to {:s}\n{:s}", base_name, dir, err_fatal.what()
523 ));
524 }
525 }
526 }
536 template <class R>
537 [[nodiscard]] FileList saveToFile(
538 const string_view dir,
539 const string_view base_name,
540 std::function<R(T value)> convert,
541 const R no_data
542 ) const
543 {
544 return this->template saveToFileWithRetry<R>(dir, base_name, convert, no_data);
545 }
554 template <class R>
555 [[nodiscard]] FileList saveToFile(
556 const string_view dir,
557 const string_view base_name,
558 std::function<R(T value)> convert
559 ) const
560 {
561 return this->template saveToFile<R>(
562 dir, base_name, convert, static_cast<R>(this->nodataInput())
563 );
564 }
571 template <class R = V>
572 [[nodiscard]] FileList saveToFile(const string_view dir, const string_view base_name) const
573 {
574 return this->template saveToFile<R>(dir, base_name, [](T value) -> R {
575 return static_cast<R>(value);
576 });
577 }
578};
579}
580#endif
Definition Util.h:10
The base class with information for a grid of data with geographic coordinates.
Definition Grid.h:23
void createPrj(const string_view dir, const string_view base_name) const
Create .prj file in directory with base name for file.
Definition Grid.cpp:341
std::optional< Coordinates > findCoordinates(const Point &point, bool flipped) const
Find Coordinates for Point.
Definition Grid.cpp:381
MathSize yllcorner_
Lower left corner Y coordinate in metres.
Definition Grid.h:147
MathSize xurcorner_
Upper right corner X coordinate in metres.
Definition Grid.h:151
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
MathSize cell_size_
Cell height and width in metres.
Definition Grid.h:139
constexpr MathSize xurcorner() const noexcept
Upper right corner X coordinate in metres.
Definition Grid.h:57
std::optional< FullCoordinates > findFullCoordinates(const Point &point, bool flipped) const
Find FullCoordinates for Point.
Definition Grid.cpp:506
string proj4_
Proj4 string defining projection.
Definition Grid.h:135
constexpr MathSize yllcorner() const noexcept
Lower left corner Y coordinate in metres.
Definition Grid.h:52
MathSize xllcorner_
Lower left corner X coordinate in metres.
Definition Grid.h:143
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
MathSize yurcorner_
Upper right corner Y coordinate in metres.
Definition Grid.h:155
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
Save GridMap contents to file based on settings.
Definition Grid.h:537
FileList saveToFile(const string_view dir, const string_view base_name) const
Save GridMap contents to file based on settings.
Definition Grid.h:572
FileList saveToFile(const string_view dir, const string_view base_name, std::function< R(T value)> convert) const
Save GridMap contents to file based on settings.
Definition Grid.h:555
string saveToAsciiFile(const string_view dir, const string_view base_name, std::function< R(T value)> convert, const R no_data) const
Save GridMap contents to .asc file.
Definition Grid.h:340
D data
Structure that holds data represented by this GridData.
Definition Grid.h:327
FileList saveToFileWithoutRetry(const string_view dir, const string_view base_name, std::function< R(T value)> convert, const R no_data) const
Save GridMap contents to file based on settings.
Definition Grid.h:465
FileList saveToFileWithRetry(const string_view dir, const string_view base_name, std::function< R(T value)> convert, const R no_data) const
Save GridMap contents to file based on settings.
Definition Grid.h:496
size_t size() const
Size of data structure storing values.
Definition Grid.h:322
string saveToTiffFile(const string_view dir, const string_view base_name, std::function< R(T value)> convert, const R no_data) const
Save GridMap contents to .tif file.
Definition Grid.h:396
A GridBase with an associated type of data.
Definition Grid.h:175
V nodata_input_
Value used to represent no data at a Location.
Definition Grid.h:251
Grid(const GridBase &grid_info, V no_data) noexcept
Construct based on GridBase and no data value.
Definition Grid.h:232
constexpr V nodataInput() const noexcept
Value used for grid locations that have no data.
Definition Grid.h:183
constexpr T nodataValue() const noexcept
Value representing no data.
Definition Grid.h:188
T nodata_value_
Value to use for representing no data at a Location.
Definition Grid.h:255
A geographic location in lat/long coordinates.
Definition Point.h:11
Coordinates ((x, y), (sub-x, sub-y))
Definition stdafx.h:289
FullCoordinates ((x, y), (sub-x, sub-y))
Definition stdafx.h:299
Definition Location.h:81