GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ll_scan.c
Go to the documentation of this file.
1 
2 /******************************************************************************
3  G_lat_scan (buf, lat)
4  char *buf;
5  double *lat;
6 
7  G_lon_scan (buf, lon)
8  char *buf;
9  double *lon;
10 
11  G_llres_scan (buf, res)
12  char *buf;
13  double *res;
14 
15  Convert ascii string representations of latitude/longitude to a double.
16  The string format is:
17 
18  dd:mm:ss.ffh
19 
20  where:
21  dd is degrees, 0-90 for latitude, 0-180 for longitude
22  mm is minutes, 0-59
23  ss is seconds, 0-59
24  ff is fractions of a second, >= 0
25  h is 'n' or 's' for latitude,
26  'e' or 'w' for longitude.
27  missing for resolution
28 
29  lat (or lon) is set to the double value for the lat/lon represented in buf.
30 
31  lat is always in the range -90 thru 90,
32  lon is always in the range -180 thru 180.
33 
34  note: southern latitude and western longitude are returned as negative values.
35 
36  returns 1 if input format is ok, 0 otherwise.
37 ******************************************************************************/
38 #include <grass/gis.h>
39 
40 static int scan_ll(const char *, const char *, double *, int);
41 static int check_minutes(const char *);
42 static int check_seconds(const char *);
43 
44 int G_lat_scan(const char *buf, double *lat)
45 {
46  return scan_ll(buf, "sn", lat, 90);
47 }
48 
49 int G_lon_scan(const char *buf, double *lon)
50 {
51  return scan_ll(buf, "we", lon, 180);
52 }
53 
54 int G_llres_scan(const char *buf, double *res)
55 {
56  char tbuf[100];
57 
58  sprintf(tbuf, "%se", buf);
59  return scan_ll(tbuf, "we", res, 0);
60 }
61 
62 #define MARKER 1
63 static int scan_ll(const char *buf, const char *dir, double *result, int max)
64 {
65  char h[100];
66  int d, m, s;
67  char ps[20], *pps;
68  double p, f;
69  double pm = 0.0;
70  char tbuf[100];
71 
72  sprintf(tbuf, "%s%c", buf, MARKER); /* add a marker at end of string */
73  buf = tbuf;
74 
75  if (sscanf(buf, "%d:%d:%d.%[0123456789]%[^\n]", &d, &m, &s, ps, h) == 5) {
76  p = 0.0;
77  f = .1;
78  for (pps = ps; *pps; pps++) {
79  p += (*pps - '0') * f;
80  f /= 10.0;
81  }
82  }
83  else if (sscanf(buf, "%d:%d:%d%[^\n]", &d, &m, &s, h) == 4) {
84  p = 0.0;
85  }
86  else if (sscanf(buf, "%d:%d.%[0123456789]%[^\n]", &d, &m, ps, h) == 4) {
87  s = 0;
88  p = 0.0;
89  f = .1;
90  for (pps = ps; *pps; pps++) {
91  pm += (*pps - '0') * f;
92  f /= 10.0;
93  }
94  }
95  else if (sscanf(buf, "%d:%d%[^\n]", &d, &m, h) == 3) {
96  p = 0.0;
97  s = 0;
98  }
99  else if (sscanf(buf, "%d%[^\n]", &d, h) == 2) {
100  p = 0.0;
101  s = m = 0;
102  }
103  else
104  return 0;
105 
106  if (d < 0)
107  return 0;
108  if (m < 0 || m >= 60)
109  return 0;
110  if (s < 0 || s >= 60)
111  return 0;
112 
113  if (max) {
114  if (d > max)
115  return 0;
116  if (d == max && (m > 0 || s > 0 || p > 0.0))
117  return 0;
118  }
119 
120  if (m && !check_minutes(buf))
121  return 0;
122  if (s && !check_seconds(buf))
123  return 0;
124 
125  *result = d + (m + pm) / 60.0 + (s + p) / 3600.0;
126 
127  G_strip(h);
128 
129  if (*result == 0.0 && *h == MARKER)
130  return (1);
131 
132  if (*h >= 'A' && *h <= 'Z')
133  *h += 'a' - 'A';
134 
135  if (*h != dir[0] && *h != dir[1])
136  return 0;
137 
138  if (h[1] != MARKER)
139  return 0;
140 
141  if (*h == dir[0] && *result != 0.0)
142  *result = -(*result);
143 
144  return 1;
145 }
146 
147 static int check_minutes(const char *buf)
148 {
149  /* skip over degrees */
150  while (*buf != ':')
151  if (*buf++ == 0)
152  return 1;
153  buf++;
154 
155  /* must have 2 digits for minutes */
156  if (*buf < '0' || *buf > '9')
157  return 0;
158  buf++;
159  if (*buf < '0' || *buf > '9')
160  return 0;
161  buf++;
162  return (*buf < '0' || *buf > '9');
163 }
164 
165 static int check_seconds(const char *buf)
166 {
167  /* skip over degrees */
168  while (*buf != ':')
169  if (*buf++ == 0)
170  return 1;
171  buf++;
172  /* skip over minutes */
173  while (*buf != ':')
174  if (*buf++ == 0)
175  return 1;
176  buf++;
177 
178  /* must have 2 digits for seconds */
179  if (*buf < '0' || *buf > '9')
180  return 0;
181  buf++;
182  if (*buf < '0' || *buf > '9')
183  return 0;
184  buf++;
185  return (*buf < '0' || *buf > '9');
186 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
#define MARKER
Definition: ll_scan.c:62
#define max(x, y)
Definition: draw2.c:69
int G_lat_scan(const char *buf, double *lat)
Definition: ll_scan.c:44
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int G_llres_scan(const char *buf, double *res)
Definition: ll_scan.c:54
tuple h
panel.defaultSize = wx.CheckBox(panel, id = wx.ID_ANY, label = _(&quot;Use default size&quot;)) panel...
int G_lon_scan(const char *buf, double *lon)
Definition: ll_scan.c:49