GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
rd_cellhd.c
Go to the documentation of this file.
1 
14 #include <string.h>
15 
16 #include <grass/gis.h>
17 #include <grass/glocale.h>
18 
19 #define ERROR(x,line) return error(x,line)
20 static int scan_item(const char *, char *, char *);
21 static int scan_int(const char *, int *);
22 static double scan_double(const char *, double *);
23 static char *error(const char *, int);
24 
25 #define F_PROJ 1
26 #define F_ZONE 2
27 #define F_NORTH 3
28 #define F_SOUTH 4
29 #define F_EAST 5
30 #define F_WEST 6
31 #define F_EWRES 7
32 #define F_NSRES 8
33 #define F_FORMAT 9
34 #define F_COMP 10
35 #define F_COLS 11
36 #define F_ROWS 12
37 
38 #define F_EWRES3 13
39 #define F_NSRES3 14
40 #define F_COLS3 15
41 #define F_ROWS3 16
42 #define F_TOP 17
43 #define F_BOTTOM 18
44 #define F_TBRES 19
45 #define F_DEPTHS 20
46 
47 #define SET(x) flags|=(1<<x)
48 #define TEST(x) (flags&(1<<x))
49 
50 char *G__read_Cell_head_array(char **array,
51  struct Cell_head *cellhd, int is_cellhd);
52 
53 char *G__read_Cell_head(FILE * fd, struct Cell_head *cellhd, int is_cellhd)
54 {
55  int count;
56  char *result, **array;
57  char buf[1024];
58 
59  G_debug(2, "G__read_Cell_head");
60 
61  /* Count lines */
62  count = 0;
63  fseek(fd, 0L, 0);
64  while (G_getl(buf, sizeof(buf), fd))
65  count++;
66 
67  array = (char **)G_calloc(count + 1, sizeof(char *));
68 
69  count = 0;
70  fseek(fd, 0L, 0);
71  while (G_getl(buf, sizeof(buf), fd)) {
72  array[count] = G_store(buf);
73  count++;
74  }
75 
76  result = G__read_Cell_head_array(array, cellhd, is_cellhd);
77 
78  count = 0;
79  while (array[count]) {
80  G_free(array[count]);
81  count++;
82  }
83  G_free(array);
84 
85  return result;
86 }
87 
88 /* Read window from NULL terminated array of strings */
89 char *G__read_Cell_head_array(char **array,
90  struct Cell_head *cellhd, int is_cellhd)
91 {
92  char *buf;
93  char label[200];
94  char value[200];
95  int i, line;
96  int flags;
97  char *G_adjust_Cell_head();
98  char *err;
99 
100  G_debug(2, "G__read_Cell_head_array");
101 
102  flags = 0;
103 
104  /* initialize the cell header */
105  cellhd->format = 0;
106  cellhd->rows = 0;
107  cellhd->rows3 = 0;
108  cellhd->cols = 0;
109  cellhd->cols3 = 0;
110  cellhd->depths = 1;
111  cellhd->proj = -1;
112  cellhd->zone = -1;
113  cellhd->compressed = -1;
114  cellhd->ew_res = 0.0;
115  cellhd->ew_res3 = 1.0;
116  cellhd->ns_res = 0.0;
117  cellhd->ns_res3 = 1.0;
118  cellhd->tb_res = 1.0;
119  cellhd->north = 0.0;
120  cellhd->south = 0.0;
121  cellhd->east = 0.0;
122  cellhd->west = 0.0;
123  cellhd->top = 1.0;
124  cellhd->bottom = 0.0;
125 
126  /* determine projection, zone first */
127 
128  i = 0;
129  for (line = 1; (buf = array[i++]); line++) {
130  if (TEST(F_PROJ) && TEST(F_ZONE))
131  break;
132 
133  switch (scan_item(buf, label, value)) {
134  case -1:
135  ERROR(buf, line);
136  case 0:
137  continue;
138  case 1:
139  break;
140  }
141  if (strncmp(label, "proj", 4) == 0) {
142  if (TEST(F_PROJ))
143  ERROR(_("duplicate projection field"), line);
144 
145  if (!scan_int(value, &cellhd->proj))
146  ERROR(buf, line);
147 
148  SET(F_PROJ);
149  continue;
150  }
151  if (strncmp(label, "zone", 4) == 0) {
152  if (TEST(F_ZONE))
153  ERROR(_("duplicate zone field"), line);
154 
155  if (!scan_int(value, &cellhd->zone))
156  ERROR(buf, line);
157 
158  SET(F_ZONE);
159  continue;
160  }
161  }
162  if (!TEST(F_PROJ))
163  ERROR(_("projection field missing"), 0);
164  if (!TEST(F_ZONE))
165  ERROR(_("zone field missing"), 0);
166 
167  /* read the other info */
168  i = 0;
169  for (line = 1; (buf = array[i++]); line++) {
170  G_debug(3, "region item: %s", buf);
171  switch (scan_item(buf, label, value)) {
172  case -1:
173  ERROR(buf, line);
174  case 0:
175  continue;
176  case 1:
177  break;
178  }
179 
180  if (strncmp(label, "proj", 4) == 0)
181  continue;
182  if (strncmp(label, "zone", 4) == 0)
183  continue;
184 
185  if (strncmp(label, "nort", 4) == 0) {
186  if (TEST(F_NORTH))
187  ERROR(_("duplicate north field"), line);
188  if (!G_scan_northing(value, &cellhd->north, cellhd->proj))
189  ERROR(buf, line);
190  SET(F_NORTH);
191  continue;
192  }
193  if (strncmp(label, "sout", 4) == 0) {
194  if (TEST(F_SOUTH))
195  ERROR(_("duplicate south field"), line);
196  if (!G_scan_northing(value, &cellhd->south, cellhd->proj))
197  ERROR(buf, line);
198  SET(F_SOUTH);
199  continue;
200  }
201  if (strncmp(label, "east", 4) == 0) {
202  if (TEST(F_EAST))
203  ERROR(_("duplicate east field"), line);
204  if (!G_scan_easting(value, &cellhd->east, cellhd->proj))
205  ERROR(buf, line);
206  SET(F_EAST);
207  continue;
208  }
209  if (strncmp(label, "west", 4) == 0) {
210  if (TEST(F_WEST))
211  ERROR(_("duplicate west field"), line);
212  if (!G_scan_easting(value, &cellhd->west, cellhd->proj))
213  ERROR(buf, line);
214  SET(F_WEST);
215  continue;
216  }
217  if (strncmp(label, "top", 3) == 0) {
218  if (TEST(F_TOP))
219  ERROR(_("duplicate top field"), line);
220  if (!scan_double(value, &cellhd->top))
221  ERROR(buf, line);
222  SET(F_TOP);
223  continue;
224  }
225  if (strncmp(label, "bottom", 6) == 0) {
226  if (TEST(F_BOTTOM))
227  ERROR(_("duplicate bottom field"), line);
228  if (!scan_double(value, &cellhd->bottom))
229  ERROR(buf, line);
230  SET(F_BOTTOM);
231  continue;
232  }
233  if (strncmp(label, "e-w ", 4) == 0 && strlen(label) == 9) {
234  if (TEST(F_EWRES))
235  ERROR(_("duplicate e-w resolution field"), line);
236  if (!G_scan_resolution(value, &cellhd->ew_res, cellhd->proj))
237  ERROR(buf, line);
238  if (cellhd->ew_res <= 0.0)
239  ERROR(buf, line);
240  SET(F_EWRES);
241  continue;
242  }
243  if (strncmp(label, "e-w resol3", 10) == 0) {
244  if (TEST(F_EWRES3))
245  ERROR(_("duplicate 3D e-w resolution field"), line);
246  if (!G_scan_resolution(value, &cellhd->ew_res3, cellhd->proj))
247  ERROR(buf, line);
248  if (cellhd->ew_res3 <= 0.0)
249  ERROR(buf, line);
250  SET(F_EWRES3);
251  continue;
252  }
253  if (strncmp(label, "n-s ", 4) == 0 && strlen(label) == 9) {
254  if (TEST(F_NSRES))
255  ERROR(_("duplicate n-s resolution field"), line);
256  if (!G_scan_resolution(value, &cellhd->ns_res, cellhd->proj))
257  ERROR(buf, line);
258  if (cellhd->ns_res <= 0.0)
259  ERROR(buf, line);
260  SET(F_NSRES);
261  continue;
262  }
263  if (strncmp(label, "n-s resol3", 10) == 0) {
264  if (TEST(F_NSRES3))
265  ERROR(_("duplicate 3D n-s resolution field"), line);
266  if (!G_scan_resolution(value, &cellhd->ns_res3, cellhd->proj))
267  ERROR(buf, line);
268  if (cellhd->ns_res3 <= 0.0)
269  ERROR(buf, line);
270  SET(F_NSRES3);
271  continue;
272  }
273  if (strncmp(label, "t-b ", 4) == 0) {
274  if (TEST(F_TBRES))
275  ERROR(_("duplicate t-b resolution field"), line);
276  if (!scan_double(value, &cellhd->tb_res))
277  ERROR(buf, line);
278  if (cellhd->tb_res <= 0.0)
279  ERROR(buf, line);
280  SET(F_TBRES);
281  continue;
282  }
283  if (strncmp(label, "rows", 4) == 0 && strlen(label) == 4) {
284  if (TEST(F_ROWS))
285  ERROR(_("duplicate rows field"), line);
286  if (!scan_int(value, &cellhd->rows))
287  ERROR(buf, line);
288  if (cellhd->rows <= 0)
289  ERROR(buf, line);
290  SET(F_ROWS);
291  continue;
292  }
293  if (strncmp(label, "rows3", 5) == 0) {
294  if (TEST(F_ROWS3))
295  ERROR(_("duplicate 3D rows field"), line);
296  if (!scan_int(value, &cellhd->rows3))
297  ERROR(buf, line);
298  if (cellhd->rows3 <= 0)
299  ERROR(buf, line);
300  SET(F_ROWS3);
301  continue;
302  }
303  if (strncmp(label, "cols", 4) == 0 && strlen(label) == 4) {
304  if (TEST(F_COLS))
305  ERROR(_("duplicate cols field"), line);
306  if (!scan_int(value, &cellhd->cols))
307  ERROR(buf, line);
308  if (cellhd->cols <= 0)
309  ERROR(buf, line);
310  SET(F_COLS);
311  continue;
312  }
313  if (strncmp(label, "cols3", 5) == 0) {
314  if (TEST(F_COLS3))
315  ERROR(_("duplicate 3D cols field"), line);
316  if (!scan_int(value, &cellhd->cols3))
317  ERROR(buf, line);
318  if (cellhd->cols3 <= 0)
319  ERROR(buf, line);
320  SET(F_COLS3);
321  continue;
322  }
323  if (strncmp(label, "depths", 6) == 0) {
324  if (TEST(F_DEPTHS))
325  ERROR(_("duplicate depths field"), line);
326  if (!scan_int(value, &cellhd->depths))
327  ERROR(buf, line);
328  if (cellhd->depths <= 0)
329  ERROR(buf, line);
330  SET(F_DEPTHS);
331  continue;
332  }
333  if (strncmp(label, "form", 4) == 0) {
334  if (TEST(F_FORMAT))
335  ERROR(_("duplicate format field"), line);
336  if (!scan_int(value, &cellhd->format))
337  ERROR(buf, line);
338  SET(F_FORMAT);
339  continue;
340  }
341  if (strncmp(label, "comp", 4) == 0) {
342  if (TEST(F_COMP))
343  ERROR(_("duplicate compressed field"), line);
344  if (!scan_int(value, &cellhd->compressed))
345  ERROR(buf, line);
346  SET(F_COMP);
347  continue;
348  }
349  ERROR(buf, line);
350  }
351 
352  /* check some of the fields */
353  if (!TEST(F_NORTH))
354  ERROR(_("north field missing"), 0);
355  if (!TEST(F_SOUTH))
356  ERROR(_("south field missing"), 0);
357  if (!TEST(F_WEST))
358  ERROR(_("west field missing"), 0);
359  if (!TEST(F_EAST))
360  ERROR(_("east field missing"), 0);
361  if (!TEST(F_EWRES) && !TEST(F_COLS))
362  ERROR(_("cols field missing"), 0);
363  if (!TEST(F_NSRES) && !TEST(F_ROWS))
364  ERROR(_("rows field missing"), 0);
365  /* This next stmt is commented out to allow wr_cellhd.c to write
366  * headers that will be readable by GRASS 3.1
367  if ((TEST(F_ROWS) && TEST(F_NSRES))
368  || (TEST(F_COLS) && TEST(F_EWRES)))
369  ERROR ("row/col and resolution information can not both appear ",0);
370  */
371 
372  /* 3D defined? */
373  if (TEST(F_EWRES3) || TEST(F_NSRES3) || TEST(F_COLS3) || TEST(F_ROWS3)) {
374  if (!TEST(F_EWRES3))
375  ERROR(_("ewres3 field missing"), 0);
376  if (!TEST(F_NSRES3))
377  ERROR(_("nsres3 field missing"), 0);
378  if (!TEST(F_COLS3))
379  ERROR(_("cols3 field missing"), 0);
380  if (!TEST(F_ROWS3))
381  ERROR(_("rows3 field missing"), 0);
382  }
383  else { /* use 2D */
384  cellhd->ew_res3 = cellhd->ew_res;
385  cellhd->ns_res3 = cellhd->ns_res;
386  cellhd->cols3 = cellhd->cols;
387  cellhd->rows3 = cellhd->rows;
388  }
389 
390  /* Adjust and complete the cell header */
391  if ((err = G_adjust_Cell_head(cellhd, TEST(F_ROWS), TEST(F_COLS))))
392  ERROR(err, 0);
393 
394 
395  return NULL;
396 }
397 
398 static int scan_item(const char *buf, char *label, char *value)
399 {
400  /* skip blank lines */
401  if (sscanf(buf, "%1s", label) != 1)
402  return 0;
403 
404  /* skip comment lines */
405  if (*label == '#')
406  return 0;
407 
408  /* must be label: value */
409  if (sscanf(buf, "%[^:]:%[^\n]", label, value) != 2)
410  return -1;
411 
412  G_strip(label);
413  G_strip(value);
414  return 1;
415 }
416 
417 static int scan_int(const char *buf, int *n)
418 {
419  char dummy[3];
420 
421  *dummy = 0;
422  return (sscanf(buf, "%d%1s", n, dummy) == 1 && *dummy == 0);
423 }
424 
425 static double scan_double(const char *buf, double *n)
426 {
427  char dummy[3];
428 
429  *dummy = 0;
430  return (sscanf(buf, "%lf%1s", n, dummy) == 1 && *dummy == 0);
431 }
432 
433 static char *error(const char *msg, int line)
434 {
435  char buf[1024];
436 
437  if (line)
438  sprintf(buf, _("line %d: <%s>"), line, msg);
439  else
440  sprintf(buf, "<%s>", msg);
441 
442  return G_store(buf);
443 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
#define F_SOUTH
Definition: rd_cellhd.c:28
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
#define F_ROWS
Definition: rd_cellhd.c:36
char * G__read_Cell_head_array(char **array, struct Cell_head *cellhd, int is_cellhd)
Definition: rd_cellhd.c:89
tuple label
gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY, label = _(&quot;width&quot;)), pos = (1, 1), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
Definition: tools.py:1366
FILE * fd
Definition: g3dcolor.c:368
#define F_COLS3
Definition: rd_cellhd.c:40
#define F_EWRES3
Definition: rd_cellhd.c:38
#define F_PROJ
Definition: rd_cellhd.c:25
#define F_ROWS3
Definition: rd_cellhd.c:41
#define F_COMP
Definition: rd_cellhd.c:34
int count
const char * err
Definition: g3dcolor.c:50
#define F_NORTH
Definition: rd_cellhd.c:27
#define F_BOTTOM
Definition: rd_cellhd.c:43
#define F_EAST
Definition: rd_cellhd.c:29
int G_scan_resolution(const char *buf, double *res, int projection)
ASCII resolution to double.
Definition: wind_scan.c:97
#define F_FORMAT
Definition: rd_cellhd.c:33
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
char * G_adjust_Cell_head(struct Cell_head *cellhd, int row_flag, int col_flag)
Adjust cell header.
Definition: adj_cellhd.c:43
char * value
Definition: env.c:30
#define F_NSRES
Definition: rd_cellhd.c:32
#define ERROR(x, line)
Definition: rd_cellhd.c:19
#define F_WEST
Definition: rd_cellhd.c:30
#define F_ZONE
Definition: rd_cellhd.c:26
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
#define F_TOP
Definition: rd_cellhd.c:42
int G_scan_northing(const char *buf, double *northing, int projection)
ASCII northing to double.
Definition: wind_scan.c:37
int G_getl(char *buf, int n, FILE *fd)
gets a line of text from a file
Definition: getl.c:17
return NULL
Definition: dbfopen.c:1394
#define SET
Definition: y.tab.c:168
tuple msg
Definition: wxnviz.py:32
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
char * G__read_Cell_head(FILE *fd, struct Cell_head *cellhd, int is_cellhd)
Definition: rd_cellhd.c:53
#define TEST(x)
Definition: rd_cellhd.c:48
#define F_NSRES3
Definition: rd_cellhd.c:39
#define F_COLS
Definition: rd_cellhd.c:35
int n
Definition: dataquad.c:291
int G_scan_easting(const char *buf, double *easting, int projection)
ASCII easting to double.
Definition: wind_scan.c:65
#define F_TBRES
Definition: rd_cellhd.c:44
#define F_EWRES
Definition: rd_cellhd.c:31
#define F_DEPTHS
Definition: rd_cellhd.c:45