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