File: edit_cats.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (380 lines) | stat: -rw-r--r-- 9,866 bytes parent folder | download | duplicates (3)
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

/****************************************************************************
 *
 * MODULE:       edit library functions
 * AUTHOR(S):    Originally part of gis lib dir in CERL GRASS code
 *               Subsequent (post-CVS) contributors:
 *               Glynn Clements <glynn gclements.plus.com>,
 *               Radim Blazek <radim.blazek gmail.com>,
 *               Eric G. Miller <egm2 jps.net>,
 *               Markus Neteler <neteler itc.it>,
 *               Brad Douglas <rez touchofmadness.com>,
 *               Bernhard Reiter <bernhard intevation.de>
 * PURPOSE:      libraries for interactively editing raster support data
 * COPYRIGHT:    (C) 1996-2006 by the GRASS Development Team
 *
 *               This program is free software under the GNU General Public
 *               License (>=v2). Read the file COPYING that comes with GRASS
 *               for details.
 *
 *****************************************************************************/

/**********************************************************************
 *
 *   E_edit_cats (name, cats, option)
 *      char *name
 *      struct Categories *cats 
 *
 *   Interactively prompts the user for category names for
 *   cats->ncats categories.  Uses screen oriented prompting through
 *   the visual_ask library.  Compile with $(VASKLIB) and $(CURSES)
 *
 *   name is used for informatin on the screen only.
 *   No files are read or written
 *
 *   If option is 0, user can change number of cats
 *                1, user must initialize number of cats
 *               -1, user may not change number of cats
 *
 *
 *   Returns:
 *            -1 if user canceled the edit
 *             1 if ok
 *
 *   note:
 *      at present, this routine pretends to know nothing about the
 *      category label generation capabilities using the cats.fmt
 *      string. If it is necessary to let the user edit this
 *      a separate interface must be used
 **********************************************************************/
#include <string.h>
#include <stdlib.h>
#include <grass/gis.h>
#include <grass/vask.h>
#include <grass/edit.h>


#define NLINES 10


int E_edit_cats(const char *name, struct Categories *cats, int option)
{
    long incr;
    long atnum;
    long startcat;
    long endcat;
    long first_cat, last_cat;
    long catnum[NLINES];
    char buff[NLINES][80];
    char next[20];
    char next_line[80];
    char title[80];
    char msg1[80];
    char msg2[80];
    int line;
    CELL max_ind, max_cat, min_ind;
    DCELL max_val, min_val;

    if (G_quant_get_limits
	(&cats->q, &min_val, &max_val, &min_ind, &max_ind) < 0)
	max_cat = 0;
    else
	max_cat = (CELL) max_val;

    if (max_cat < 0)
	option = 1;

    last_cat = (long)max_cat;
    if (option >= 0) {
	V_clear();
	V_line(1, msg1);
	V_line(2, msg2);

	if (option == 0) {
	    strcpy(msg1, "The current value for the highest category");
	    sprintf(msg2, "in [%s] is shown below", name);
	    V_line(3, "If you need to change it, enter another value");
	}
	else {
	    last_cat = 0;
	    strcpy(msg1, "Please enter the highest category value");
	    sprintf(msg2, "for [%s]", name);
	}

	V_line(10, "         Highest Category:");
	V_ques(&last_cat, 'l', 10, 28, 5);
	V_line(16, next_line);

	*next_line = 0;
	while (1) {
	    V_intrpt_ok();
	    if (!V_call())
		return -1;

	    if (last_cat >= 0)
		break;

	    sprintf(next_line, "** Negative values not allowed **");
	}
    }

    max_cat = last_cat;

    first_cat = 0;
    if (cats->ncats > 0 && min_val < 0)
	first_cat = (CELL) min_val;

    *title = 0;
    if (cats->title != NULL)
	strcpy(title, cats->title);

    startcat = first_cat;
    sprintf(msg1, "[%s] ENTER NEW CATEGORY NAMES FOR THESE CATEGORIES", name);

    while (1) {
	V_clear();
	V_line(0, msg1);
	V_line(2, "TITLE: ");
	V_line(3, "CAT   NEW CATEGORY NAME");
	V_line(4, "NUM");

	V_ques(title, 's', 2, 8, 60);

	endcat =
	    startcat + NLINES <=
	    last_cat + 1 ? startcat + NLINES : last_cat + 1;

	line = 5;
	for (incr = startcat; incr < endcat; incr++) {
	    atnum = incr - startcat;
	    strcpy(buff[atnum], G_get_cat((CELL) incr, cats));
	    catnum[atnum] = incr;
	    V_const(&catnum[atnum], 'l', line, 1, 3);
	    V_ques(buff[atnum], 's', line, 5, 60);
	    line++;
	}

	line += 2;
	*next = 0;
	if (endcat > last_cat)
	    strcpy(next, "end");
	else
	    sprintf(next, "%ld", endcat);
	sprintf(next_line, "%*s%*s  (of %ld)", 41,
		"Next category ('end' to end): ", 5, "", last_cat);
	V_line(line, next_line);
	V_ques(next, 's', line, 41, 5);

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

	/* store new category name in structure */
	for (incr = startcat; incr < endcat; incr++) {
	    atnum = incr - startcat;
	    G_strip(buff[atnum]);
	    if (strcmp(buff[atnum], G_get_cat((CELL) incr, cats)) != 0)
		G_set_cat((CELL) incr, buff[atnum], cats);
	}

	if (*next == 0)
	    break;
	if (strcmp(next, "end") == 0)
	    break;
	if (sscanf(next, "%ld", &endcat) != 1)
	    continue;

	if (endcat < first_cat)
	    endcat = first_cat;

	if (endcat > last_cat) {
	    endcat = last_cat - NLINES + 1;
	    if (endcat < 0)
		endcat = 0;
	}
	startcat = endcat;
    }
    if (cats->title)
	G_free(cats->title);

    G_strip(title);
    cats->title = G_store(title);

    return (1);
}


