MeteoIODoc  2.10.0
Coords.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 COORDS_H
20 #define COORDS_H
21 
22 #include <string>
23 #include <iostream>
24 
25 namespace mio {
82 class Coords {
83  public:
85  typedef enum GEO_DISTANCES {
89 
91  typedef enum {
93  FULL,
95  XY,
96  CARTESIAN
98 
99  //Constructors
100  Coords();
101  Coords(const std::string& in_coordinatesystem, const std::string& in_parameters="");
102  Coords(const std::string& in_coordinatesystem, const std::string& in_parameters, std::string coord_spec);
103  Coords(const double& in_lat_ref, const double& in_long_ref);
104  Coords(const Coords& c);
105 
106  //Operators
107  Coords& operator=(const Coords&);
108  bool operator==(const Coords&) const;
109  bool operator!=(const Coords&) const;
110  bool isNodata() const;
111  void moveByXY(const double& x_displacement, const double& y_displacement);
112  void moveByBearing(const double& i_bearing, const double& i_distance);
113 
114  static Coords merge(const Coords& coord1, const Coords& coord2);
115  void merge(const Coords& coord2);
116 
117  //Getter methods
118  double getEasting() const {return easting;}
119  double getNorthing() const {return northing;}
120  double getLat() const {return latitude;}
121  double getLon() const {return longitude;}
122  double getAltitude() const {return altitude;}
123  int getGridI() const {return grid_i;}
124  int getGridJ() const {return grid_j;}
125  int getGridK() const {return grid_k;}
126  bool indexIsValid() const {return validIndex;}
127  void getProj(std::string& proj_type, std::string& proj_args) const;
128  short int getEPSG() const;
129 
130  const std::string toString(const FORMATS& type = DEBUG) const;
131  friend std::ostream& operator<<(std::ostream& os, const Coords& coord);
132  friend std::istream& operator>>(std::istream& is, Coords& coord);
133 
134  //Setter methods
135  void setLatLon(const double in_latitude, const double in_longitude, const double in_altitude, const bool in_update=true);
136  void setLatLon(const std::string& in_coordinates, const double in_altitude, const bool in_update=true);
137  void setXY(const double in_easting, const double in_northing, const double in_altitude, const bool in_update=true);
138  void setGridIndex(const int in_grid_i, const int in_grid_j, const int in_grid_k, const bool setValid=false);
139  void setAltitude(const double in_altitude, const bool in_update=true);
140  void setProj(const std::string& in_coordinatesystem, const std::string& in_parameters="");
141  void setLocalRef(const double in_ref_latitude, const double in_ref_longitude);
142  void setLocalRef(const std::string in_coordparam);
143  void setDistances(const geo_distances in_algo);
144  void setEPSG(const int& epsg);
145 
146  void check(const std::string& pre_msg="");
147  double distance(const Coords& destination) const;
148  bool isSameProj(const Coords& target) const;
149  void copyProj(const Coords& source, const bool i_update=true);
150 
151  private:
152  //Coordinates conversions
153  void convert_to_WGS84(double i_easting, double i_northing, double& o_latitude, double& o_longitude) const;
154  void convert_from_WGS84(double i_latitude, double i_longitude, double& o_easting, double& o_northing) const;
155 
156  void WGS84_to_local(double lat_in, double long_in, double& east_out, double& north_out) const;
157  void local_to_WGS84(double east_in, double north_in, double& lat_out, double& long_out) const;
158  void WGS84_to_NULL(double lat_in, double long_in, double& east_out, double& north_out) const;
159  void NULL_to_WGS84(double east_in, double north_in, double& lat_out, double& long_out) const;
160 
161  //Distances calculations
162  void distance(const Coords& destination, double& o_distance, double& o_bearing) const;
163 
164  private:
165  void clearCoordinates();
166  void setDefaultValues();
167 
168  private:
169  double ref_latitude, ref_longitude;
170  double altitude;
171  double latitude, longitude;
172  double easting, northing;
173  int grid_i, grid_j, grid_k;
174  bool validIndex;
175 
176  std::string coordsystem, coordparam;
177  geo_distances distance_algo;
178 };
179 } //end namespace
180 
181 #endif
A class to handle geographic coordinate systems. This class offers an easy way to transparently conve...
Definition: Coords.h:82
friend std::istream & operator>>(std::istream &is, Coords &coord)
Definition: Coords.cc:290
int getGridI() const
Definition: Coords.h:123
geo_distances
Keywords for selecting the algorithm for computing geodesic distances.
Definition: Coords.h:85
@ GEO_VINCENTY
Vincenty ellispoid formula (See T. Vincenty, "Closed formulas for the direct and reverse geodetic pro...
Definition: Coords.h:87
@ GEO_COSINE
Spherical law of cosine (See http://www.movable-type.co.uk/scripts/latlong.html)
Definition: Coords.h:86
const std::string toString(const FORMATS &type=DEBUG) const
Print the content of the Coords object (useful for debugging) The Coords is bound by "<Coords>" and "...
Definition: Coords.cc:222
void setLocalRef(const double in_ref_latitude, const double in_ref_longitude)
Set the local projection reference coordinates This projection will be used for converting between la...
Definition: Coords.cc:635
friend std::ostream & operator<<(std::ostream &os, const Coords &coord)
Definition: Coords.cc:266
void moveByBearing(const double &i_bearing, const double &i_distance)
Definition: Coords.cc:149
void setProj(const std::string &in_coordinatesystem, const std::string &in_parameters="")
Set projection to use This projection will be used for converting between lat/lon and East/North (see...
Definition: Coords.cc:591
Coords & operator=(const Coords &)
Assignement operator.
Definition: Coords.cc:112
int getGridJ() const
Definition: Coords.h:124
void setGridIndex(const int in_grid_i, const int in_grid_j, const int in_grid_k, const bool setValid=false)
Set grid indices This index represent the position in a cartesian grid. It can not be automatically m...
Definition: Coords.cc:559
bool indexIsValid() const
Returns true if the (i,j,k) index are valid.
Definition: Coords.h:126
double getLon() const
Definition: Coords.h:121
double distance(const Coords &destination) const
Calculate the distance between two points.
Definition: Coords.cc:719
short int getEPSG() const
returns the epsg code of the current projection
Definition: Coords.cc:770
FORMATS
Keywords for selecting the toString formats.
Definition: Coords.h:91
@ DEBUG
As much information as possible, useful for debugging.
Definition: Coords.h:92
@ FULL
Provide all the usually necessary information.
Definition: Coords.h:93
@ CARTESIAN
Compact representation only containing the X/Y and I/J coordinates.
Definition: Coords.h:96
@ XY
Simplified cartesian, only easting/northing.
Definition: Coords.h:95
@ LATLON
Simplified, lat/lon only.
Definition: Coords.h:94
double getAltitude() const
Definition: Coords.h:122
void setXY(const double in_easting, const double in_northing, const double in_altitude, const bool in_update=true)
Set easting and northing The automatic update of the latitude/longitude can be turned off so that bot...
Definition: Coords.cc:528
bool isNodata() const
move the point by the specified distance (in m) along easting and northing
Definition: Coords.cc:132
bool operator==(const Coords &) const
Operator that tests for equality.
Definition: Coords.cc:75
void moveByXY(const double &x_displacement, const double &y_displacement)
move the point by the specified bearing and distance (in m)
Definition: Coords.cc:144
void setDistances(const geo_distances in_algo)
Set the algorithm to use to compute distances Various algorithm exist that offer various precision/co...
Definition: Coords.cc:664
void getProj(std::string &proj_type, std::string &proj_args) const
Returns the projection parameters.
Definition: Coords.cc:462
bool isSameProj(const Coords &target) const
Check if two Coords object are using the same projection.
Definition: Coords.cc:730
void check(const std::string &pre_msg="")
Check consistency of coordinates When both latitude/longitude and easting/northing are given,...
Definition: Coords.cc:680
double getNorthing() const
Definition: Coords.h:119
void setEPSG(const int &epsg)
set the current projection to a given EPSG-defined projection
Definition: Coords.cc:779
void setAltitude(const double in_altitude, const bool in_update=true)
Set altitude at a given value. If the i,j,k indices were set, reset them to inodata,...
Definition: Coords.cc:577
void copyProj(const Coords &source, const bool i_update=true)
Copy the projection parameters of another Coords object.
Definition: Coords.cc:743
double getLat() const
Definition: Coords.h:120
static Coords merge(const Coords &coord1, const Coords &coord2)
Simple merge strategy. If some fields of the first argument are empty, they will be filled by the mat...
Definition: Coords.cc:174
bool operator!=(const Coords &) const
Operator that tests for inequality.
Definition: Coords.cc:108
double getEasting() const
Definition: Coords.h:118
int getGridK() const
Definition: Coords.h:125
Coords()
Default constructor This constructor builds a dummy object that performs no conversions but can be us...
Definition: Coords.cc:320
void setLatLon(const double in_latitude, const double in_longitude, const double in_altitude, const bool in_update=true)
Set latitude and longitude The automatic update of the easting/northing can be turned off so that bot...
Definition: Coords.cc:498
Definition: Config.cc:30