-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathswhgeo.c
221 lines (207 loc) · 5.25 KB
/
swhgeo.c
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
/*
Swephelp
Copyright 2007-2020 Stanislas Marquis <[email protected]>
Swephelp is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
Swephelp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Swephelp. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "swhgeo.h"
#include "swhwin.h"
int _swh_geocstrip(const char* coord, char* ret, const size_t maxlen)
{
size_t i = 0;
char* p = (char*) coord;
for (; *p; ++p) {
if (++i == maxlen)
return 1;
switch (*p) {
case '\xc2': /* degree sign c2b0 */
if (*++p != '\xb0')
return 1;
case '\'':
case '"':
case ',':
case ':':
case '/':
*ret++ = ' ';
continue;
case 'N':
case 'S':
case 'E':
case 'W':
*ret++ = tolower(*p);
continue;
case 'n':
case 's':
case 'e':
case 'w':
case '-':
case '.':
case ' ':
*ret++ = *p;
continue;
default:
if (!isdigit(*p))
return 1;
*ret++ = *p;
}
}
*ret = '\0';
return 0;
}
int swh_geoc2d(const char* coord, double* ret)
{
int i, ideg, imin;
double ddeg, dmin, dsec;
char sign, rest;
char str[64];
assert(coord);
assert(ret);
if (!*coord)
return 1;
/* remove decorations (°'",:/) */
if (_swh_geocstrip(coord, str, 64))
return 1;
/* longest patterns first */
i = sscanf(str, "%d %d %lf %c%c", /* DMSx */
&ideg, &imin, &dsec, &sign, &rest);
if (i == 4) {
*ret = ideg + ((1.0/60)*imin) + ((1.0/3600)*dsec);
ddeg = ideg;
dmin = imin;
goto checksign;
}
i = sscanf(str, "%d %c %d %lf%c", /* DxMS */
&ideg, &sign, &imin, &dsec, &rest);
if (i == 4) {
*ret = ideg + ((1.0/60)*imin) + ((1.0/3600)*dsec);
ddeg = ideg;
dmin = imin;
goto checksign;
}
i = sscanf(str, "%d %lf %c%c", /* DMx */
&ideg, &dmin, &sign, &rest);
if (i == 3) {
*ret = ideg + ((1.0/60)*dmin);
ddeg = ideg;
dsec = 0;
goto checksign;
}
i = sscanf(str, "%d %c %lf%c", /* DxM */
&ideg, &sign, &dmin, &rest);
if (i == 3) {
*ret = ideg + ((1.0/60)*dmin);
ddeg = ideg;
dsec = 0;
goto checksign;
}
i = sscanf(str, "%d %d %lf%c", /* DMS */
&ideg, &imin, &dsec, &rest);
if (i == 3) {
*ret = ideg + ((1.0/60)*imin) + ((1.0/3600)*dsec);
ddeg = ideg;
dmin = imin;
goto check;
}
i = sscanf(str, "%lf %c%c", /* Dx */
&ddeg, &sign, &rest);
if (i == 2) {
*ret = ddeg;
dmin = 0;
dsec = 0;
goto checksign;
}
i = sscanf(str, "%d %lf%c", /* DM */
&ideg, &dmin, &rest);
if (i == 2) {
*ret = ideg + ((1.0/60)*dmin);
ddeg = ideg;
dsec = 0;
goto check;
}
i = sscanf(str, "%lf%c", /* D */
&ddeg, &rest);
if (i == 1) {
*ret = ddeg;
dmin = 0;
dsec = 0;
goto check;
}
return 1;
checksign:
switch (sign) {
case 'n':
case 's':
if (ddeg < 0 || ddeg > 90)
return 1;
break;
default:
if (ddeg < 0 || ddeg > 180)
return 1;
}
switch (sign) {
case 'n':
case 'e':
if (*ret < 0)
return 1;
break;
default:
if (*ret > 0)
*ret = -(*ret);
}
check:
if (ddeg < -180 || ddeg > 180
|| dmin < 0 || dmin >= 60 || dsec < 0 || dsec >= 60)
return 1;
return 0;
}
int swh_geod2i(double coord, int *ret)
{
coord = fabs(coord);
ret[0] = (int) floor(coord);
coord -= ret[0];
ret[1] = (int) lround(coord * 60);
coord -= ret[1] / 60.0;
ret[2] = (int) lround(coord * 3600);
if (ret[2] < 0) ret[2] = 0;
return 0;
}
int swh_geod2c(double coord, int maxdeg, char *ret)
{
int deg, dir, min, sec;
if (coord < -(maxdeg) || coord > maxdeg)
return -1;
if (coord >= 0)
{
deg = (int) floor(coord);
dir = 1;
}
else
{
deg = (int) fabs(ceil(coord));
dir = 0;
}
coord = fabs(coord) - deg;
min = (int) lround(coord * 60);
coord -= min / 60.0;
sec = (int) lround(coord * 3600);
if (maxdeg == 90) /* latitude */
sprintf(ret, "%.2d:%s:%.2d:%.2d", deg, dir ? "N" : "S", min, sec);
else /* assuming longitude */
sprintf(ret, "%.3d:%s:%.2d:%.2d", deg, dir ? "E" : "W", min, sec);
return 0;
}
/* vi: set fenc=utf-8 ff=unix et sw=4 ts=4 : */