MeteoIODoc 20241221.207bde49
Grid2DObject.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2009 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
4/***********************************************************************************/
5/* This file is part of MeteoIO.
6 MeteoIO is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 MeteoIO is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef GRID2DOBJECT_H
20#define GRID2DOBJECT_H
21
25
26#include <iostream>
27
28namespace mio {
29
30class IOInterface;
31class GridsManager;
32
43 friend class IOInterface;
44 friend class GridsManager;
45
46 public:
48 typedef struct GRID_POINT_2D {
49 size_t ix;
50 size_t iy;
52
53 double& operator ()(const size_t& ix, const size_t& iy);
54 double operator ()(const size_t& ix, const size_t& iy) const;
55 double& operator ()(const size_t& i);
56 double operator ()(const size_t& i) const;
57
58 const std::string toString() const;
59 friend std::ostream& operator<<(std::ostream& os, const Grid2DObject& grid);
60 friend std::istream& operator>>(std::istream& is, Grid2DObject& grid);
61
67 Grid2DObject(const size_t& ncols, const size_t& nrows,
68 const double& cellsize, const Coords& i_llcorner);
69
70 Grid2DObject(const size_t& ncols, const size_t& nrows,
71 const double& cellsize, const Coords& i_llcorner, const double& init);
72
73 Grid2DObject(const Grid2DObject& i_grid, const double& init);
74
75 Grid2DObject(const double& cellsize, const Coords& i_llcorner, const Array2D<double>& grid2D_in);
76
85 Grid2DObject(const Grid2DObject& i_grid2Dobj,
86 const size_t& i_nx, const size_t& i_ny, //Point in the plane
87 const size_t& i_ncols, const size_t& i_nrows); //dimensions of the sub-plane
88
97 bool gridify(Coords& point) const;
98
109 bool gridify(std::vector<Coords>& vec_points, const bool& keep_invalid=false) const;
110 bool gridify(std::vector<StationData>& vec_points, const bool& keep_invalid=false) const;
111
119 void set(const size_t& ncols, const size_t& nrows,
120 const double& cellsize, const Coords& i_llcorner);
128 void set(const double& cellsize, const Coords& i_llcorner, const Array2D<double>& grid2D_in);
129
130 void set(const size_t& ncols, const size_t& nrows,
131 const double& cellsize, const Coords& i_llcorner, const double& init);
132
133 void set(const Grid2DObject& i_grid, const double& init);
134
139 void rescale(const double& i_cellsize);
140
145 void compute_spatial_mean(const double& radius);
146
147 void size(size_t& o_ncols, size_t& o_nrows) const;
148 size_t size() const;
149 size_t getNx() const;
150 size_t getNy() const;
151 double getMin() const;
152 double getMax() const;
153 double getMean() const;
154
158 void clear();
159
164 bool empty() const;
165
166 bool allNodata() const;
167
175 bool isSameGeolocalization(const Grid2DObject& target) const;
176
187 void binning(const std::vector<double>& thresholds, const std::vector<double>& ids);
188
194 std::vector< double > extractPoints(const std::vector< std::pair<size_t, size_t> >& Pts) const;
195
196 Grid2DObject& operator=(const double& value);
197
198 Grid2DObject& operator+=(const double& rhs);
199 const Grid2DObject operator+(const double& rhs) const;
201 const Grid2DObject operator+(const Grid2DObject& rhs) const;
202
203 Grid2DObject& operator-=(const double& rhs);
204 const Grid2DObject operator-(const double& rhs) const;
206 const Grid2DObject operator-(const Grid2DObject& rhs) const;
207
208 Grid2DObject& operator*=(const double& rhs);
209 const Grid2DObject operator*(const double& rhs) const;
211 const Grid2DObject operator*(const Grid2DObject& rhs) const;
212
213 Grid2DObject& operator/=(const double& rhs);
214 const Grid2DObject operator/(const double& rhs) const;
216 const Grid2DObject operator/(const Grid2DObject& rhs) const;
217
218 bool operator==(const Grid2DObject& in) const;
219 bool operator!=(const Grid2DObject& in) const;
220
223 double cellsize;
224
225 protected:
226 void setValues(const double& cellsize, const Coords& i_llcorner);
227
233 bool WGS84_to_grid(Coords& point) const;
234
240 bool grid_to_WGS84(Coords& point) const;
241
242 bool isLatlon() const {return isLatLon;}
243
244 void reproject();
245
246 static double calculate_XYcellsize(const std::vector<double>& vecX, const std::vector<double>& vecY);
247 double calculate_cellsize(const double& i_ur_lat, const double& i_ur_lon) const;
248 void setLatLon(const double& i_ur_lat, const double& i_ur_lon) {ur_lat=i_ur_lat; ur_lon=i_ur_lon; isLatLon=true;}
249
250 double ur_lat, ur_lon;
252};
253} //end namespace
254
255#endif
size_t iy
grid index along Y
Definition: Grid2DObject.h:50
size_t ix
grid index along X
Definition: Grid2DObject.h:49
structure to contain the grid coordinates of a point in a 2D grid
Definition: Grid2DObject.h:48
A class to handle geographic coordinate systems. This class offers an easy way to transparently conve...
Definition: Coords.h:83
A class to represent 2D Grids. Typical application as DEM or Landuse Model.
Definition: Grid2DObject.h:42
bool grid_to_WGS84(Coords &point) const
Converts grid coordinates (i,j) into WGS84 coordinates.
Definition: Grid2DObject.cc:236
bool isLatlon() const
Definition: Grid2DObject.h:242
bool operator!=(const Grid2DObject &in) const
Operator that tests for inequality.
Definition: Grid2DObject.cc:144
bool allNodata() const
Definition: Grid2DObject.cc:474
Grid2DObject & operator=(const double &value)
Assignement operator.
Definition: Grid2DObject.cc:31
const Grid2DObject operator+(const double &rhs) const
Definition: Grid2DObject.cc:41
Grid2DObject & operator+=(const double &rhs)
Definition: Grid2DObject.cc:36
const std::string toString() const
Definition: Grid2DObject.cc:583
double ur_lat
Definition: Grid2DObject.h:250
double calculate_cellsize(const double &i_ur_lat, const double &i_ur_lon) const
Definition: Grid2DObject.cc:547
double cellsize
dimension in meters of a cell (considered to be square)
Definition: Grid2DObject.h:223
Grid2DObject()
Default constructor. Initializes all variables to 0, except lat/long which are initialized to IOUtils...
Definition: Grid2DObject.cc:152
void set(const size_t &ncols, const size_t &nrows, const double &cellsize, const Coords &i_llcorner)
Set all variables in one go.
Definition: Grid2DObject.cc:338
Array2D< double > grid2D
the grid itself (simple 2D table containing the values for each point)
Definition: Grid2DObject.h:221
double getMin() const
Definition: Grid2DObject.cc:453
const Grid2DObject operator*(const double &rhs) const
Definition: Grid2DObject.cc:93
bool gridify(Coords &point) const
Compute the positional parameters that are not already known This means that the Coords::point object...
Definition: Grid2DObject.cc:218
size_t getNx() const
Definition: Grid2DObject.cc:445
double getMean() const
Definition: Grid2DObject.cc:461
bool isSameGeolocalization(const Grid2DObject &target) const
check if the current Grid2DObject has the same geolocalization attributes as another Grid2DObject (as...
Definition: Grid2DObject.cc:488
Grid2DObject & operator/=(const double &rhs)
Definition: Grid2DObject.cc:114
void rescale(const double &i_cellsize)
Rescale (bilinear resampling) the grid to match the given cell size.
Definition: Grid2DObject.cc:367
std::vector< double > extractPoints(const std::vector< std::pair< size_t, size_t > > &Pts) const
extract point values from grid
Definition: Grid2DObject.cc:519
void reproject()
Definition: Grid2DObject.cc:562
Grid2DObject & operator-=(const double &rhs)
Definition: Grid2DObject.cc:62
void setValues(const double &cellsize, const Coords &i_llcorner)
Definition: Grid2DObject.cc:482
size_t size() const
Definition: Grid2DObject.cc:441
double & operator()(const size_t &ix, const size_t &iy)
Definition: Grid2DObject.cc:567
void compute_spatial_mean(const double &radius)
Spatially average the grid over a given radius.
Definition: Grid2DObject.cc:382
double getMax() const
Definition: Grid2DObject.cc:457
bool operator==(const Grid2DObject &in) const
Operator that tests for equality.
Definition: Grid2DObject.cc:140
friend std::ostream & operator<<(std::ostream &os, const Grid2DObject &grid)
Definition: Grid2DObject.cc:594
void clear()
deletes the data, but keeps geolocalization
Definition: Grid2DObject.cc:466
bool isLatLon
Definition: Grid2DObject.h:251
const Grid2DObject operator/(const double &rhs) const
Definition: Grid2DObject.cc:119
double ur_lon
Definition: Grid2DObject.h:250
bool WGS84_to_grid(Coords &point) const
Converts WGS84 coordinates into grid coordinates (i,j)
Definition: Grid2DObject.cc:286
size_t getNy() const
Definition: Grid2DObject.cc:449
const Grid2DObject operator-(const double &rhs) const
Definition: Grid2DObject.cc:67
friend std::istream & operator>>(std::istream &is, Grid2DObject &grid)
Definition: Grid2DObject.cc:604
Grid2DObject & operator*=(const double &rhs)
Definition: Grid2DObject.cc:88
void setLatLon(const double &i_ur_lat, const double &i_ur_lon)
Definition: Grid2DObject.h:248
Coords llcorner
lower left corner of the grid
Definition: Grid2DObject.h:222
void binning(const std::vector< double > &thresholds, const std::vector< double > &ids)
Partitional algorithm to classify each point of the grid into a 'bin'. The classification is given by...
Definition: Grid2DObject.cc:500
bool empty() const
Check if a grid does not contain any data (but it can contain geolocalization)
Definition: Grid2DObject.cc:470
static double calculate_XYcellsize(const std::vector< double > &vecX, const std::vector< double > &vecY)
Definition: Grid2DObject.cc:535
Definition: GridsManager.h:33
A class representing the IO Layer of the software Alpine3D. For each type of IO (File,...
Definition: IOInterface.h:98
Definition: Config.cc:31