GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-835afb4352
ll_format.c
Go to the documentation of this file.
1 /***************************************************************
2 G_lat_format (lat, buf)
3  double lat;
4  char *buf;
5 
6 G_lon_format (lon, buf)
7  double lon;
8  char *buf;
9 
10 G_llres_format (res, buf)
11  double res;
12  char *buf;
13 
14  formats lat (latitude in degrees), or lon (longitude in degrees)
15  into buf as dd:mm:ssH, where H (hemisphere) is
16  N for northern hemisphere, S for southern,
17  W for western hemisphere, E for eastern
18  none for resolution
19  (lat > 0 is northern, lat < 0 is southern)
20  (lon > 0 is eastern, lon < 0 is western)
21 
22 Note: lat should be in the range -90 to 90s, but
23  the range is NOT checked by G_lat_format().
24  lon can be anything, but
25  values outside [-180,180] are moved into this range
26  by adding (or subtracting) 360.
27 
28 NOTE: These routines are used by G_format_northing(), G_format_easting(), and
29  G_format_resolution(). Those routines are intended to provide
30  a general interface to window values and should be used instead of
31  these projection specific routines. In other words, these routines
32  are for the library only, programmers shouldn't use them.
33 ***************************************************************/
34 #include <grass/gis.h>
35 #include <string.h>
36 
37 static void format(char *, int, int, double, char);
38 static void ll_parts(double, int *, int *, double *);
39 
40 void G_lat_format(double lat, char *buf)
41 {
42  int d, m;
43  char h;
44  double s;
45 
46  G_lat_parts(lat, &d, &m, &s, &h);
47  format(buf, d, m, s, h);
48 }
49 
50 const char *G_lat_format_string(void)
51 {
52  return "dd:mm:ss{N|S}";
53 }
54 
55 void G_lon_format(double lon, char *buf)
56 {
57  int d, m;
58  char h;
59  double s;
60 
61  G_lon_parts(lon, &d, &m, &s, &h);
62  format(buf, d, m, s, h);
63 }
64 
65 const char *G_lon_format_string(void)
66 {
67  return "ddd:mm:ss{E|W}";
68 }
69 
70 void G_llres_format(double res, char *buf)
71 {
72  int d, m;
73  char h;
74  double s;
75 
76  G_lat_parts(res, &d, &m, &s, &h);
77  h = 0;
78  format(buf, d, m, s, h);
79 }
80 
81 const char *G_llres_format_string(void)
82 {
83  return "dd:mm:ss";
84 }
85 
86 static void format(char *buf, int d, int m, double s, char h)
87 {
88  char temp[50];
89  double ss;
90 
91  sprintf(temp, "%f", s);
92  sscanf(temp, "%lf", &ss);
93  if (ss >= 60) {
94  ss = 0; /* force it to zero */
95  if (++m >= 60) {
96  m = 0;
97  d++;
98  }
99  }
100 
101  if (ss < 10.0)
102  sprintf(temp, "0%f", ss);
103  else
104  sprintf(temp, "%f", ss);
105  G_trim_decimal(temp);
106  if (strcmp(temp, "00") != 0 && strcmp(temp, "0") != 0)
107  sprintf(buf, "%d:%02d:%s%c", d, m, temp, h);
108  else if (m > 0)
109  sprintf(buf, "%d:%02d%c", d, m, h);
110  else if (d > 0)
111  sprintf(buf, "%d%c", d, h);
112  else
113  sprintf(buf, "0");
114 }
115 
116 void G_lat_parts(double lat, /* lat in degrees to be split into parts */
117  int *d, int *m, /* degrees, minutes */
118  double *s, /* seconds */
119  char *h /* hemisphere */
120 )
121 {
122  if (lat < 0) {
123  *h = 'S';
124  lat = -lat;
125  }
126  else
127  *h = 'N';
128 
129  ll_parts(lat, d, m, s);
130 }
131 
132 void G_lon_parts(double lon, /* lon in degrees to be split into parts */
133  int *d, int *m, /* degrees, minutes */
134  double *s, /* seconds */
135  char *h /* hemisphere */
136 )
137 {
138 #if 0
139  while (lon > 180.0)
140  lon -= 360.0;
141  while (lon < -180.0)
142  lon += 360.0;
143 #endif
144 
145  if (lon < 0) {
146  *h = 'W';
147  lon = -lon;
148  }
149  else
150  *h = 'E';
151 
152  ll_parts(lon, d, m, s);
153 }
154 
155 static void ll_parts(double ll, /* ll in degrees to be split into parts */
156  int *d, int *m, /* degrees, minutes */
157  double *s /* seconds */
158 )
159 {
160  if (ll == 0.0) {
161  *d = 0;
162  *m = 0;
163  *s = 0.0;
164  }
165  else {
166  *d = ll;
167  *m = (ll - *d) * 60;
168  if (*m < 0)
169  *m = 0;
170  *s = ((ll - *d) * 60 - *m) * 60;
171  if (*s < 0)
172  *s = 0;
173  }
174 }
void G_trim_decimal(char *)
Removes trailing zeros from decimal number.
Definition: trim_dec.c:24
void G_lat_parts(double lat, int *d, int *m, double *s, char *h)
Definition: ll_format.c:116
void G_lat_format(double lat, char *buf)
Definition: ll_format.c:40
void G_llres_format(double res, char *buf)
Definition: ll_format.c:70
const char * G_lon_format_string(void)
Definition: ll_format.c:65
void G_lon_format(double lon, char *buf)
Definition: ll_format.c:55
const char * G_llres_format_string(void)
Definition: ll_format.c:81
void G_lon_parts(double lon, int *d, int *m, double *s, char *h)
Definition: ll_format.c:132
const char * G_lat_format_string(void)
Definition: ll_format.c:50