forked from sfelixwu/ecs36b_s2024_master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPS.cpp
232 lines (197 loc) · 6.47 KB
/
GPS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "GPS.h"
GPS_DD::GPS_DD()
{
this->class_name = "GPS_DD";
this->latitude = 0.0;
this->longitude = 0.0;
}
GPS_DD::GPS_DD(double arg_latitude, double arg_longitude)
{
this->class_name = "GPS_DD";
this->latitude = arg_latitude;
this->longitude = arg_longitude;
}
const double
GPS_DD::getLatitude()
{
return this->latitude;
}
const double
GPS_DD::getLongitude
()
{
return this->longitude;
}
// DD distance calculation
/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: :*/
/*:: This routine calculates the distance between two points (given the :*/
/*:: latitude/longitude of those points). It is being used to calculate :*/
/*:: the distance between two locations using GeoDataSource(TM) products. :*/
/*:: :*/
/*:: Definitions: :*/
/*:: South latitudes are negative, east longitudes are positive :*/
/*:: :*/
/*:: Passed to function: :*/
/*:: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :*/
/*:: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :*/
/*:: unit = the unit you desire for results :*/
/*:: where: 'M' is statute miles (default) :*/
/*:: 'K' is kilometers :*/
/*:: 'N' is nautical miles :*/
/*:: Worldwide cities and other features databases with latitude longitude :*/
/*:: are available at https://www.geodatasource.com :*/
/*:: :*/
/*:: For enquiries, please contact [email protected] :*/
/*:: :*/
/*:: Official Web site: https://www.geodatasource.com :*/
/*:: :*/
/*:: GeoDataSource.com (C) All Rights Reserved 2018 :*/
/*:: :*/
/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
#include <math.h>
#define pi 3.14159265358979323846
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: Function prototypes :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
double deg2rad(double);
double rad2deg(double);
double GeoDataSource_distance(double lat1, double lon1, double lat2, double lon2, char unit)
{
double theta, dist;
if ((lat1 == lat2) && (lon1 == lon2))
{
return 0;
}
else
{
theta = lon1 - lon2;
dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * cos(deg2rad(theta));
dist = acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
switch(unit)
{
case 'M':
break;
case 'K':
dist = dist * 1.609344;
break;
case 'N':
dist = dist * 0.8684;
break;
}
return (dist);
}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
double deg2rad(double deg)
{
return (deg * pi / 180);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
double rad2deg(double rad)
{
return (rad * 180 / pi);
}
double
GPS_DD::distance
(GPS_DD another)
{
return GeoDataSource_distance(this->latitude, this->longitude,
another.getLatitude(), another.getLongitude(), 'M');
}
bool
GPS_DD::operator==
(GPS_DD another)
{
return((this->latitude == another.getLatitude()) &&
(this->longitude == another.getLongitude()));
}
bool
GPS_DD::operator<
(GPS_DD another)
{
if (this->latitude < another.getLatitude()) return true;
if ((this->latitude == another.getLatitude()) &&
(this->longitude < another.getLongitude()))
return true;
return false;
}
Json::Value *
GPS_DD::dump2JSON
(void)
{
Json::Value * result_ptr = new Json::Value();
if (this->latitude != 0.0)
{
(*result_ptr)["latitude"] = this->latitude;
}
if (this->longitude != 0.0)
{
(*result_ptr)["longitude"] = this->longitude;
}
return result_ptr;
}
void
GPS_DD::JSON2Object
(Json::Value * arg_json_ptr)
{
Exception_Info * ei_ptr = NULL;
ecs36b_Exception *lv_exception_ptr = new ecs36b_Exception();
JSON2Object_precheck(arg_json_ptr, lv_exception_ptr,
ECS36B_ERROR_JSON2OBJECT_GPS_DD);
if ((((*arg_json_ptr)["latitude"]).isNull() == true) ||
(((*arg_json_ptr)["latitude"]).isDouble() != true))
{
ei_ptr = new Exception_Info {};
ei_ptr->where_code = ECS36B_ERROR_JSON2OBJECT_GPS_DD;
ei_ptr->which_string = "latitude";
ei_ptr->how_code = ECS36B_ERROR_NORMAL;
if ((*arg_json_ptr)["latitude"].isNull() == true)
{
ei_ptr->what_code = ECS36B_ERROR_JSON_KEY_MISSING;
}
else
{
ei_ptr->what_code = ECS36B_ERROR_JSON_KEY_TYPE_MISMATCHED;
}
ei_ptr->array_index = 0;
(lv_exception_ptr->info_vector).push_back(ei_ptr);
}
else
{
this->latitude = ((*arg_json_ptr)["latitude"]).asDouble();
}
if ((((*arg_json_ptr)["longitude"]).isNull() == true) ||
(((*arg_json_ptr)["longitude"]).isDouble() != true))
{
ei_ptr = new Exception_Info {};
ei_ptr->where_code = ECS36B_ERROR_JSON2OBJECT_GPS_DD;
ei_ptr->which_string = "longitude";
ei_ptr->how_code = ECS36B_ERROR_NORMAL;
if ((*arg_json_ptr)["longitude"].isNull() == true)
{
ei_ptr->what_code = ECS36B_ERROR_JSON_KEY_MISSING;
}
else
{
ei_ptr->what_code = ECS36B_ERROR_JSON_KEY_TYPE_MISMATCHED;
}
ei_ptr->array_index = 0;
(lv_exception_ptr->info_vector).push_back(ei_ptr);
}
else
{
this->longitude = ((*arg_json_ptr)["longitude"]).asDouble();
}
if ((lv_exception_ptr->info_vector).size() != 0)
{
throw (*lv_exception_ptr);
}
return;
}