File: ll_format.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (200 lines) | stat: -rw-r--r-- 3,817 bytes parent folder | download | duplicates (2)
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
/***************************************************************
G_lat_format (lat, buf)
    double lat;
    char *buf;

G_lon_format (lon, buf)
    double lon;
    char *buf;

G_llres_format (res, buf)
    double res;
    char *buf;

  formats lat (latitude in degrees), or lon (longitude in degrees)
  into buf as dd:mm:ssH, where H (hemishpere) is
      N for nothern hemishpere, S for southern,
      W for western hemishpere, E for eastern
      none for resolution
  (lat > 0 is northern, lat < 0 is southern)
  (lon > 0 is eastern,  lon < 0 is western)

Note: lat should be in the range -90 to 90s, but
	  the range is NOT checked by G_lat_format().
      lon can be anything, but
	  values outside [-180,180] are moved into this range 
          by adding (or subtracting) 360.

NOTE: These routines are used by G_format_northing(), G_format_easting(), and
      G_format_resolution(). Those routines are intended to provide
      a general interface to window values and should be used instead of
      these projection specific routines. In other words, these routines
      are for the library only, programmers shouldn't use them.
***************************************************************/
#include "gis.h"
#include <string.h>

static int format ( char *, int,int, double, char);
static int ll_parts( double, int *,int *, double *);

int G_lat_format (double lat, char *buf)
{
    int d,m;
    char h;
    double s;

    G_lat_parts (lat, &d, &m, &s, &h);
    format (buf, d,m,s,h);

    return 0;
}

char *G_lat_format_string (void)
{
    return "dd:mm:ss{N|S}";
}

int G_lon_format (double lon, char *buf)
{
    int d,m;
    char h;
    double s;

    G_lon_parts (lon, &d, &m, &s, &h);
    format (buf, d,m,s,h);

    return 0;
}
char *
G_lon_format_string (void)
{
    return "ddd:mm:ss{E|W}";
}

int 
G_llres_format (double res, char *buf)
{
    int d,m;
    char h;
    double s;

    G_lat_parts (res, &d, &m, &s, &h);
    h = 0;
    format (buf, d,m,s,h);

    return 0;
}
char *
G_llres_format_string (void)
{
    return "dd:mm:ss";
}


static int format (
    char *buf,
    int d,int m,
    double s,
    char h)
{
    char temp[50];
    double ss;

    sprintf (temp, "%f", s);
    sscanf (temp, "%lf", &ss);
    if (ss >= 60)
    {
	ss = 0;	/* force it to zero */
	if (++m >= 60)
	{
	    m = 0;
	    d++;
	}
    }

    if (ss < 10.0)
	sprintf (temp, "0%f", ss);
    else
	sprintf (temp, "%f", ss);
    G_trim_decimal (temp);
    if (strcmp(temp,"00") != 0 && strcmp(temp,"0") != 0)
	sprintf (buf, "%d:%02d:%s%c", d, m, temp, h);
    else if (m > 0)
	sprintf (buf, "%d:%02d%c", d, m, h);
    else if (d > 0)
	sprintf (buf, "%d%c", d, h);
    else
	sprintf (buf, "0");

    return 0;
}

int G_lat_parts (
    double lat,     /* lat in degrees to be split into parts */
    int *d,
    int *m,     /* degrees, minutes */
    double *s,      /* seconds */
    char *h        /* hemisphere */
)
{
    if (lat < 0)
    {
	*h = 'S' ;
	lat = -lat;
    }
    else
	*h = 'N' ;

    ll_parts (lat, d, m, s);

    return 0;
}

int G_lon_parts (
    double lon,	    /* lon in degrees to be split into parts */
    int *d,
    int *m,     /* degrees, minutes */
    double *s,      /* seconds */
    char *h        /* hemisphere */
)
{
    while (lon > 180.0)
	lon -= 360.0;
    while (lon < -180.0)
	lon += 360.0;

    if (lon < 0)
    {
	*h = 'W' ;
	lon = -lon;
    }
    else
	*h = 'E' ;

    ll_parts (lon, d, m, s);

    return 0;
}

static int ll_parts(
    double ll,	/* ll in degrees to be split into parts */
    int *d,int *m, /* degrees, minutes */
    double *s)  /* seconds */
{
    if (ll == 0.0)
    {
	*d = 0;
	*m = 0;
	*s = 0.0;
    }
    else
    {
	*d = ll ;
	*m = (ll - *d) * 60;
	if (*m < 0) *m = 0;
	*s = ((ll - *d) * 60 - *m) * 60 ;
	if (*s < 0) *s = 0;
    }

    return 0;
}