svn-gvsig-desktop / tags / v1_10_0_Build_1258 / libraries / libjni-proj4 / src / libcs2cs.c
History | View | Annotate | Download (12.4 KB)
1 |
#include <stdio.h> |
---|---|
2 |
#include <stdlib.h> |
3 |
#include <ctype.h> |
4 |
#include <string.h> |
5 |
#include <math.h> |
6 |
#include "projects.h" |
7 |
#include "emess.h" |
8 |
|
9 |
#define MAX_LINE 200 |
10 |
#define MAX_PARGS 100 |
11 |
|
12 |
static projPJ fromProj, toProj;
|
13 |
|
14 |
static int |
15 |
reversein = 0, /* != 0 reverse input arguments */ |
16 |
reverseout = 0, /* != 0 reverse output arguments */ |
17 |
echoin = 0, /* echo input data to output line */ |
18 |
tag = '#'; /* beginning of line tag character */ |
19 |
static char |
20 |
*oform = (char *)0, /* output format for x-y or decimal degrees */ |
21 |
*oterr = "*\t*", /* output line for unprojectable input */ |
22 |
*usage = |
23 |
"%s\nusage: %s [ -eEfIlrstvwW [args] ] [ +opts[=arg] ]\n"
|
24 |
" [+to [+opts[=arg] [ files ]\n";
|
25 |
|
26 |
static struct FACTORS facs; |
27 |
//static double (*informat)(const char *,
|
28 |
// char **); /* input data deformatter function */
|
29 |
|
30 |
|
31 |
/************************************************************************/
|
32 |
/* process() */
|
33 |
/* */
|
34 |
/* File processing function. */
|
35 |
/************************************************************************/
|
36 |
//static void process(FILE *fid)
|
37 |
//
|
38 |
//{
|
39 |
// char line[MAX_LINE+3], *s, pline[40];
|
40 |
// projUV data;
|
41 |
//
|
42 |
// for (;;) {
|
43 |
// double z;
|
44 |
//
|
45 |
// ++emess_dat.File_line;
|
46 |
// if (!(s = fgets(line, MAX_LINE, fid)))
|
47 |
// break;
|
48 |
// if (!strchr(s, '\n')) { /* overlong line */
|
49 |
// int c;
|
50 |
// (void)strcat(s, "\n");
|
51 |
// /* gobble up to newline */
|
52 |
// while ((c = fgetc(fid)) != EOF && c != '\n') ;
|
53 |
// }
|
54 |
// if (*s == tag) {
|
55 |
// fputs(line, stdout);
|
56 |
// continue;
|
57 |
// }
|
58 |
//
|
59 |
// if (reversein) {
|
60 |
// data.v = (*informat)(s, &s);
|
61 |
// data.u = (*informat)(s, &s);
|
62 |
// } else {
|
63 |
// data.u = (*informat)(s, &s);
|
64 |
// data.v = (*informat)(s, &s);
|
65 |
// }
|
66 |
//
|
67 |
// z = strtod( s, &s );
|
68 |
//
|
69 |
// if (data.v == HUGE_VAL)
|
70 |
// data.u = HUGE_VAL;
|
71 |
//
|
72 |
// if (!*s && (s > line)) --s; /* assumed we gobbled \n */
|
73 |
//
|
74 |
// if ( echoin) {
|
75 |
// int t;
|
76 |
// t = *s;
|
77 |
// *s = '\0';
|
78 |
// (void)fputs(line, stdout);
|
79 |
// *s = t;
|
80 |
// putchar('\t');
|
81 |
// }
|
82 |
//
|
83 |
// if (data.u != HUGE_VAL) {
|
84 |
// if( pj_transform( fromProj, toProj, 1, 0,
|
85 |
// &(data.u), &(data.v), &z ) != 0 )
|
86 |
// {
|
87 |
// data.u = HUGE_VAL;
|
88 |
// data.v = HUGE_VAL;
|
89 |
// }
|
90 |
// }
|
91 |
//
|
92 |
// if (data.u == HUGE_VAL) /* error output */
|
93 |
// fputs(oterr, stdout);
|
94 |
//
|
95 |
// else if (pj_is_latlong(toProj) && !oform) { /*ascii DMS output */
|
96 |
// if (reverseout) {
|
97 |
// fputs(rtodms(pline, data.v, 'N', 'S'), stdout);
|
98 |
// putchar('\t');
|
99 |
// fputs(rtodms(pline, data.u, 'E', 'W'), stdout);
|
100 |
// } else {
|
101 |
// fputs(rtodms(pline, data.u, 'E', 'W'), stdout);
|
102 |
// putchar('\t');
|
103 |
// fputs(rtodms(pline, data.v, 'N', 'S'), stdout);
|
104 |
// }
|
105 |
//
|
106 |
// } else { /* x-y or decimal degree ascii output */
|
107 |
// if ( pj_is_latlong(toProj) ) {
|
108 |
// data.v *= RAD_TO_DEG;
|
109 |
// data.u *= RAD_TO_DEG;
|
110 |
// }
|
111 |
// if (reverseout) {
|
112 |
// printf(oform,data.v); putchar('\t');
|
113 |
// printf(oform,data.u);
|
114 |
// } else {
|
115 |
// printf(oform,data.u); putchar('\t');
|
116 |
// printf(oform,data.v);
|
117 |
// }
|
118 |
// }
|
119 |
//
|
120 |
// putchar(' ');
|
121 |
// if( oform != NULL )
|
122 |
// printf( oform, z );
|
123 |
// else
|
124 |
// printf( "%.3f", z );
|
125 |
// fputs("\n", stdout );
|
126 |
// }
|
127 |
//}
|
128 |
|
129 |
/************************************************************************/
|
130 |
/* main() */
|
131 |
/************************************************************************/
|
132 |
|
133 |
int libcs2cs(int argc,char** argv,double* input1,double* input2,double* input3) |
134 |
{ |
135 |
char *arg, **eargv = argv, *from_argv[MAX_PARGS], *to_argv[MAX_PARGS],
|
136 |
**iargv = argv; |
137 |
FILE *fid; |
138 |
int from_argc=0, to_argc=0, iargc = argc, eargc = 0, c, mon = 0; |
139 |
int have_to_flag = 0, inverse = 0, i; |
140 |
|
141 |
if (emess_dat.Prog_name = strrchr(*argv,DIR_CHAR))
|
142 |
++emess_dat.Prog_name; |
143 |
else emess_dat.Prog_name = *argv;
|
144 |
inverse = ! strncmp(emess_dat.Prog_name, "inv", 3); |
145 |
if (argc <= 1 ) { |
146 |
(void)fprintf(stderr, usage, pj_get_release(), emess_dat.Prog_name);
|
147 |
exit (0);
|
148 |
} |
149 |
/* process run line arguments */
|
150 |
while (--argc > 0) { /* collect run line arguments */ |
151 |
if(**++argv == '-') for(arg = *argv;;) { |
152 |
switch(*++arg) {
|
153 |
case '\0': /* position of "stdin" */ |
154 |
if (arg[-1] == '-') eargv[eargc++] = "-"; |
155 |
break;
|
156 |
case 'v': /* monitor dump of initialization */ |
157 |
mon = 1;
|
158 |
continue;
|
159 |
case 'I': /* alt. method to spec inverse */ |
160 |
inverse = 1;
|
161 |
continue;
|
162 |
case 'E': /* echo ascii input to ascii output */ |
163 |
echoin = 1;
|
164 |
continue;
|
165 |
case 't': /* set col. one char */ |
166 |
if (arg[1]) tag = *++arg; |
167 |
else emess(1,"missing -t col. 1 tag"); |
168 |
continue;
|
169 |
case 'l': /* list projections, ellipses or units */ |
170 |
if (!arg[1] || arg[1] == 'p' || arg[1] == 'P') { |
171 |
/* list projections */
|
172 |
struct PJ_LIST *lp;
|
173 |
int do_long = arg[1] == 'P', c; |
174 |
char *str;
|
175 |
|
176 |
for (lp = pj_get_list_ref() ; lp->id ; ++lp) {
|
177 |
(void)printf("%s : ", lp->id); |
178 |
if (do_long) /* possibly multiline description */ |
179 |
(void)puts(*lp->descr);
|
180 |
else { /* first line, only */ |
181 |
str = *lp->descr; |
182 |
while ((c = *str++) && c != '\n') |
183 |
putchar(c); |
184 |
putchar('\n');
|
185 |
} |
186 |
} |
187 |
} else if (arg[1] == '=') { /* list projection 'descr' */ |
188 |
struct PJ_LIST *lp;
|
189 |
|
190 |
arg += 2;
|
191 |
for (lp = pj_get_list_ref() ; lp->id ; ++lp)
|
192 |
if (!strcmp(lp->id, arg)) {
|
193 |
(void)printf("%9s : %s\n", lp->id, *lp->descr); |
194 |
break;
|
195 |
} |
196 |
} else if (arg[1] == 'e') { /* list ellipses */ |
197 |
struct PJ_ELLPS *le;
|
198 |
|
199 |
for (le = pj_get_ellps_ref(); le->id ; ++le)
|
200 |
(void)printf("%9s %-16s %-16s %s\n", |
201 |
le->id, le->major, le->ell, le->name); |
202 |
} else if (arg[1] == 'u') { /* list units */ |
203 |
struct PJ_UNITS *lu;
|
204 |
|
205 |
for (lu = pj_get_units_ref(); lu->id ; ++lu)
|
206 |
(void)printf("%12s %-20s %s\n", |
207 |
lu->id, lu->to_meter, lu->name); |
208 |
} else if (arg[1] == 'd') { /* list datums */ |
209 |
struct PJ_DATUMS *ld;
|
210 |
|
211 |
printf("__datum_id__ __ellipse___ __definition/comments______________________________\n" );
|
212 |
for (ld = pj_get_datums_ref(); ld->id ; ++ld)
|
213 |
{ |
214 |
printf("%12s %-12s %-30s\n",
|
215 |
ld->id, ld->ellipse_id, ld->defn); |
216 |
if( ld->comments != NULL && strlen(ld->comments) > 0 ) |
217 |
printf( "%25s %s\n", " ", ld->comments ); |
218 |
} |
219 |
} else if( arg[1] == 'm') { /* list prime meridians */ |
220 |
struct PJ_PRIME_MERIDIANS *lpm;
|
221 |
|
222 |
for (lpm = pj_get_prime_meridians_ref(); lpm->id ; ++lpm)
|
223 |
(void)printf("%12s %-30s\n", |
224 |
lpm->id, lpm->defn); |
225 |
} else
|
226 |
emess(1,"invalid list option: l%c",arg[1]); |
227 |
exit(0);
|
228 |
continue; /* artificial */ |
229 |
case 'e': /* error line alternative */ |
230 |
if (--argc <= 0) |
231 |
noargument: |
232 |
emess(1,"missing argument for -%c",*arg); |
233 |
oterr = *++argv; |
234 |
continue;
|
235 |
case 'W': /* specify seconds precision */ |
236 |
case 'w': /* -W for constant field width */ |
237 |
if ((c = arg[1]) != 0 && isdigit(c)) { |
238 |
set_rtodms(c - '0', *arg == 'W'); |
239 |
++arg; |
240 |
} else
|
241 |
emess(1,"-W argument missing or non-digit"); |
242 |
continue;
|
243 |
case 'f': /* alternate output format degrees or xy */ |
244 |
if (--argc <= 0) goto noargument; |
245 |
oform = *++argv; |
246 |
continue;
|
247 |
case 'r': /* reverse input */ |
248 |
reversein = 1;
|
249 |
continue;
|
250 |
case 's': /* reverse output */ |
251 |
reverseout = 1;
|
252 |
continue;
|
253 |
default:
|
254 |
emess(1, "invalid option: -%c",*arg); |
255 |
break;
|
256 |
} |
257 |
break;
|
258 |
|
259 |
} else if (strcmp(*argv,"+to") == 0 ) { |
260 |
have_to_flag = 1;
|
261 |
|
262 |
} else if (**argv == '+') { /* + argument */ |
263 |
if( have_to_flag )
|
264 |
{ |
265 |
if( to_argc < MAX_PARGS )
|
266 |
to_argv[to_argc++] = *argv + 1;
|
267 |
else
|
268 |
emess(1,"overflowed + argument table"); |
269 |
} |
270 |
else
|
271 |
{ |
272 |
if (from_argc < MAX_PARGS)
|
273 |
from_argv[from_argc++] = *argv + 1;
|
274 |
else
|
275 |
emess(1,"overflowed + argument table"); |
276 |
} |
277 |
} else /* assumed to be input file name(s) */ |
278 |
eargv[eargc++] = *argv; |
279 |
} |
280 |
if (eargc == 0 ) /* if no specific files force sysin */ |
281 |
eargv[eargc++] = "-";
|
282 |
|
283 |
/*
|
284 |
* If the user has requested inverse, then just reverse the
|
285 |
* coordinate systems.
|
286 |
*/
|
287 |
if( inverse )
|
288 |
{ |
289 |
int argcount;
|
290 |
|
291 |
for( i = 0; i < MAX_PARGS; i++ ) |
292 |
{ |
293 |
char *arg;
|
294 |
|
295 |
arg = from_argv[i]; |
296 |
from_argv[i] = to_argv[i]; |
297 |
to_argv[i] = arg; |
298 |
} |
299 |
|
300 |
argcount = from_argc; |
301 |
from_argc = to_argc; |
302 |
to_argc = argcount; |
303 |
} |
304 |
|
305 |
if (!(fromProj = pj_init(from_argc, from_argv)))
|
306 |
{ |
307 |
printf( "Using from definition: " );
|
308 |
for( i = 0; i < from_argc; i++ ) |
309 |
printf( "%s ", from_argv[i] );
|
310 |
printf( "\n" );
|
311 |
|
312 |
emess(3,"projection initialization failure\ncause: %s", |
313 |
pj_strerrno(pj_errno)); |
314 |
} |
315 |
|
316 |
if( to_argc == 0 ) |
317 |
{ |
318 |
if (!(toProj = pj_latlong_from_proj( fromProj )))
|
319 |
{ |
320 |
printf( "Using to definition: " );
|
321 |
for( i = 0; i < to_argc; i++ ) |
322 |
printf( "%s ", to_argv[i] );
|
323 |
printf( "\n" );
|
324 |
|
325 |
emess(3,"projection initialization failure\ncause: %s", |
326 |
pj_strerrno(pj_errno)); |
327 |
} |
328 |
} |
329 |
else if (!(toProj = pj_init(to_argc, to_argv))) |
330 |
{ |
331 |
printf( "Using to definition: " );
|
332 |
for( i = 0; i < to_argc; i++ ) |
333 |
printf( "%s ", to_argv[i] );
|
334 |
printf( "\n" );
|
335 |
|
336 |
emess(3,"projection initialization failure\ncause: %s", |
337 |
pj_strerrno(pj_errno)); |
338 |
} |
339 |
|
340 |
if (mon) {
|
341 |
printf( "%c ---- From Coordinate System ----\n", tag );
|
342 |
pj_pr_list(fromProj); |
343 |
printf( "%c ---- To Coordinate System ----\n", tag );
|
344 |
pj_pr_list(toProj); |
345 |
} |
346 |
|
347 |
projUV data; |
348 |
double z;
|
349 |
if (reversein)
|
350 |
{ |
351 |
data.v = *input1; |
352 |
data.u = *input2; |
353 |
} |
354 |
else
|
355 |
{ |
356 |
data.u = *input1; |
357 |
data.v = *input2; |
358 |
} |
359 |
z = *input3; |
360 |
if (data.v == HUGE_VAL) data.u = HUGE_VAL;
|
361 |
if (data.u != HUGE_VAL)
|
362 |
{ |
363 |
if( pj_transform( fromProj, toProj, 1, 0, &(data.u), &(data.v), &z ) != 0 ) |
364 |
{ |
365 |
data.u = HUGE_VAL; |
366 |
data.v = HUGE_VAL; |
367 |
} |
368 |
} |
369 |
if (data.u == HUGE_VAL) return(1);/* error output */ |
370 |
*input1=data.u; |
371 |
*input2=data.v; |
372 |
*input3=z; |
373 |
|
374 |
if( fromProj != NULL ) |
375 |
pj_free( fromProj ); |
376 |
if( toProj != NULL ) |
377 |
pj_free( toProj ); |
378 |
|
379 |
pj_deallocate_grids(); |
380 |
|
381 |
return(0); /* normal completion */ |
382 |
} |