GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
edit_cellhd.c
Go to the documentation of this file.
1 
2 /****************************************************************************
3  *
4  * MODULE: edit library functions
5  * AUTHOR(S): Originally part of gis lib dir in CERL GRASS code
6  * Subsequent (post-CVS) contributors:
7  * Glynn Clements <glynn gclements.plus.com>,
8  * Radim Blazek <radim.blazek gmail.com>,
9  * Eric G. Miller <egm2 jps.net>,
10  * Markus Neteler <neteler itc.it>,
11  * Brad Douglas <rez touchofmadness.com>,
12  * Bernhard Reiter <bernhard intevation.de>
13  * PURPOSE: libraries for interactively editing raster support data
14  * COPYRIGHT: (C) 1996-2006 by the GRASS Development Team
15  *
16  * This program is free software under the GNU General Public
17  * License (>=v2). Read the file COPYING that comes with GRASS
18  * for details.
19  *
20  *****************************************************************************/
21 #define AS_CELLHD 1
22 #define AS_WINDOW 0
23 #define AS_DEF_WINDOW -1
24 
25 /* modified 26nov to use word region instead of window
26  * as far as the USER is concerned.
27  */
28 /*
29  **********************************************************************
30  *
31  * E_edit_cellhd (cellhd, type)
32  * struct Cell_head *cellhd (cellhd to be defined)
33  * int type
34  * 0 = region - user input resolutions
35  * -1 = default region - user input resolutions
36  * 1 = cellhd - rows and cols must be set
37  *
38  * Screen oriented user interactive session for modifying a cell header
39  * or region.
40  * Uses the visual_ask V_ask routines. As such, programs including
41  * this must load the GRASS library $(VASKLIB) and include $(CURSES) in
42  * in the compile line
43  *
44  * returns:
45  * -1 error of some sort, or user cancels the edit
46  * 0 ok
47  *
48  **********************************************************************/
49 
50 /* Documentation moved here from the g.region man page:
51  * (g.region no longer does interactive; lib/init/set_data.c is the
52  * only thing left using E_edit_cellhd() AFAICT. --HB 28 Jan 2008)
53  *
54  * <h2>REGION EDIT PROMPT</h2>
55  *
56  * Most of the options will require the user to edit a
57  * geographic region, be it the current geographic region or
58  * one stored in the user's named region definitions
59  * (the <kbd>windows</kbd> directory). A standard prompt is
60  * used to perform this edit. An example is shown below:
61  *
62  *
63  *
64  * <pre>
65  * ---------------------------------------------------------------
66  * | IDENTIFY REGION |
67  * | |
68  * | =========== DEFAULT REGION ========== |
69  * | | Default North: 3402025.00 | |
70  * | | | |
71  * | | ===YOUR REGION=== | |
72  * | | | NORTH EDGE | | |
73  * | | | 3402025.00_ | | |
74  * | | | | | |
75  * | Def West: |WEST EDGE | |EAST EDGE | Def.East: |
76  * | 233975.00 |233975.00_| |236025.00_| 236025.00 |
77  * | | | SOUTH EDGE | | |
78  * | | | 3399975.00_ | | |
79  * | | ================= | |
80  * | | | |
81  * | | Default South: 3399975.00 | |
82  * | ======================================= |
83  * | |
84  * | Default GRID RESOLUTION Region |
85  * | 50.00 --- East-West --- 50.00__ |
86  * | 50.00 -- North-South -- 50.00__ |
87  * | |
88  * | |
89  * | AFTER COMPLETING ALL ANSWERS, HIT &lt;ESC&gt; TO CONTINUE |
90  * ---------------------------------------------------------------
91  * </pre>
92  *
93  * The fields NORTH EDGE, SOUTH EDGE, WEST EDGE and EAST EDGE,
94  * are the boundaries of the geographic region that the user
95  * can change. The fields Default North, Default South, Def
96  * West and Def East are the boundaries of the default
97  * geographic region that are displayed for reference and
98  * <em>cannot</em> be changed. The two GRID RESOLUTION Region
99  * fields (east-west, and north-south) are the geographic
100  * region's cell resolutions that the user can change. The
101  * two GRID RESOLUTION Default fields list the resolutions of
102  * the default geographic region; these are displayed for
103  * reference and cannot be changed here by the user.
104  *
105  */
106 
107 #include <string.h>
108 #include <stdlib.h>
109 #include <grass/vask.h>
110 #include <grass/gis.h>
111 #include <grass/edit.h>
112 
113 
114 static void format_value(int (*func) (double, char *, int),
115  double x, char *buf, int projection);
116 static void format_northing(double north, char *buf, int projection);
117 static void format_easting(double east, char *buf, int projection);
118 static void format_resolution(double res, char *buf, int projection);
119 static int hitreturn(void);
120 
121 
122 static char *cellhd_screen[] = {
123  " IDENTIFY CELL HEADER",
124  "",
125  " ============================= DEFAULT REGION ========",
126  " | Default North: |",
127  " | |",
128  " | ======= CELL HEADER ======= |",
129  " | | NORTH EDGE: | |",
130  " | | | |",
131  " Def. West |WEST EDGE | |EAST EDGE | Def. East",
132  " | | | |",
133  " | | SOUTH EDGE: | |",
134  " | ============================= |",
135  " | |",
136  " | Default South: |",
137  " =====================================================",
138  " PROJECTION: ZONE:",
139  NULL
140 };
141 
142 static char *window_screen[] = {
143  " IDENTIFY REGION",
144  "",
145  " ============================= DEFAULT REGION ========",
146  " | Default North: |",
147  " | |",
148  " | ======= YOUR REGION ======= |",
149  " | | NORTH EDGE: | |",
150  " | | | |",
151  " Def. West |WEST EDGE | |EAST EDGE | Def. East",
152  " | | | |",
153  " | | SOUTH EDGE: | |",
154  " | ============================= |",
155  " | |",
156  " | Default South: |",
157  " =====================================================",
158  " PROJECTION: ZONE:",
159  "",
160  " Default GRID RESOLUTION Region",
161  " --- East-West ---",
162  " -- North-South --",
163  NULL
164 };
165 
166 static char *def_window_screen[] = {
167  " DEFINE THE DEFAULT REGION",
168  "",
169  "",
170  "",
171  "",
172  " ====== DEFAULT REGION =======",
173  " | NORTH EDGE: |",
174  " | |",
175  " WEST EDGE | |EAST EDGE",
176  " | |",
177  " | SOUTH EDGE: |",
178  " =============================",
179  "",
180  "",
181  "",
182  " PROJECTION: ZONE:",
183  "",
184  " GRID RESOLUTION",
185  " East-West:",
186  " North-South:",
187  NULL
188 };
189 
190 
191 static void format_value(int (*func) (double, char *, int),
192  double x, char *buf, int projection)
193 {
194  if (projection != PROJECTION_XY) {
195  int k = (projection == PROJECTION_LL) ? 3600 : 1;
196  int i = (int)(x * k + (x < 0 ? -0.5 : 0.5));
197 
198  x = (double)i / k;
199  }
200 
201  func(x, buf, projection);
202  buf[10] = '\0';
203 }
204 
205 
206 static void format_northing(double north, char *buf, int projection)
207 {
208  format_value(G_format_northing, north, buf, projection);
209 }
210 
211 
212 static void format_easting(double east, char *buf, int projection)
213 {
214  format_value(G_format_easting, east, buf, projection);
215 }
216 
217 
218 static void format_resolution(double res, char *buf, int projection)
219 {
220  format_value(G_format_resolution, res, buf, projection);
221 }
222 
223 
224 int E_edit_cellhd(struct Cell_head *cellhd, int type)
225 {
226  char ll_north[20];
227  char ll_south[20];
228  char ll_east[20];
229  char ll_west[20];
230  char ll_nsres[20];
231  char ll_ewres[20];
232  char ll_def_north[20];
233  char ll_def_south[20];
234  char ll_def_east[20];
235  char ll_def_west[20];
236  char ll_def_ewres[20];
237  char ll_def_nsres[20];
238  char projection[80];
239  char **screen;
240 
241  struct Cell_head def_wind;
242  double north, south, east, west;
243  double nsres, ewres;
244  char buf[64], buf2[30], *p;
245  short ok;
246  int line;
247  char *prj;
248  char *err;
249 
250  if (type == AS_CELLHD && (cellhd->rows <= 0 || cellhd->cols <= 0)) {
251  G_message("E_edit_cellhd() - programmer error");
252  G_message(" ** rows and cols must be positive **");
253  return -1;
254  }
255  if (type != AS_DEF_WINDOW) {
256  if (G_get_default_window(&def_wind) != 1)
257  return -1;
258 
259  if (cellhd->proj < 0) {
260  cellhd->proj = def_wind.proj;
261  cellhd->zone = def_wind.zone;
262  }
263  else if (cellhd->zone < 0)
264  cellhd->zone = def_wind.zone;
265  }
266 
267  prj = G__projection_name(cellhd->proj);
268  if (!prj)
269  prj = "** unknown **";
270  sprintf(projection, "%d (%s)", cellhd->proj, prj);
271 
272  if (type != AS_DEF_WINDOW) {
273  if (cellhd->west >= cellhd->east || cellhd->south >= cellhd->north) {
274  cellhd->north = def_wind.north;
275  cellhd->south = def_wind.south;
276  cellhd->west = def_wind.west;
277  cellhd->east = def_wind.east;
278 
279  if (type != AS_CELLHD) {
280  cellhd->ew_res = def_wind.ew_res;
281  cellhd->ns_res = def_wind.ns_res;
282  cellhd->rows = def_wind.rows;
283  cellhd->cols = def_wind.cols;
284  }
285  }
286 
287  if (cellhd->proj != def_wind.proj) {
288  if (type == AS_CELLHD)
289  G_message
290  ("header projection %d differs from default projection %d",
291  cellhd->proj, def_wind.proj);
292  else
293  G_message
294  ("region projection %d differs from default projection %d",
295  cellhd->proj, def_wind.proj);
296 
297  if (!G_yes("do you want to make them match? ", 1))
298  return -1;
299 
300  cellhd->proj = def_wind.proj;
301  cellhd->zone = def_wind.zone;
302  }
303 
304  if (cellhd->zone != def_wind.zone) {
305  if (type == AS_CELLHD)
306  G_message("header zone %d differs from default zone %d",
307  cellhd->zone, def_wind.zone);
308  else
309  G_message("region zone %d differs from default zone %d",
310  cellhd->zone, def_wind.zone);
311 
312  if (!G_yes("do you want to make them match? ", 1))
313  return -1;
314 
315  cellhd->zone = def_wind.zone;
316  }
317 
318  *ll_def_north = 0;
319  *ll_def_south = 0;
320  *ll_def_east = 0;
321  *ll_def_west = 0;
322  *ll_def_ewres = 0;
323  *ll_def_nsres = 0;
324  format_northing(def_wind.north, ll_def_north, def_wind.proj);
325  format_northing(def_wind.south, ll_def_south, def_wind.proj);
326  format_easting(def_wind.east, ll_def_east, def_wind.proj);
327  format_easting(def_wind.west, ll_def_west, def_wind.proj);
328  format_resolution(def_wind.ew_res, ll_def_ewres, def_wind.proj);
329  format_resolution(def_wind.ns_res, ll_def_nsres, def_wind.proj);
330  }
331 
332  *ll_north = 0;
333  *ll_south = 0;
334  *ll_east = 0;
335  *ll_west = 0;
336  *ll_ewres = 0;
337  *ll_nsres = 0;
338  format_northing(cellhd->north, ll_north, cellhd->proj);
339  format_northing(cellhd->south, ll_south, cellhd->proj);
340  format_easting(cellhd->east, ll_east, cellhd->proj);
341  format_easting(cellhd->west, ll_west, cellhd->proj);
342  format_resolution(cellhd->ew_res, ll_ewres, cellhd->proj);
343  format_resolution(cellhd->ns_res, ll_nsres, cellhd->proj);
344 
345  while (1) {
346  ok = 1;
347 
348  /* List window options on the screen for the user to answer */
349  switch (type) {
350  case AS_CELLHD:
351  screen = cellhd_screen;
352  break;
353  case AS_DEF_WINDOW:
354  screen = def_window_screen;
355  break;
356  default:
357  screen = window_screen;
358  break;
359  }
360 
361  V_clear();
362  line = 0;
363  while (*screen)
364  V_line(line++, *screen++);
365 
366  /* V_ques ( variable, type, row, col, length) ; */
367  V_ques(ll_north, 's', 6, 36, 10);
368  V_ques(ll_south, 's', 10, 36, 10);
369  V_ques(ll_west, 's', 9, 12, 10);
370  V_ques(ll_east, 's', 9, 52, 10);
371 
372  if (type != AS_CELLHD) {
373  V_ques(ll_ewres, 's', 18, 48, 10);
374  V_ques(ll_nsres, 's', 19, 48, 10);
375  }
376 
377  if (type != AS_DEF_WINDOW) {
378  V_const(ll_def_north, 's', 3, 36, 10);
379  V_const(ll_def_south, 's', 13, 36, 10);
380  V_const(ll_def_west, 's', 9, 1, 10);
381  V_const(ll_def_east, 's', 9, 65, 10);
382 
383  if (type != AS_CELLHD) {
384  V_const(ll_def_ewres, 's', 18, 21, 10);
385  V_const(ll_def_nsres, 's', 19, 21, 10);
386  }
387  }
388 
389  V_const(projection, 's', 15, 23, (int)strlen(projection));
390  V_const(&cellhd->zone, 'i', 15, 60, 3);
391 
392  V_intrpt_ok();
393  if (!V_call())
394  return -1;
395 
396  G_squeeze(ll_north);
397  G_squeeze(ll_south);
398  G_squeeze(ll_east);
399  G_squeeze(ll_west);
400 
401  if (type != AS_CELLHD) {
402  G_squeeze(ll_ewres);
403  G_squeeze(ll_nsres);
404  }
405 
406  if (!G_scan_northing(ll_north, &cellhd->north, cellhd->proj)) {
407  G_warning("Illegal value for north: %s", ll_north);
408  ok = 0;
409  }
410 
411  if (!G_scan_northing(ll_south, &cellhd->south, cellhd->proj)) {
412  G_warning("Illegal value for south: %s", ll_south);
413  ok = 0;
414  }
415 
416  if (!G_scan_easting(ll_east, &cellhd->east, cellhd->proj)) {
417  G_warning("Illegal value for east: %s", ll_east);
418  ok = 0;
419  }
420 
421  if (!G_scan_easting(ll_west, &cellhd->west, cellhd->proj)) {
422  G_warning("Illegal value for west: %s", ll_west);
423  ok = 0;
424  }
425 
426  if (type != AS_CELLHD) {
427  if (!G_scan_resolution(ll_ewres, &cellhd->ew_res, cellhd->proj)) {
428  G_warning("Illegal east-west resolution: %s", ll_ewres);
429  ok = 0;
430  }
431 
432  if (!G_scan_resolution(ll_nsres, &cellhd->ns_res, cellhd->proj)) {
433  G_warning("Illegal north-south resolution: %s", ll_nsres);
434  ok = 0;
435  }
436  }
437 
438  if (!ok) {
439  hitreturn();
440  continue;
441  }
442 
443  /* Adjust and complete the cell header */
444  north = cellhd->north;
445  south = cellhd->south;
446  east = cellhd->east;
447  west = cellhd->west;
448  nsres = cellhd->ns_res;
449  ewres = cellhd->ew_res;
450 
451  if ((err =
452  G_adjust_Cell_head(cellhd, type == AS_CELLHD,
453  type == AS_CELLHD))) {
454  G_message("%s", err);
455  hitreturn();
456  continue;
457  }
458 
459  if (type == AS_CELLHD) {
460  nsres = cellhd->ns_res;
461  ewres = cellhd->ew_res;
462  }
463 
464  SHOW:
465  fprintf(stderr, "\n\n");
466  G_message(" projection: %s", projection);
467  G_message(" zone: %d", cellhd->zone);
468 
469  G_format_northing(cellhd->north, buf, cellhd->proj);
470  G_format_northing(north, buf2, cellhd->proj);
471  fprintf(stderr, " north: %s", buf);
472 
473  if (strcmp(buf, buf2) != 0) {
474  ok = 0;
475  fprintf(stderr, " (Changed to match resolution)");
476  }
477  fprintf(stderr, "\n");
478 
479  G_format_northing(cellhd->south, buf, cellhd->proj);
480  G_format_northing(south, buf2, cellhd->proj);
481  fprintf(stderr, " south: %s", buf);
482  if (strcmp(buf, buf2) != 0) {
483  ok = 0;
484  fprintf(stderr, " (Changed to match resolution)");
485  }
486  fprintf(stderr, "\n");
487 
488  G_format_easting(cellhd->east, buf, cellhd->proj);
489  G_format_easting(east, buf2, cellhd->proj);
490  fprintf(stderr, " east: %s", buf);
491  if (strcmp(buf, buf2) != 0) {
492  ok = 0;
493  fprintf(stderr, " (Changed to match resolution)");
494  }
495  fprintf(stderr, "\n");
496 
497  G_format_easting(cellhd->west, buf, cellhd->proj);
498  G_format_easting(west, buf2, cellhd->proj);
499  fprintf(stderr, " west: %s", buf);
500  if (strcmp(buf, buf2) != 0) {
501  ok = 0;
502  fprintf(stderr, " (Changed to match resolution)");
503  }
504  fprintf(stderr, "\n\n");
505 
506  G_format_resolution(cellhd->ew_res, buf, cellhd->proj);
507  G_format_resolution(ewres, buf2, cellhd->proj);
508  fprintf(stderr, " e-w res: %s", buf);
509  if (strcmp(buf, buf2) != 0) {
510  ok = 0;
511  fprintf(stderr, " (Changed to conform to grid)");
512  }
513  fprintf(stderr, "\n");
514 
515  G_format_resolution(cellhd->ns_res, buf, cellhd->proj);
516  G_format_resolution(nsres, buf2, cellhd->proj);
517  fprintf(stderr, " n-s res: %s", buf);
518  if (strcmp(buf, buf2) != 0) {
519  ok = 0;
520  fprintf(stderr, " (Changed to conform to grid)");
521  }
522  fprintf(stderr, "\n\n");
523 
524  G_message(" total rows: %15d", cellhd->rows);
525  G_message(" total cols: %15d", cellhd->cols);
526 
527  sprintf(buf, "%lf", (double)cellhd->rows * cellhd->cols);
528  *(p = strchr(buf, '.')) = 0;
529  G_insert_commas(buf);
530  G_message(" total cells: %15s", buf);
531  fprintf(stderr, "\n");
532 
533  if (type != AS_DEF_WINDOW) {
534  if (cellhd->north > def_wind.north) {
535  G_warning("north falls outside the default region");
536  ok = 0;
537  }
538 
539  if (cellhd->south < def_wind.south) {
540  G_warning("south falls outside the default region");
541  ok = 0;
542  }
543 
544  if (cellhd->proj != PROJECTION_LL) {
545  if (cellhd->east > def_wind.east) {
546  G_warning("east falls outside the default region");
547  ok = 0;
548  }
549 
550  if (cellhd->west < def_wind.west) {
551  G_warning("west falls outside the default region");
552  ok = 0;
553  }
554  }
555  }
556 
557  ASK:
558  fflush(stdin);
559  if (type == AS_CELLHD)
560  fprintf(stderr, "\nDo you accept this header? (y/n) [%s] > ",
561  ok ? "y" : "n");
562  else
563  fprintf(stderr, "\nDo you accept this region? (y/n) [%s] > ",
564  ok ? "y" : "n");
565 
566  if (!G_gets(buf))
567  goto SHOW;
568 
569  G_strip(buf);
570  switch (*buf) {
571  case 0:
572  break;
573  case 'y':
574  case 'Y':
575  ok = 1;
576  break;
577  case 'n':
578  case 'N':
579  ok = 0;
580  break;
581  default:
582  goto ASK;
583  }
584 
585  if (ok)
586  return 0;
587  }
588 }
589 
590 
591 static int hitreturn(void)
592 {
593  char buf[100];
594 
595  fprintf(stderr, "hit RETURN -->");
596  if (!fgets(buf, 100, stdin))
597  exit(EXIT_SUCCESS);
598 
599  G_strip(buf);
600  if (!strncmp(buf, "exit", sizeof(buf)))
601  exit(EXIT_SUCCESS);
602 
603  return 0;
604 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_format_resolution(double res, char *buf, int projection)
Resolution to ASCII.
Definition: wind_format.c:86
int G_gets(char *buf)
Definition: gets.c:39
void V_clear(void)
Zero out prompt and answer arrays.
Definition: V_clear.c:44
int V_line(int linenumber, const char *text)
Definition: V_line.c:50
int G_yes(const char *question, int dflt)
Ask a yes/no question.
Definition: yes.c:39
#define AS_CELLHD
Definition: edit_cellhd.c:21
int G_get_default_window(struct Cell_head *window)
read the default region
Definition: get_window.c:115
int V_call(void)
Interact with the user.
Definition: V_call.c:149
const char * err
Definition: g3dcolor.c:50
int E_edit_cellhd(struct Cell_head *cellhd, int type)
Definition: edit_cellhd.c:224
int G_scan_resolution(const char *buf, double *res, int projection)
ASCII resolution to double.
Definition: wind_scan.c:97
void G_message(const char *msg,...)
Print a message to stderr.
Definition: lib/gis/error.c:74
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
int
Definition: g3dcolor.c:48
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int G_scan_northing(const char *buf, double *northing, int projection)
ASCII northing to double.
Definition: wind_scan.c:37
int V_const(void *src, int var_type, int row, int col, int length)
Definition: V_const.c:51
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_insert_commas(char *buf)
Inserts commas into a number string.
Definition: commas.c:38
int G_format_easting(double east, char *buf, int projection)
Easting to ASCII.
Definition: wind_format.c:61
char buf2[200]
Definition: g3dcats.c:89
char * G__projection_name(int n)
Definition: proj2.c:36
#define AS_DEF_WINDOW
Definition: edit_cellhd.c:23
char * G_squeeze(char *line)
Remove superfluous white space.
Definition: squeeze.c:45
int G_format_northing(double north, char *buf, int projection)
Northing to ASCII.
Definition: wind_format.c:36
int G_scan_easting(const char *buf, double *easting, int projection)
ASCII easting to double.
Definition: wind_scan.c:65
int V_ques(void *src, int var_type, int row, int col, int length)
Definition: V_ques.c:80
void V_intrpt_ok(void)
Allow CTRL-C.
Definition: V_call.c:455