/**********************************************************************
 *
 *   E_edit_fp_cats (name, cats)
 *      char *name
 *      struct Categories *cats 
 *
 *   Interactively prompts the user for category names for
 *   fp ranges of data.  Uses screen oriented prompting through
 *   the visual_ask library.  Compile with $(VASKLIB) and $(CURSES)
 *
 *   name is used for informatin on the screen only.
 *   No files are read or written
 *
 *   Returns:
 *            -1 if user canceled the edit
 *             1 if ok
 *
 *   note:
 *      at present, this routine pretends to know nothing about the
 *      category label generation capabilities using the cats.fmt
 *      string. If it is necessary to let the user edit this
 *      a separate interface must be used
 **********************************************************************/

#define NLINES 10

int E_edit_fp_cats(const char *name, struct Categories *cats)
{
    long incr;
    long atnum;
    long startcat;
    long endcat;
    char buff[NLINES][60];
    char next[20];
    char next_line[80];
    char title[80];
    char msg1[80];
    char msg2[80];
    int line, ncats;
    size_t lab_len;
    DCELL max_val[NLINES], min_val[NLINES];
    DCELL dmin, dmax;
    CELL tmp_cell;
    struct Categories old_cats;
    struct FPRange fp_range;

    if (G_read_fp_range(name, G_mapset(), &fp_range) < 0)
	G_fatal_error("can't read the floating point range for %s", name);

    G_get_fp_range_min_max(&fp_range, &dmin, &dmax);
    /* first save old cats */
    G_copy_raster_cats(&old_cats, cats);

    G_init_raster_cats(old_cats.title, cats);
    G_free_raster_cats(cats);

    ncats = old_cats.ncats;
    V_clear();

    if (!ncats)
	sprintf(msg1, "There are no predefined fp ranges to label");
    else
	sprintf(msg1, "There are %d predefined fp ranges to label", ncats);
    sprintf(msg2, "Enter the number of fp ranges you want to label");

    V_line(1, msg1);
    V_line(2, msg2);
    V_ques(&ncats, 'l', 2, 48, 5);
    V_line(16, next_line);
    *next_line = 0;
    V_intrpt_ok();

    if (!V_call())
	return -1;

    *title = 0;
    if (old_cats.title != NULL)
	strcpy(title, old_cats.title);

    startcat = 0;
    sprintf(msg1, "The fp data in map %s ranges from %f to %f", name, dmin,
	    dmax);
    sprintf(msg2, "[%s] ENTER NEW CATEGORY NAMES FOR THESE CATEGORIES", name);

    while (1) {
	V_clear();
	V_line(0, msg1);
	V_line(1, msg2);
	V_line(3, "TITLE: ");
	V_line(4, "FP RANGE           NEW CATEGORY NAME");

	V_ques(title, 's', 2, 8, 60);

	endcat = startcat + NLINES <= ncats ? startcat + NLINES : ncats;

	line = 6;
	for (incr = startcat; incr < endcat; incr++) {
	    atnum = incr - startcat;
	    if (incr < old_cats.ncats) {
		/* if editing existing range label */
		lab_len = strlen(old_cats.labels[incr]);
		if (lab_len > 58)
		    lab_len = 58;
		strncpy(buff[atnum], old_cats.labels[incr], lab_len);
		buff[atnum][lab_len] = 0;
		G_quant_get_ith_rule(&old_cats.q, incr, &min_val[atnum],
				     &max_val[atnum], &tmp_cell, &tmp_cell);
	    }
	    else {
		/* adding new range label */
		strcpy(buff[atnum], "");
		max_val[atnum] = min_val[atnum] = 0;
	    }

	    V_ques(&min_val[atnum], 'd', line, 1, 8);
	    V_const("-", 's', line, 9, 1);
	    V_ques(&max_val[atnum], 'd', line, 10, 8);
	    V_ques(buff[atnum], 's', line, 19, 58);
	    line++;
	}

	line += 2;
	*next = 0;
	if (endcat >= ncats)
	    strcpy(next, "end");
	else
	    sprintf(next, "%ld", endcat);
	sprintf(next_line, "%*s%*s  (of %d)", 41,
		"Next range number ('end' to end): ", 5, "", ncats);
	V_line(line, next_line);
	V_ques(next, 's', line, 41, 5);

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

	/* store new category name in structure */
	for (incr = startcat; incr < endcat; incr++) {
	    atnum = incr - startcat;
	    G_strip(buff[atnum]);

	    /* adding new range label */
	    if (!(strcmp(buff[atnum], "") == 0 &&
		  min_val[atnum] == 0. && max_val[atnum] == 0.))
		G_set_d_raster_cat(&min_val[atnum], &max_val[atnum],
				   buff[atnum], cats);
	}

	if (*next == 0)
	    break;
	if (strcmp(next, "end") == 0)
	    break;
	if (sscanf(next, "%ld", &endcat) != 1)
	    continue;

	if (endcat < 0)
	    endcat = 0;

	if (endcat > ncats) {
	    endcat = ncats - NLINES + 1;
	    if (endcat < 0)
		endcat = 0;
	}
	startcat = endcat;
    }

    G_strip(title);
    cats->title = G_store(title);
    /* since label pointers in old_cats point to the old allocated strings,
       and cats now has all the newly allocated strings, it never reuses
       old ones, we need to free them */

    return (1);
}