svn-gvsig-desktop / branches / v10 / libraries / libjni-proj4 / src / rtodms.c @ 12472
History | View | Annotate | Download (1.58 KB)
1 |
/* Convert radian argument to DMS ascii format */
|
---|---|
2 |
#ifndef lint
|
3 |
static const char SCCSID[]="@(#)rtodms.c 4.3 93/06/12 GIE REL"; |
4 |
#endif
|
5 |
#include <projects.h> |
6 |
#include <stdio.h> |
7 |
#include <string.h> |
8 |
/*
|
9 |
** RES is fractional second figures
|
10 |
** RES60 = 60 * RES
|
11 |
** CONV = 180 * 3600 * RES / PI (radians to RES seconds)
|
12 |
*/
|
13 |
static double |
14 |
RES = 1000., |
15 |
RES60 = 60000., |
16 |
CONV = 206264806.24709635515796003417; |
17 |
static char |
18 |
format[50] = "%dd%d'%.3f\"%c"; |
19 |
static int |
20 |
dolong = 0;
|
21 |
void
|
22 |
set_rtodms(int fract, int con_w) { |
23 |
int i;
|
24 |
|
25 |
if (fract >= 0 && fract < 9 ) { |
26 |
RES = 1.;
|
27 |
/* following not very elegant, but used infrequently */
|
28 |
for (i = 0; i < fract; ++i) |
29 |
RES *= 10.; |
30 |
RES60 = RES * 60.; |
31 |
CONV = 180. * 3600. * RES / PI; |
32 |
if (! con_w)
|
33 |
(void)sprintf(format,"%%dd%%d'%%.%df\"%%c", fract); |
34 |
else
|
35 |
(void)sprintf(format,"%%dd%%02d'%%0%d.%df\"%%c", |
36 |
fract+2+(fract?1:0), fract); |
37 |
dolong = con_w; |
38 |
} |
39 |
} |
40 |
char *
|
41 |
rtodms(char *s, double r, int pos, int neg) { |
42 |
int deg, min, sign;
|
43 |
char *ss = s;
|
44 |
double sec;
|
45 |
|
46 |
if (r < 0) { |
47 |
r = -r; |
48 |
if (!pos) { *ss++ = '-'; sign = 0; } |
49 |
else sign = neg;
|
50 |
} else
|
51 |
sign = pos; |
52 |
r = floor(r * CONV + .5);
|
53 |
sec = fmod(r / RES, 60.); |
54 |
r = floor(r / RES60); |
55 |
min = fmod(r, 60.); |
56 |
deg = r / 60.; |
57 |
if (dolong)
|
58 |
(void)sprintf(ss,format,deg,min,sec,sign);
|
59 |
else if (sec) { |
60 |
char *p, *q;
|
61 |
|
62 |
(void)sprintf(ss,format,deg,min,sec,sign);
|
63 |
for (q = p = ss + strlen(ss) - (sign ? 3 : 2); *p == '0'; --p) ; |
64 |
if (*p != '.') |
65 |
++p; |
66 |
if (++q != p)
|
67 |
(void)strcpy(p, q);
|
68 |
} else if (min) |
69 |
(void)sprintf(ss,"%dd%d'%c",deg,min,sign); |
70 |
else
|
71 |
(void)sprintf(ss,"%dd%c",deg, sign); |
72 |
return s;
|
73 |
} |