41template <
typename Key = std::
string,
typename Time =
int>
class landscape
49 using time_type = Time;
52 using location_descriptor =
typename raster<time_type>::location_descriptor;
55 using time_descriptor =
typename raster<time_type>::time_descriptor;
70 using decimal_degree =
typename raster<time_type>::decimal_degree;
73 std::map<key_type, raster<Time>> _variables;
76 landscape(
const std::map<key_type, std::filesystem::path> &files,
const std::vector<time_type> &
times)
80 for (
auto const &it : files)
82 _variables.emplace(std::piecewise_construct, std::forward_as_tuple(it.first),
83 std::forward_as_tuple(it.second,
times));
86 auto have_same_origin = [
this](
auto const &it) {
87 return it.second.origin() == _variables.cbegin()->second.origin();
90 if (!all_of(_variables.cbegin(), _variables.cend(), have_same_origin))
92 throw std::runtime_error(
"all ecological quantities dataset must have same origin");
95 auto have_same_extent = [
this](
auto const &it) {
96 return it.second.get_extent() == _variables.cbegin()->second.get_extent();
99 if (!all_of(_variables.cbegin(), _variables.cend(), have_same_extent))
101 throw std::runtime_error(
"all ecological quantities dataset must have same extent");
104 auto have_same_resolution = [
this](
auto const &it) {
105 return it.second.get_resolution() == _variables.cbegin()->second.get_resolution();
108 if (!all_of(_variables.cbegin(), _variables.cend(), have_same_resolution))
110 throw std::runtime_error(
"all ecological quantities dataset must have same resolution");
113 auto have_same_depth = [
this](
auto const &it) {
114 return it.second.depth() == _variables.cbegin()->second.depth();
117 if (!all_of(_variables.cbegin(), _variables.cend(), have_same_depth))
119 throw std::runtime_error(
"all ecological quantities dataset must have same depth");
136 const std::vector<time_type> &
times)
147 template <
typename Callable>
148 requires std::invocable<Callable, location_descriptor, time_descriptor, std::optional<double>>
149 inline void to_geotiff(Callable f, time_descriptor start, time_descriptor end,
const std::filesystem::path &file)
const
151 _variables.cbegin()->second.to_geotiff(f, start, end, file);
157 inline void to_shapefile(std::vector<latlon> points,
const std::filesystem::path &file)
const
159 _variables.cbegin()->second.to_shapefile(points, file);
165 inline void to_shapefile(std::map<latlon, int> counts,
const std::filesystem::path &file)
const
167 _variables.cbegin()->second.to_shapefile(counts, file);
171 std::ostream &
write(std::ostream &stream)
const
173 stream <<
"Landscape of " << _variables.size() <<
" aligned rasters:\n";
175 for (
const auto &it : _variables)
176 stream << it.first <<
" ";
178 stream <<
"\nOrigin: " <<
origin() <<
"\nWidth: " <<
width() <<
"\nHeight: " <<
height()
179 <<
"\nDepth: " <<
depth() <<
"\nResolution: "
195 return _variables.size();
216 return _variables.at(key);
225 return _variables.at(key);
236 return _variables.cbegin()->second.origin();
242 return _variables.cbegin()->second.get_resolution();
248 return _variables.cbegin()->second.get_extent();
254 return _variables.cbegin()->second.width();
260 return _variables.cbegin()->second.height();
266 return _variables.cbegin()->second.depth();
278 return _variables.cbegin()->second.locations();
285 return _variables.cbegin()->second.times();
291 inline bool is_valid(location_descriptor x)
const noexcept
293 return _variables.cbegin()->second.is_valid(x);
299 inline bool is_valid(
const colrow &x)
const noexcept
301 return _variables.cbegin()->second.is_valid(x);
307 inline bool is_valid(
const rowcol &x)
const noexcept
309 return _variables.cbegin()->second.is_valid(x);
315 inline bool contains(
const latlon &x)
const noexcept
317 return _variables.cbegin()->second.contains(x);
323 inline bool contains(
const lonlat &x)
const noexcept
325 return _variables.cbegin()->second.contains(x);
331 inline bool is_valid(time_descriptor t)
const noexcept
333 return _variables.cbegin()->second.is_valid(t);
341 return _variables.cbegin()->second.is_recorded(t);
349 return _variables.cbegin()->second.is_in_interval(t);
364 return _variables.cbegin()->second.to_descriptor(x);
374 return _variables.cbegin()->second.to_descriptor(x);
384 return _variables.cbegin()->second.to_colrow(x);
394 return _variables.cbegin()->second.to_rowcol(x);
401 inline colrow
to_colrow(location_descriptor x)
const noexcept
404 return _variables.cbegin()->second.to_colrow(x);
411 inline latlon
to_latlon(location_descriptor x)
const noexcept
414 return _variables.cbegin()->second.to_latlon(x);
421 inline lonlat
to_lonlat(location_descriptor x)
const noexcept
424 return _variables.cbegin()->second.to_lonlat(x);
434 return _variables.cbegin()->second.to_latlon(x);
444 return _variables.cbegin()->second.to_lonlat(x);
455 return _variables.cbegin()->second.to_centroid(x);
461template <
typename Key,
typename Time> std::ostream &
operator<<(std::ostream &os,
const landscape<Key, Time> &l)
extent of a raster grid object
Definition extent.hpp:19
Discrete spatio-temporal variations of a set of environmental variables.
Definition landscape.hpp:42
colrow to_colrow(location_descriptor x) const noexcept
Column and row of the cell to which the given descriptor belongs.
Definition landscape.hpp:401
static landscape from_file(const std::map< key_type, std::filesystem::path > &files, const std::vector< time_type > ×)
Read the rasters from a set of input geotiff files.
Definition landscape.hpp:135
const raster< time_type > & operator[](const key_type &key) const
Returns a const reference to the mapped raster with key equivalent to key. If no such raster exists,...
Definition landscape.hpp:214
auto num_locations() const noexcept
Number of cells in the raster.
Definition landscape.hpp:200
latlon origin() const noexcept
Origin of the spatial grid.
Definition landscape.hpp:234
lonlat to_lonlat(location_descriptor x) const noexcept
Longitude and latitude of the cell to which the given coordinate belongs.
Definition landscape.hpp:421
auto times() const noexcept
Time descriptors (unique identifiers) of the dataset bands.
Definition landscape.hpp:283
bool is_recorded(const time_type &t) const noexcept
Search for the exact time in the list of time points recorded by the raster.
Definition landscape.hpp:339
bool is_valid(location_descriptor x) const noexcept
Check if a descriptor describes a valid location of the spatial grid.
Definition landscape.hpp:291
std::ostream & write(std::ostream &stream) const
Landscape is streamable.
Definition landscape.hpp:171
latlon to_latlon(location_descriptor x) const noexcept
Latitude and longitude of the cell to which the given coordinate belongs.
Definition landscape.hpp:411
resolution< decimal_degree > get_resolution() const noexcept
Resolution of the spatial grid.
Definition landscape.hpp:240
void to_geotiff(Callable f, time_descriptor start, time_descriptor end, const std::filesystem::path &file) const
Export a spatio-temporal raster to a Geotiff file.
Definition landscape.hpp:149
int height() const noexcept
Height of the spatial grid.
Definition landscape.hpp:258
auto num_variables() const noexcept
Retrieves the number of variables (rasters)
Definition landscape.hpp:193
raster< time_type > & operator[](const key_type &key)
Returns a reference to the mapped raster with key equivalent to key. If no such raster exists,...
Definition landscape.hpp:223
location_descriptor to_descriptor(const latlon &x) const noexcept
Location descriptor of the cell to which the given coordinate belongs.
Definition landscape.hpp:371
bool contains(const lonlat &x) const noexcept
Check if the raster contains a coordinate.
Definition landscape.hpp:323
bool is_valid(const rowcol &x) const noexcept
Check if the coordinate describes a valid location of the spatial grid.
Definition landscape.hpp:307
latlon to_centroid(const latlon &x) const
Reprojects a coordinate to the centroid of the cell it belongs.
Definition landscape.hpp:452
colrow to_colrow(const latlon &x) const noexcept
Column and row of the cell to which the given coordinate belongs.
Definition landscape.hpp:381
int depth() const noexcept
Depth of the spatial grid.
Definition landscape.hpp:264
extent< decimal_degree > get_extent() const noexcept
Extent of the spatial grid.
Definition landscape.hpp:246
location_descriptor to_descriptor(const colrow &x) const noexcept
Location descriptor of the cell identified by its column/row.
Definition landscape.hpp:361
bool contains(const latlon &x) const noexcept
Check if the raster contains a coordinate.
Definition landscape.hpp:315
lonlat to_lonlat(const colrow &x) const noexcept
Longitude and latitude of the deme identified by its column/row.
Definition landscape.hpp:441
auto locations() const noexcept
Location descriptors (unique identifiers) of the grid cells.
Definition landscape.hpp:276
void to_shapefile(std::vector< latlon > points, const std::filesystem::path &file) const
Export spatial points to a shapefile.
Definition landscape.hpp:157
void to_shapefile(std::map< latlon, int > counts, const std::filesystem::path &file) const
Export geolocalized counts as spatial-points to a shapefile.
Definition landscape.hpp:165
bool is_valid(const colrow &x) const noexcept
Check if the coordinate describes a valid location of the spatial grid.
Definition landscape.hpp:299
latlon to_latlon(const colrow &x) const noexcept
Latitude and longitude of the cell identified by its column/row.
Definition landscape.hpp:431
bool is_in_interval(const time_type &t) const noexcept
Check if the exact time point falls between the first and last date of record.
Definition landscape.hpp:347
int width() const noexcept
Width of the spatial grid.
Definition landscape.hpp:252
rowcol to_rowcol(const latlon &x) const noexcept
Row and column of the cell to which the given coordinate belongs.
Definition landscape.hpp:391
bool is_valid(time_descriptor t) const noexcept
Check if the time descriptor is a valid index.
Definition landscape.hpp:331
Discrete spatio-temporal variations of an environmental variable.
Definition raster.hpp:38
Resolution of a spatial grid.
Definition resolution.hpp:16
constexpr value_type lat() const noexcept
Gets latitude resolution.
Definition resolution.hpp:35
constexpr value_type lon() const noexcept
Gets longitude resolution.
Definition resolution.hpp:41
Geospatial data formatting and processing.
Definition geography.hpp:17
std::ostream & operator<<(std::ostream &stream, const C &c)
GridCoordinates are streamable.
Definition coordinates.hpp:51
Grid coordinates.
Definition coordinates.hpp:82
Geographic coordinates.
Definition coordinates.hpp:179
Geographic coordinates.
Definition coordinates.hpp:218
Grid coordinates.
Definition coordinates.hpp:61