File: edit_cellhd.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (513 lines) | stat: -rw-r--r-- 14,776 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#define AS_CELLHD 1
#define AS_WINDOW 0
#define AS_DEF_WINDOW -1
/* modified 26nov to use word region instead of window
 * as far as the USER is concerned.
 */
/*
 **********************************************************************
 *
 *   E_edit_cellhd (cellhd, type)
 *      struct Cell_head *cellhd   (cellhd to be defined)
 *      int type 
 *        0 = region - user input resolutions
 *       -1 = default region - user input resolutions
 *        1 = cellhd - rows and cols must be set
 *
 *   Screen oriented user interactive session for modifying a cell header
 *   or region.
 *   Uses the visual_ask V_ask routines.  As such, programs including
 *   this must load the GRASS library $(VASKLIB) and include $(CURSES) in
 *   in the compile line
 *
 *   returns:
 *      -1 error of some sort, or user cancels the edit
 *       0 ok
 *
 **********************************************************************/
#include <string.h>
#include <stdlib.h>
#include "vask.h"
#include "gis.h"
#include "edit.h"

#if 0
/* never used...? */
static int max(int,int);
#endif
static int hitreturn(void);

static char *cellhd_screen[] = {
"                           IDENTIFY CELL HEADER",
"",
"           ============================= DEFAULT REGION ========",
"           |          Default North:                           |",
"           |                                                   |",
"           |           =======  CELL HEADER  =======           |",
"           |           | NORTH EDGE:               |           |",
"           |           |                           |           |",
" Def. West |WEST EDGE  |                           |EAST EDGE  | Def. East",
"           |           |                           |           |",
"           |           | SOUTH EDGE:               |           |",
"           |           =============================           |",
"           |                                                   |",
"           |          Default South:                           |",
"           =====================================================",
"           PROJECTION:                                ZONE:",
NULL};

static char *window_screen[] = {
"                              IDENTIFY REGION",
"",
"           ============================= DEFAULT REGION ========",
"           |          Default North:                           |",
"           |                                                   |",
"           |           =======  YOUR REGION  =======           |",
"           |           | NORTH EDGE:               |           |",
"           |           |                           |           |",
" Def. West |WEST EDGE  |                           |EAST EDGE  | Def. East",
"           |           |                           |           |",
"           |           | SOUTH EDGE:               |           |",
"           |           =============================           |",
"           |                                                   |",
"           |          Default South:                           |",
"           =====================================================",
"           PROJECTION:                                ZONE:",
"",
"                   Default   GRID RESOLUTION   Region",
"                            --- East-West ---",
"                            -- North-South --",
NULL};

static char *def_window_screen[] = {
"                         DEFINE THE DEFAULT REGION",
"",
"",
"",
"",
"                       ====== DEFAULT REGION =======",
"                       | NORTH EDGE:               |",
"                       |                           |",
"            WEST EDGE  |                           |EAST EDGE",
"                       |                           |",
"                       | SOUTH EDGE:               |",
"                       =============================",
"",
"",
"",
"           PROJECTION:                                ZONE:",
"",
"                             GRID RESOLUTION",
"                                 East-West:",
"                               North-South:",
NULL};

static void format_value(int (*func)(double, char *, int), 
                         double x, char *buf, int projection)
{
    if (projection != PROJECTION_XY)
    {
	int k = (projection == PROJECTION_LL) ? 3600 : 1;
	int i = (int) (x * k + (x < 0 ? -0.5 : 0.5));
	x = (double) i / k;
    }
    func (x, buf, projection);
    buf[10] = '\0';
}

static void format_northing(double north, char *buf, int projection)
{
	format_value(G_format_northing, north, buf, projection);
}

static void format_easting(double east, char *buf, int projection)
{
	format_value(G_format_easting, east, buf, projection);
}

static void format_resolution(double res, char *buf, int projection)
{
	format_value(G_format_resolution, res, buf, projection);
}

