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