geod.c
6.77 KB
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
233
234
235
236
237
238
239
240
#ifndef lint
static const char SCCSID[]="@(#)geod.c 4.8 95/09/23 GIE REL";
#endif
/* <<<< Geodesic filter program >>>> */
# include "projects.h"
# include "geodesic.h"
# include "emess.h"
# include <ctype.h>
# include <stdio.h>
# include <string.h>
# define MAXLINE 200
# define MAX_PARGS 50
# define TAB putchar('\t')
static int
fullout = 0, /* output full set of geodesic values */
tag = '#', /* beginning of line tag character */
pos_azi = 0, /* output azimuths as positive values */
inverse = 0; /* != 0 then inverse geodesic */
static char
*oform = (char *)0, /* output format for decimal degrees */
*osform = "%.3f", /* output format for S */
pline[50], /* work string */
*usage =
"%s\nusage: %s [ -afFIptTwW [args] ] [ +opts[=arg] ] [ files ]\n";
static void
printLL(double p, double l) {
if (oform) {
(void)printf(oform, p * RAD_TO_DEG); TAB;
(void)printf(oform, l * RAD_TO_DEG);
} else {
(void)fputs(rtodms(pline, p, 'N', 'S'),stdout); TAB;
(void)fputs(rtodms(pline, l, 'E', 'W'),stdout);
}
}
static void
do_arc(void) {
double az;
printLL(phi2, lam2); putchar('\n');
for (az = al12; n_alpha--; ) {
al12 = az = adjlon(az + del_alpha);
geod_pre();
geod_for();
printLL(phi2, lam2); putchar('\n');
}
}
static void /* generate intermediate geodesic coordinates */
do_geod(void) {
double phil, laml, del_S;
phil = phi2;
laml = lam2;
printLL(phi1, lam1); putchar('\n');
for ( geod_S = del_S = geod_S / n_S; --n_S; geod_S += del_S) {
geod_for();
printLL(phi2, lam2); putchar('\n');
}
printLL(phil, laml); putchar('\n');
}
void static /* file processing function */
process(FILE *fid) {
char line[MAXLINE+3], *s;
for (;;) {
++emess_dat.File_line;
if (!(s = fgets(line, MAXLINE, fid)))
break;
if (!strchr(s, '\n')) { /* overlong line */
int c;
strcat(s, "\n");
/* gobble up to newline */
while ((c = fgetc(fid)) != EOF && c != '\n') ;
}
if (*s == tag) {
fputs(line, stdout);
continue;
}
phi1 = dmstor(s, &s);
lam1 = dmstor(s, &s);
if (inverse) {
phi2 = dmstor(s, &s);
lam2 = dmstor(s, &s);
geod_inv();
} else {
al12 = dmstor(s, &s);
geod_S = strtod(s, &s) * to_meter;
geod_pre();
geod_for();
}
if (!*s && (s > line)) --s; /* assumed we gobbled \n */
if (pos_azi) {
if (al12 < 0.) al12 += TWOPI;
if (al21 < 0.) al21 += TWOPI;
}
if (fullout) {
printLL(phi1, lam1); TAB;
printLL(phi2, lam2); TAB;
if (oform) {
(void)printf(oform, al12 * RAD_TO_DEG); TAB;
(void)printf(oform, al21 * RAD_TO_DEG); TAB;
(void)printf(osform, geod_S * fr_meter);
} else {
(void)fputs(rtodms(pline, al12, 0, 0), stdout); TAB;
(void)fputs(rtodms(pline, al21, 0, 0), stdout); TAB;
(void)printf(osform, geod_S * fr_meter);
}
} else if (inverse)
if (oform) {
(void)printf(oform, al12 * RAD_TO_DEG); TAB;
(void)printf(oform, al21 * RAD_TO_DEG); TAB;
(void)printf(osform, geod_S * fr_meter);
} else {
(void)fputs(rtodms(pline, al12, 0, 0), stdout); TAB;
(void)fputs(rtodms(pline, al21, 0, 0), stdout); TAB;
(void)printf(osform, geod_S * fr_meter);
}
else {
printLL(phi2, lam2); TAB;
if (oform)
(void)printf(oform, al21 * RAD_TO_DEG);
else
(void)fputs(rtodms(pline, al21, 0, 0), stdout);
}
(void)fputs(s, stdout);
}
}
static char *pargv[MAX_PARGS];
static int pargc = 0;
int main(int argc, char **argv) {
char *arg, **eargv = argv, *strnchr();
FILE *fid;
static int eargc = 0, c;
if (emess_dat.Prog_name = strrchr(*argv,'/')) ++emess_dat.Prog_name;
else emess_dat.Prog_name = *argv;
inverse = ! strncmp(emess_dat.Prog_name, "inv", 3);
if (argc <= 1 ) {
(void)fprintf(stderr, usage, pj_get_release(),
emess_dat.Prog_name);
exit (0);
}
/* process run line arguments */
while (--argc > 0) { /* collect run line arguments */
if(**++argv == '-') for(arg = *argv;;) {
switch(*++arg) {
case '\0': /* position of "stdin" */
if (arg[-1] == '-') eargv[eargc++] = "-";
break;
case 'a': /* output full set of values */
fullout = 1;
continue;
case 'I': /* alt. inverse spec. */
inverse = 1;
continue;
case 't': /* set col. one char */
if (arg[1]) tag = *++arg;
else emess(1,"missing -t col. 1 tag");
continue;
case 'W': /* specify seconds precision */
case 'w': /* -W for constant field width */
if ((c = arg[1]) && isdigit(c)) {
set_rtodms(c - '0', *arg == 'W');
++arg;
} else
emess(1,"-W argument missing or non-digit");
continue;
case 'f': /* alternate output format degrees or xy */
if (--argc <= 0)
noargument: emess(1,"missing argument for -%c",*arg);
oform = *++argv;
continue;
case 'F': /* alternate output format degrees or xy */
if (--argc <= 0) goto noargument;
osform = *++argv;
continue;
case 'l':
if (!arg[1] || arg[1] == 'e') { /* list of ellipsoids */
struct PJ_ELLPS *le;
for (le=pj_get_ellps_ref(); le->id ; ++le)
(void)printf("%9s %-16s %-16s %s\n",
le->id, le->major, le->ell, le->name);
} else if (arg[1] == 'u') { /* list of units */
struct PJ_UNITS *lu;
for (lu = pj_get_units_ref();lu->id ; ++lu)
(void)printf("%12s %-20s %s\n",
lu->id, lu->to_meter, lu->name);
} else
emess(1,"invalid list option: l%c",arg[1]);
exit( 0 );
case 'p': /* output azimuths as positive */
pos_azi = 1;
continue;
default:
emess(1, "invalid option: -%c",*arg);
break;
}
break;
} else if (**argv == '+') /* + argument */
if (pargc < MAX_PARGS)
pargv[pargc++] = *argv + 1;
else
emess(1,"overflowed + argument table");
else /* assumed to be input file name(s) */
eargv[eargc++] = *argv;
}
/* done with parameter and control input */
geod_set(pargc, pargv); /* setup projection */
if ((n_alpha || n_S) && eargc)
emess(1,"files specified for arc/geodesic mode");
if (n_alpha)
do_arc();
else if (n_S)
do_geod();
else { /* process input file list */
if (eargc == 0) /* if no specific files force sysin */
eargv[eargc++] = "-";
for ( ; eargc-- ; ++eargv) {
if (**eargv == '-') {
fid = stdin;
emess_dat.File_name = "<stdin>";
} else {
if ((fid = fopen(*eargv, "r")) == NULL) {
emess(-2, *eargv, "input file");
continue;
}
emess_dat.File_name = *eargv;
}
emess_dat.File_line = 0;
process(fid);
(void)fclose(fid);
emess_dat.File_name = (char *)0;
}
}
exit(0); /* normal completion */
}