int E_edit_cellhd (struct Cell_head *cellhd , int type)
{
    char ll_north[20];
    char ll_south[20];
    char ll_east[20];
    char ll_west[20];
    char ll_nsres[20];
    char ll_ewres[20];
    char ll_def_north[20];
    char ll_def_south[20];
    char ll_def_east[20];
    char ll_def_west[20];
    char ll_def_ewres[20];
    char ll_def_nsres[20];
    char projection[80];
    char **screen;

    struct Cell_head def_wind ;
    double north, south, east, west ;
    double nsres, ewres;
    char buf[64], buf2[30], *p;
    short ok ;
    int line;
    char *prj;
    char *err;

    if (type == AS_CELLHD && (cellhd->rows <= 0 || cellhd->cols <= 0))
    {
	fprintf (stderr, "E_edit_cellhd() - programmer error\n");
	fprintf (stderr, "  ** rows and cols must be positive **\n");
	return -1;
    }
    if (type != AS_DEF_WINDOW)
    {
	if (G_get_default_window(&def_wind) != 1)
	    return -1 ;


	if(cellhd->proj < 0)
	{
	    cellhd->proj        = def_wind.proj ;
	    cellhd->zone        = def_wind.zone ;
	}
	else if(cellhd->zone < 0)
	    cellhd->zone        = def_wind.zone ;
    }

    prj = G__projection_name (cellhd->proj);
    if (!prj) prj = "** unknown **";
    sprintf (projection, "%d (%s)", cellhd->proj, prj) ;

    if (type !=AS_DEF_WINDOW)
    {
	if (cellhd->west >= cellhd->east || cellhd->south >= cellhd->north) 
	{
	    cellhd->north       = def_wind.north     ; 
	    cellhd->south       = def_wind.south     ;
	    cellhd->west        = def_wind.west      ; 
	    cellhd->east        = def_wind.east      ;
	    if (type != AS_CELLHD)
	    {
	    cellhd->ew_res      = def_wind.ew_res    ;
	    cellhd->ns_res      = def_wind.ns_res    ;
	    cellhd->rows        = def_wind.rows      ;
	    cellhd->cols        = def_wind.cols      ;
	    }
	}

	if (cellhd->proj != def_wind.proj)
	{
	    fprintf (stderr, "%s projection %d differs from default projection %d\n",
		type == AS_CELLHD?"header":"region", cellhd->proj, def_wind.proj);
	    if (!G_yes("do you want to make them match? ", 1))
		return -1;
	    cellhd->proj = def_wind.proj;
	    cellhd->zone = def_wind.zone;
	}
	if (cellhd->zone != def_wind.zone)
	{
	    fprintf (stderr, "%s zone %d differs from default zone %d\n",
		type == AS_CELLHD?"header":"region", cellhd->zone, def_wind.zone);
	    if (!G_yes("do you want to make them match? ", 1))
		    return -1;
	    cellhd->zone = def_wind.zone;
	}

	*ll_def_north = 0;
	*ll_def_south = 0;
	*ll_def_east = 0;
	*ll_def_west = 0;
	*ll_def_ewres = 0;
	*ll_def_nsres = 0;
	format_northing (def_wind.north, ll_def_north, def_wind.proj);
	format_northing (def_wind.south, ll_def_south, def_wind.proj);
	format_easting  (def_wind.east,  ll_def_east,  def_wind.proj);
	format_easting  (def_wind.west,  ll_def_west, def_wind.proj);
	format_resolution (def_wind.ew_res, ll_def_ewres, def_wind.proj);
	format_resolution (def_wind.ns_res, ll_def_nsres, def_wind.proj);
    }

    *ll_north = 0;
    *ll_south = 0;
    *ll_east = 0;
    *ll_west = 0;
    *ll_ewres = 0;
    *ll_nsres = 0;
    format_northing (cellhd->north, ll_north, cellhd->proj);
    format_northing (cellhd->south, ll_south, cellhd->proj);
    format_easting  (cellhd->east, ll_east, cellhd->proj);
    format_easting  (cellhd->west, ll_west, cellhd->proj);
    format_resolution (cellhd->ew_res, ll_ewres, cellhd->proj);
    format_resolution (cellhd->ns_res, ll_nsres, cellhd->proj);


while(1)
{
    ok = 1;

/* List window options on the screen for the user to answer */
    switch(type)
    {
    case AS_CELLHD: screen = cellhd_screen; break;
    case AS_DEF_WINDOW: screen = def_window_screen; break;
    default: screen = window_screen; break;
    }

    V_clear() ;
    line = 0;
    while (*screen)
	V_line (line++, *screen++);

/* V_ques ( variable, type, row, col, length) ; */
    V_ques (ll_north, 's',  6, 36, 10);
    V_ques (ll_south, 's', 10, 36, 10) ;
    V_ques (ll_west,  's',  9, 12, 10) ;
    V_ques (ll_east,  's',  9, 52, 10) ;
    if (type != AS_CELLHD)
    {
	V_ques (ll_ewres, 's', 18, 48, 10) ;
	V_ques (ll_nsres, 's', 19, 48, 10) ;
    }

    if (type != AS_DEF_WINDOW)
    {
	V_const (ll_def_north, 's',  3, 36, 10) ;
	V_const (ll_def_south, 's', 13, 36, 10) ;
	V_const (ll_def_west,  's',  9,  1, 10) ;
	V_const (ll_def_east,  's',  9, 65, 10) ;
	if (type != AS_CELLHD)
	{
	    V_const (ll_def_ewres, 's', 18, 21, 10) ;
	    V_const (ll_def_nsres, 's', 19, 21, 10) ;
	}
    }

    V_const (projection,   's', 15, 23, strlen(projection));
    V_const (&cellhd->zone,'i', 15,60,3);


    V_intrpt_ok();
    if(!V_call())
	return -1;

    G_squeeze (ll_north);
    G_squeeze (ll_south);
    G_squeeze (ll_east);
    G_squeeze (ll_west);
    if (type != AS_CELLHD)
    {
	G_squeeze (ll_ewres);
	G_squeeze (ll_nsres);
    }

    if (!G_scan_northing (ll_north, &cellhd->north, cellhd->proj))
    {
        fprintf (stderr, "Illegal value for north: %s\n",ll_north);
	ok = 0;
    }
    if (!G_scan_northing (ll_south, &cellhd->south, cellhd->proj))
    {
        fprintf (stderr, "Illegal value for south: %s\n", ll_south);
	ok = 0;
    }
    if (!G_scan_easting (ll_east, &cellhd->east, cellhd->proj))
    {
        fprintf (stderr, "Illegal value for east: %s\n", ll_east);
	ok = 0;
    }
    if (!G_scan_easting (ll_west, &cellhd->west, cellhd->proj))
    {
        fprintf (stderr, "Illegal value for west: %s\n", ll_west);
	ok = 0;
    }
    if (type != AS_CELLHD)
    {
	if (!G_scan_resolution (ll_ewres, &cellhd->ew_res, cellhd->proj))
	{
	    fprintf (stderr, "Illegal east-west resolution: %s\n", ll_ewres);
	    ok = 0;
	}
	if (!G_scan_resolution (ll_nsres, &cellhd->ns_res, cellhd->proj))
	{
	    fprintf (stderr, "Illegal north-south resolution: %s\n", ll_nsres);
	    ok = 0;
	}
    }
    if (!ok)
    {
	hitreturn();
	continue;
    }

/* Adjust and complete the cell header */

    north = cellhd->north;
    south = cellhd->south;
    east  = cellhd->east ;
    west  = cellhd->west ;
    nsres = cellhd->ns_res;
    ewres = cellhd->ew_res;

    if ((err = G_adjust_Cell_head(cellhd,type==AS_CELLHD,type==AS_CELLHD)))
    {
	fprintf (stderr, "%s\n", err);
	hitreturn();
	continue;
    }
    if (type==AS_CELLHD)
    {
	nsres = cellhd->ns_res;
	ewres = cellhd->ew_res;
    }

SHOW:
    fprintf (stderr, "\n\n") ;
    fprintf (stderr, "  projection:   %s\n", projection);
    fprintf (stderr, "  zone:         %d\n", cellhd->zone);

    G_format_northing (cellhd->north, buf,  cellhd->proj);
    G_format_northing (north,         buf2, cellhd->proj);
    fprintf (stderr, "  north:       %s", buf);
    if (strcmp (buf, buf2) != 0)
    {
	ok = 0;
        fprintf (stderr, "  (Changed to match resolution)");
    }
    fprintf (stderr, "\n");

    G_format_northing (cellhd->south, buf,  cellhd->proj);
    G_format_northing (south,         buf2, cellhd->proj);
    fprintf (stderr, "  south:       %s", buf);
    if (strcmp (buf, buf2) != 0)
    {
	ok = 0;
        fprintf (stderr, "  (Changed to match resolution)");
    }
    fprintf (stderr, "\n");

    G_format_easting (cellhd->east, buf,  cellhd->proj);
    G_format_easting (east,         buf2, cellhd->proj);
    fprintf (stderr, "  east:        %s", buf);
    if (strcmp (buf, buf2) != 0)
    {
	ok = 0;
        fprintf (stderr, "  (Changed to match resolution)");
    }
    fprintf (stderr, "\n");

    G_format_easting (cellhd->west, buf,  cellhd->proj);
    G_format_easting (west,         buf2, cellhd->proj);
    fprintf (stderr, "  west:        %s", buf);
    if (strcmp (buf, buf2) != 0)
    {
	ok = 0;
        fprintf (stderr, "  (Changed to match resolution)");
    }
    fprintf (stderr, "\n");

    fprintf (stderr, "\n");
    G_format_resolution (cellhd->ew_res, buf,  cellhd->proj);
    G_format_resolution (ewres,          buf2, cellhd->proj);
    fprintf (stderr, "  e-w res:     %s", buf);
    if (strcmp (buf, buf2) != 0)
    {
	ok = 0;
        fprintf (stderr, "  (Changed to conform to grid)");
    }
    fprintf (stderr, "\n");

    G_format_resolution (cellhd->ns_res, buf,  cellhd->proj);
    G_format_resolution (nsres,          buf2, cellhd->proj);
    fprintf (stderr, "  n-s res:     %s", buf);
    if (strcmp (buf, buf2) != 0)
    {
	ok = 0;
        fprintf (stderr, "  (Changed to conform to grid)");
    }
    fprintf (stderr, "\n");

    fprintf (stderr, "\n");
    fprintf (stderr, "  total rows:  %15d\n",    cellhd->rows);
    fprintf (stderr, "  total cols:  %15d\n",    cellhd->cols);
    sprintf(buf,"%lf",    (double) cellhd->rows * cellhd->cols);
    *(p = strchr(buf, '.')) = 0;
    G_insert_commas(buf);
    fprintf (stderr, "  total cells: %15s\n",    buf);
    fprintf (stderr, "\n");

    if (type != AS_DEF_WINDOW)
    {
	if (cellhd->north > def_wind.north)
	{
	    fprintf (stderr, "warning - north falls outside the default region\n");
	    ok = 0;
	}
	if (cellhd->south < def_wind.south)
	{
	    fprintf (stderr, "warning - south falls outside the default region\n");
	    ok = 0;
	}
	if (cellhd->proj != PROJECTION_LL)
	{
	    if (cellhd->east > def_wind.east)
	    {
		fprintf (stderr, "warning - east falls outside the default region\n");
		ok = 0;
	    }
	    if (cellhd->west < def_wind.west)
	    {
		fprintf (stderr, "warning - west falls outside the default region\n");
		ok = 0;
	    }
	}
    }
ASK:
    fflush(stdin);
    fprintf (stderr, "\nDo you accept this %s? (y/n) [%s] > ",
	type==AS_CELLHD?"header":"region", ok?"y":"n") ;
    if(!G_gets(buf))
	goto SHOW;
    G_strip (buf);
    switch (*buf)
    {
    case 0:
	    break;
    case 'y':
    case 'Y':
	    ok = 1 ;
	    break;
    case 'n':
    case 'N':
	    ok = 0;
	    break;
    default:
	    goto ASK;
    }
    if (ok)
	return 0;
}
}

static int hitreturn()
{
    char buf[100];

    fprintf (stderr, "hit RETURN -->");
    if (!fgets(buf,100,stdin)) exit(0);
    G_strip (buf);
    if (strcmp (buf, "exit") == 0) exit(0);

    return 0;
}

#if 0
/* not used ? */
static int max(a,b)
{
    return a>b?a:b;
}
#endif