Jpp 19.3.0
the software that should make you happy
Loading...
Searching...
No Matches
JUTMGrid.hh
Go to the documentation of this file.
1#ifndef __JUTM__JUTMGRID__
2#define __JUTM__JUTMGRID__
3
4#include <string>
5#include <ostream>
6#include <ostream>
7#include <sstream>
8#include <ctype.h>
9
10#include "JLang/JException.hh"
11#include "JLang/JEquals.hh"
12#include "JMath/JConstants.hh"
13#include "JIO/JSerialisable.hh"
14#include "JIO/JSTDIO.hh"
15
16
17/**
18 * \author mdejong
19 */
20
21namespace JUTM {}
22namespace JPP { using namespace JUTM; }
23
24namespace JUTM {
25
27 using JLANG::JEquals;
28 using JMATH::PI;
29 using JIO::JReader;
30 using JIO::JWriter;
31
32 /**
33 * UTM zone width [rad]
34 *
35 * The UTM system consists of 60 zones, each 6-degrees of longitude in width.\n
36 * The zones are numbered 1-60, beginning at 180-degrees longitude and increasing to the East.
37 */
38 static const double UTM_ZONE_WIDTH_RAD = 6.0 * PI / 180.0;
39
40
41 /**
42 * Get UTM zone for given longitude.
43 *
44 * \param longitude longitude [rad]
45 * \return UTM zone
46 */
47 inline int getUTMZone(const double longitude)
48 {
49 return 1 + int((PI + longitude) / UTM_ZONE_WIDTH_RAD);
50 }
51
52
53 /**
54 * Get longitude of the central meridian for given UTM zone.
55 *
56 * \param zone UTM zone
57 * \return longitude of central meridian [rad]
58 */
59 static inline double getUTMLongitude(const int zone)
60 {
61 return -PI + (zone - 1) * UTM_ZONE_WIDTH_RAD + 0.5*UTM_ZONE_WIDTH_RAD;
62 }
63
64
65 /**
66 * Data structure for UTM grid.
67 *
68 * This data structure is composed of:
69 * - World Geodetic System;
70 * - UTM zone;
71 */
72 class JUTMGrid :
73 public JEquals<JUTMGrid>
74 {
75 public:
76 /**
77 * Default constructor.
78 */
80 key ("?"),
81 wgs ("?"),
82 zone("?")
83 {}
84
85
86 /**
87 * Constructor.
88 *
89 * \param key key
90 * \param wgs WGS
91 * \param zone UTM zone
92 */
93 JUTMGrid(const std::string& key,
94 const std::string& wgs,
95 const std::string& zone)
96 {
97 this->key = key;
98 this->wgs = wgs;
99 this->zone = zone;
100 }
101
102
103 /**
104 * Get UTM grid.
105 *
106 * \return UTM grid
107 */
108 const JUTMGrid& getUTMGrid() const
109 {
110 return static_cast<const JUTMGrid&>(*this);
111 }
112
113
114 /**
115 * Set UTM grid.
116 *
117 * \param grid UTM grid
118 */
119 void setUTMGrid(const JUTMGrid& grid)
120 {
121 static_cast<JUTMGrid&>(*this) = grid;
122 }
123
124
125 /**
126 * Get key.
127 *
128 * \return key
129 */
130 const std::string& getKey() const
131 {
132 return key;
133 }
134
135
136 /**
137 * Get WGS.
138 *
139 * \return WGS
140 */
141 const std::string& getWGS() const
142 {
143 return wgs;
144 }
145
146
147 /**
148 * Get UTM zone.
149 *
150 * \return UTM zone
151 */
152 const std::string& getUTMZone() const
153 {
154 return zone;
155 }
156
157
158 /**
159 * Get UTM zone by numerical value.
160 *
161 * \return UTM zone
162 */
163 int getZone() const
164 {
165 using namespace std;
166
167 int value = 0;
168
169 for (string::const_iterator i = zone.begin(); i != zone.end(); ++i) {
170 if (isdigit(*i))
171 value = 10 * value + (*i - '0');
172 else
173 break;
174 }
175
176 return value;
177 }
178
179
180 /**
181 * Check equality.
182 *
183 * \param grid UTM grid
184 * \return true if grids are equal; else false
185 */
186 bool equals(const JUTMGrid& grid) const
187 {
188 return (this->getKey() == grid.getKey() &&
189 this->getWGS() == grid.getWGS() &&
190 this->getUTMZone() == grid.getUTMZone());
191 }
192
193
194 /**
195 * Convert UTM grid.
196 *
197 * \return UTM grid
198 */
199 std::string toString() const
200 {
201 return (key + " " + wgs + " " + zone);
202 }
203
204
205 /**
206 * Extract UTM grid.
207 *
208 * \param buffer WGS and UTM zone
209 * \return UTM grid
210 */
211 static JUTMGrid valueOf(const std::string& buffer)
212 {
213 JUTMGrid grid;
214
215 std::istringstream is(buffer);
216
217 if (is >> grid)
218 return grid;
219 else
220 throw JParseError("JUTMGrid::valueOf()");
221 }
222
223
224 /**
225 * Read UTM grid from input.
226 *
227 * \param in input stream
228 * \param grid UTM grid
229 * \return input stream
230 */
231 friend inline std::istream& operator>>(std::istream& in, JUTMGrid& grid)
232 {
233 return in >> grid.key >> grid.wgs >> grid.zone;
234 }
235
236
237 /**
238 * Write UTM grid to output.
239 *
240 * \param out output stream
241 * \param grid UTM grid
242 * \return output stream
243 */
244 friend inline std::ostream& operator<<(std::ostream& out, const JUTMGrid& grid)
245 {
246 return out << grid.key << ' ' << grid.wgs << ' ' << grid.zone;
247 }
248
249
250 /**
251 * Read UTM grid from input.
252 *
253 * \param in input stream
254 * \param grid UTM grid
255 * \return input stream
256 */
257 friend inline JReader& operator>>(JReader& in, JUTMGrid& grid)
258 {
259 using namespace JIO;
260
261 return in >> grid.key >> grid.wgs >> grid.zone;
262 }
263
264
265 /**
266 * Write UTM grid to output.
267 *
268 * \param out output stream
269 * \param grid UTM grid
270 * \return output stream
271 */
272 friend inline JWriter& operator<<(JWriter& out, const JUTMGrid& grid)
273 {
274 using namespace JIO;
275
276 return out << grid.key << grid.wgs << grid.zone;
277 }
278
279
280 protected:
281 std::string key;
282 std::string wgs;
283 std::string zone;
284 };
285}
286
287#endif
Exceptions.
Mathematical constants.
STD extensions for binary I/O.
Interface for binary input.
Interface for binary output.
Exception for parsing value.
Data structure for UTM grid.
Definition JUTMGrid.hh:74
std::string toString() const
Convert UTM grid.
Definition JUTMGrid.hh:199
JUTMGrid(const std::string &key, const std::string &wgs, const std::string &zone)
Constructor.
Definition JUTMGrid.hh:93
friend std::ostream & operator<<(std::ostream &out, const JUTMGrid &grid)
Write UTM grid to output.
Definition JUTMGrid.hh:244
friend JReader & operator>>(JReader &in, JUTMGrid &grid)
Read UTM grid from input.
Definition JUTMGrid.hh:257
static JUTMGrid valueOf(const std::string &buffer)
Extract UTM grid.
Definition JUTMGrid.hh:211
const std::string & getKey() const
Get key.
Definition JUTMGrid.hh:130
friend std::istream & operator>>(std::istream &in, JUTMGrid &grid)
Read UTM grid from input.
Definition JUTMGrid.hh:231
const JUTMGrid & getUTMGrid() const
Get UTM grid.
Definition JUTMGrid.hh:108
friend JWriter & operator<<(JWriter &out, const JUTMGrid &grid)
Write UTM grid to output.
Definition JUTMGrid.hh:272
const std::string & getWGS() const
Get WGS.
Definition JUTMGrid.hh:141
int getZone() const
Get UTM zone by numerical value.
Definition JUTMGrid.hh:163
bool equals(const JUTMGrid &grid) const
Check equality.
Definition JUTMGrid.hh:186
void setUTMGrid(const JUTMGrid &grid)
Set UTM grid.
Definition JUTMGrid.hh:119
const std::string & getUTMZone() const
Get UTM zone.
Definition JUTMGrid.hh:152
std::string key
Definition JUTMGrid.hh:281
JUTMGrid()
Default constructor.
Definition JUTMGrid.hh:79
std::string wgs
Definition JUTMGrid.hh:282
std::string zone
Definition JUTMGrid.hh:283
Auxiliary classes and methods for binary I/O.
static const double PI
Mathematical constants.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliaries for handling universal transverse mercator coordinate system (UTM).
Definition JUTMGrid.hh:21
int getUTMZone(const double longitude)
Get UTM zone for given longitude.
Definition JUTMGrid.hh:47
static double getUTMLongitude(const int zone)
Get longitude of the central meridian for given UTM zone.
Definition JUTMGrid.hh:59
static const double UTM_ZONE_WIDTH_RAD
UTM zone width [rad].
Definition JUTMGrid.hh:38
Template definition of auxiliary base class for comparison of data structures.
Definition JEquals.hh:84