scifir-units 2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
Loading...
Searching...
No Matches
longitude.cpp
Go to the documentation of this file.
1#include "./longitude.hpp"
2
3#include "../util/types.hpp"
4
5#include "boost/algorithm/string.hpp"
6#include "unicode/unistr.h"
7#include "unicode/uchar.h"
8
9#include <cmath>
10#include <iostream>
11#include <sstream>
12#include <string>
13
14using namespace std;
15
16namespace scifir
17{
20
21 longitude::longitude(const longitude& x) : angle(x.get_value())
22 {}
23
24 longitude::longitude(longitude&& x) : angle(std::move(x.get_value()))
25 {}
26
32
38
40 {
43 }
44
50
55
57 {
59 {
60 value = float(x);
62 }
63 else
64 {
65 cerr << "A longitude cannot be initialized with dimensions" << endl;
66 value = 0.0f;
67 }
68 }
69
74
76 {
77 value = x.get_value();
78 return *this;
79 }
80
82 {
83 value = std::move(x.get_value());
84 return *this;
85 }
86
88 {
91 return *this;
92 }
93
99
101 {
102 if (x.has_empty_dimensions())
103 {
104 value = x.get_value();
106 }
107 else
108 {
109 cerr << "A longitude cannot be initialized with dimensions" << endl;
110 }
111 return *this;
112 }
113
115 {
116 if (value > 0.0f)
117 {
118 return longitude::EAST;
119 }
120 else if (value < 0.0f)
121 {
122 return longitude::WEST;
123 }
124 else if (value == 0.0f)
125 {
126 return longitude::CENTER;
127 }
128 return longitude::CENTER;
129 }
130
132 {
133 if (value > 0.0f)
134 {
135 value = -180.0f + std::abs(value);
136 }
137 else if (value < 0.0f)
138 {
139 value = 180.0f - std::abs(value);
140 }
141 }
142
144 {
145 if(isfinite(value))
146 {
147 if (value > 180.0f)
148 {
149 value = 0.0f;
150 }
151 else if (value < -180.0f)
152 {
153 value = 0.0f;
154 }
155 }
156 }
157
159 {
160 if (x > 180.0f)
161 {
162 value = -180.0f + std::abs(x.get_value() - 180.0f);
163 }
164 else if (x <= 180.0f)
165 {
166 value = x.get_value();
167 }
168 }
169
171 {
172 if (init_longitude.length() >= 3)
173 {
174 if (init_longitude.substr(init_longitude.length() - 1) == "E")
175 {
176 value = stof(init_longitude.substr(0,init_longitude.length() - 2));
178 return;
179 }
180 else if (init_longitude.substr(init_longitude.length() - 1) == "W")
181 {
182 value = -1.0f * stof(init_longitude.substr(0,init_longitude.length() - 2));
184 return;
185 }
186 }
187 icu::UnicodeString init_longitude_unicode = icu::UnicodeString(init_longitude.c_str());
188 if (init_longitude_unicode.endsWith(0x00B0) or init_longitude_unicode.endsWith(0x00BA))
189 {
192 }
193 }
194
195 bool is_longitude(const string& init_longitude)
196 {
197 if (init_longitude.length() == 0)
198 {
199 return false;
200 }
201 bool cardinale_point_present = false;
202 if (init_longitude.substr(init_longitude.length() - 1) == "E")
203 {
205 }
206 else if (init_longitude.substr(init_longitude.length() - 1) == "W")
207 {
209 }
210 int start_point = 0;
211 if (cardinale_point_present == false && init_longitude[0] == '-')
212 {
213 start_point++;
214 }
215 icu::UnicodeString x_unicode = icu::UnicodeString(init_longitude.c_str());
216 int total_chars = x_unicode.countChar32();
218 {
219 total_chars--;
220 }
221 bool loop = false;
222 if (x_unicode[total_chars - 1] == 0x00B0 || x_unicode[total_chars - 1] == 0x00BA)
223 {
224 loop = true;
225 }
226 if (loop == false)
227 {
228 return false;
229 }
230 bool dot_present = false;
231 for (int i = start_point; i < (total_chars - 1); i++)
232 {
233 if (x_unicode[i] == '.')
234 {
235 if (dot_present)
236 {
237 return false;
238 }
239 else
240 {
241 dot_present = true;
242 }
243 }
244 else if (!u_isdigit(x_unicode[i]))
245 {
246 return false;
247 }
248 }
249 return true;
250 }
251}
Class that allows to work with angles. Each angle sizes 4 bytes. Initialization string example: "20°"...
Definition angle.hpp:77
const float & get_value() const
Gets the value of the angle, in degrees.
Definition angle.hpp:102
float value
Value of the angle. It is stored in degrees.
Definition angle.hpp:212
void initialize_from_string(string init_longitude)
longitude::position get_position() const
longitude & operator=(const longitude &x)
Definition longitude.cpp:75
void initialize_from_angle(const angle &x)
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
bool has_empty_dimensions() const
Checks if there aren't base dimensions.
const float & get_value() const
Read-only getter of the value.
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
bool is_longitude(const string &init_longitude)