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
35 [[nodiscard]]
constexpr FullIdx calculateHeight() const noexcept
39 [[nodiscard]]
constexpr FullIdx calculateWidth() const noexcept
67 [[nodiscard]]
constexpr const string&
proj4() const noexcept {
return proj4_; }
83 const string_view
proj4
91 void createPrj(const string_view dir, const string_view base_name) const;
107 [[nodiscard]]
bool validate(const
Point& point) const;
108 string saveToTiffFileInt(
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
119 string saveToTiffFileFloat(
122 const tuple<Idx, Idx, Idx, Idx> bounds,
123 const string_view dir,
124 const string_view base_name,
127 std::function<
double(const
XYIdx&)> value_at,
128 const
int nodata_as_int
157void write_ascii_header(
166GridBase read_header(GeoTiff& geotiff);
167GridBase read_header(
const string_view filename);
173template <
class T,
class V = T>
177 [[nodiscard]]
constexpr Idx height()
const noexcept {
return height_; }
178 [[nodiscard]]
constexpr Idx width()
const noexcept {
return width_; }
192 [[nodiscard]]
virtual T at(
const XYIdx& location)
const = 0;
194 virtual void set(
const XYIdx& location, T value) = 0;
199 const MathSize cell_size,
202 const V nodata_input,
203 const T nodata_value,
208 const string_view
proj4
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);
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");
234 grid_info.cellSize(),
235 static_cast<Idx
>(grid_info.calculateHeight()),
236 static_cast<Idx
>(grid_info.calculateWidth()),
239 grid_info.xllcorner(),
240 grid_info.yllcorner(),
241 grid_info.xurcorner(),
242 grid_info.yurcorner(),
265template <
class T,
class V,
class D>
269 using Grid<T, V>::Grid;
271 const MathSize cell_size,
274 const V nodata_input,
275 const T nodata_value,
280 const string_view
proj4,
322 [[nodiscard]]
size_t size()
const {
return data.size(); }
330 virtual tuple<Idx, Idx, Idx, Idx> dataBounds()
const = 0;
341 const string_view dir,
342 const string_view base_name,
343 std::function<R(T value)> convert,
347 const auto [min_x, min_y, max_x, max_y] = dataBounds();
354 logging::debug(
"Bounds are ({:d}, {:d}), ({:d}, {:d})", min_x, min_y, max_x, max_y);
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);
361 logging::extensive(
"Lower left corner is ({:f}, {:f})", xll, yll);
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};
368 out, width_calc, height_calc, xll, yll, this->
cellSize(),
static_cast<MathSize
>(no_data)
370 for (Idx y0 = 0; y0 < height_calc; ++y0)
374 const Idx y1 =
static_cast<Idx
>(max_y) - y0;
375 for (Idx x0 = 0; x0 < width_calc; ++x0)
377 const XYIdx idx{
static_cast<Idx
>(min_x + x0),
static_cast<Idx
>(y1)};
380 out << +(convert(this->at(idx))) <<
" ";
397 const string_view dir,
398 const string_view base_name,
399 std::function<R(T value)> convert,
402 requires(std::is_floating_point_v<R>)
414 return GridBase::saveToTiffFileFloat(
422 [&](
const XYIdx& idx) {
return static_cast<double>(convert(this->at(idx))); },
428 const string_view dir,
429 const string_view base_name,
430 std::function<R(T value)> convert,
433 requires(std::is_integral_v<R>)
435 auto bps =
sizeof(R) * 8;
441 return GridBase::saveToTiffFileInt(
448 std::is_unsigned_v<R>,
449 [&](
const XYIdx& idx) {
return static_cast<int>(convert(this->at(idx))); },
466 const string_view dir,
467 const string_view base_name,
468 std::function<R(T value)> convert,
472 static const auto& settings = settings::instance();
475 if (settings.save_as_ascii)
477 files.emplace_back(this->
template saveToAsciiFile<R>(dir, base_name, convert, no_data));
480 if (!settings.save_as_ascii || settings.save_as_tiff)
482 files.emplace_back(this->
template saveToTiffFile<R>(dir, base_name, convert, no_data));
497 const string_view dir,
498 const string_view base_name,
499 std::function<R(T value)> convert,
509 catch (
const std::exception& err)
512 "Error trying to write {:s} to {:s} so retrying\n{:s}", base_name, dir, err.what()
518 catch (
const std::exception& err_fatal)
522 "Error trying to write {:s} to {:s}\n{:s}", base_name, dir, err_fatal.what()
538 const string_view dir,
539 const string_view base_name,
540 std::function<R(T value)> convert,
556 const string_view dir,
557 const string_view base_name,
558 std::function<R(T value)> convert
562 dir, base_name, convert,
static_cast<R
>(this->
nodataInput())
571 template <
class R = V>
574 return this->
template saveToFile<R>(dir, base_name, [](T value) -> R {
575 return static_cast<R
>(value);
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