File: reclass.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 (382 lines) | stat: -rw-r--r-- 8,083 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
#include <string.h>
#include "gis.h"
#include "glocale.h"

static char *NULL_STRING = "null";
static int reclass_type(FILE *,char *,char *);
static FILE *fopen_cellhd_old( char *, char *);
static FILE *fopen_cellhd_new(char *);
static int get_reclass_table(FILE *, struct Reclass *);


/*!
 * \brief reclass file?
 *
 * This function determines if the raster file
 * <b>name</b> in <b>mapset</b> is a reclass file.
 * If it is, then the name and mapset of the referenced 
 * raster file are copied into the <b>r_name</b> and <b>r_mapset</b> 
 * buffers.
 * Returns 1 if <b>name</b> is a reclass file, 0 if it is not, and -1 if 
 * there was a problem reading the raster header for <b>name.</b>
 *
 *  \param name
 *  \param mapset
 *  \param r_name
 *  \param r_mapset
 *  \return int
 */

int G_is_reclass (char *name, char *mapset, char *rname, char *rmapset)
{
    FILE *fd;
    int type;

    fd = fopen_cellhd_old (name, mapset);
    if (fd == NULL)
	return -1;
    
    type = reclass_type (fd, rname, rmapset);
    fclose (fd);
    if (type < 0)
	return -1;
    else
	return type != 0;
}


/*!
 * \brief get child reclass maps list
 *
 * This function generates a
 * child reclass maps list from the cell_misc/reclassed_to file which stores 
 * this list. The cell_misc/reclassed_to file is written by 
 * G_put_reclass().
 * G_is_reclassed_to() is used by g.rename, g.remove and r.reclass to
 * prevent accidentally deleting the parent map of a reclassed raster map.
 *
 *  \param name
 *  \param mapset
 *  \param nrmaps
 *  \param rmaps
 *  \return int
 */

int G_is_reclassed_to (char *name, char *mapset, int *nrmaps, char ***rmaps)
{
    FILE *fd;
    int i, j, k, l;
    char buf1[256], buf2[256], buf3[256], *p;

    strcpy(buf2, name);
    if ((p = strchr(buf2, '@')))
        *p = 0;

    sprintf (buf1, "%s/%s/cell_misc/%s/reclassed_to",
    		G__location_path(), mapset, buf2);

    fd = fopen(buf1, "r");

    if (fd == NULL)
    {
        return -1;
    }

    if (rmaps)
        *rmaps = NULL;
    for (i=0; !feof(fd) && fgets(buf2, 255, fd); )
    {
	l = strlen(buf2);
	for (j=0, k=0; j<l; j++)
	{
	    if(buf2[j] == '#' ||
		((buf2[j] == ' ' || buf2[j] == '\t' || buf2[j] == '\n') && k))
	        break;
	    else
	    if(buf2[j] != ' ' && buf2[j] != '\t')
		buf3[k++] = buf2[j];
	}

	if (k)
	{
	    buf3[k] = 0;
	    i++;
	    if (rmaps)
	    {
	        *rmaps = (char **) G_realloc(*rmaps, i*sizeof(char *));
	        (*rmaps)[i-1] = (char *) G_malloc(k+1);
	        strncpy((*rmaps)[i-1], buf3, k);
	        (*rmaps)[i-1][k] = 0;
	    }
	}
    }

    if (nrmaps)
        *nrmaps = i;

    if (i && rmaps)
    {
	i++;
	*rmaps = (char **) G_realloc(*rmaps, i*sizeof(char *));
	(*rmaps)[i-1] = NULL;
    }

    return i;
}

int G_get_reclass (char *name, char *mapset, struct Reclass *reclass)
{
    FILE *fd;
    int stat;

    fd = fopen_cellhd_old (name, mapset);
    if (fd == NULL)
	return -1;
    reclass->type = reclass_type (fd, reclass->name, reclass->mapset);
    if (reclass->type <= 0)
    {
	fclose (fd);
	return reclass->type;
    }

    switch (reclass->type)
    {
    case RECLASS_TABLE:
	stat = get_reclass_table (fd, reclass);
	break;
    default:
	stat = -1;
    }

    fclose (fd);
    if (stat < 0)
    {
	char msg[100];
	if (stat == -2)
	    sprintf(msg, _("Too many reclass categories for [%s in %s]"),
		    name, mapset);
	else
	    sprintf(msg, _("Illegal reclass format in header file for [%s in %s]"),
		    name, mapset);
	G_warning (msg);
	stat = -1;
    }
    return stat;
}

int G_free_reclass (struct Reclass *reclass)
{
    switch (reclass->type)
    {
    case RECLASS_TABLE:
	if (reclass->num > 0)
	    G_free (reclass->table);
	reclass->num = 0;
	break;
    default:
	break;
    }

    return 0;
}

static int reclass_type( FILE *fd,char *rname,char *rmapset)
{
    char buf[128];
    char label[128], arg[128];
    int i;
    int type;

/* Check to see if this is a reclass file */
    if (fgets(buf,sizeof(buf),fd) == NULL)
	return 0;
    if (strncmp(buf,"reclas",6))
	return 0;
/* later may add other types of reclass */
    type = RECLASS_TABLE;

/* Read the mapset and file name of the REAL cell file */
    *rname = *rmapset = 0;
    for (i=0; i<2; i++)
    {
	if (fgets(buf,sizeof buf,fd) == NULL)
	    return -1;
	if(sscanf(buf,"%[^:]:%s", label, arg) != 2)
	    return -1;
	if (! strncmp(label, "maps", 4))
	    strcpy(rmapset, arg) ;
	else if (! strncmp(label, "name", 4))
	    strcpy(rname, arg) ;
	else
	    return -1;
    } 
    if (*rmapset && *rname)
	return type;
    else
	return -1;
}

static FILE *fopen_cellhd_old( char *name, char *mapset)
{
    return G_fopen_old ("cellhd", name, mapset);
}

int G_put_reclass (char *name, struct Reclass *reclass)
{
    FILE *fd;
    long min, max;
    int i;
    char buf1[256], buf2[256], buf3[256], *p;

    switch (reclass->type)
    {
    case RECLASS_TABLE:
	if (reclass->min > reclass->max || reclass->num <= 0)
	{
	    G_fatal_error (_("Illegal reclass request"));
	    return -1;
	}
	break;
    default:
	G_fatal_error (_("Illegal reclass type"));
	return -1;
    }

    fd = fopen_cellhd_new (name);
    if (fd == NULL)
    {
	G_warning (_("Unable to create header file for [%s in %s]"),
		name, G_mapset());
	return -1;
    }

    fprintf (fd, "reclass\n");
    fprintf (fd, "name: %s\n", reclass->name);
    fprintf (fd, "mapset: %s\n", reclass->mapset);

/* find first non-null entry */
    for (min = 0; min < reclass->num; min++)
	if (!G_is_c_null_value(&reclass->table[min]))
	    break;
/* find last non-zero entry */
    for (max = reclass->num-1; max >= 0; max--)
	if (!G_is_c_null_value(&reclass->table[max]))
	    break;

/*
 * if the resultant table is empty, write out a dummy table
 * else write out the table
 *   first entry is #min
 *   rest are translations for cat min+i
 */
    if (min > max)
	fprintf (fd, "0\n");
    else
    {
	fprintf (fd, "#%ld\n", (long) reclass->min + min);
	while (min <= max)
	{
	    if (G_is_c_null_value(&reclass->table[min]))
	       fprintf (fd, "%s\n", NULL_STRING);
            else 
	       fprintf (fd, "%ld\n", (long) reclass->table[min]);
	    min++;
        }
    }
    fclose (fd);

    strcpy(buf2, reclass->name);
    if ((p = strchr(buf2, '@')))
        *p = 0;

    sprintf (buf1, "%s/%s/cell_misc/%s/reclassed_to",
    		G__location_path(), reclass->mapset, buf2);

    fd = fopen(buf1, "a+");
    if (fd == NULL)
    {
#if 0
        G_warning (_("Unable to create dependency file in [%s in %s]"),
    	    buf2, reclass->mapset);
#endif
        return 1;
    }

    fseek (fd, 0L, SEEK_SET);

    sprintf (buf2, "%s@%s\n", name, G_mapset());
    for (i=0; !feof(fd) && fgets(buf3, 255, fd); )
    {
        if (!(strcmp(buf2, buf3)))
        {
            i = 1;
    	break;
        }
    }

    if (!i)
    {
        fprintf (fd, "%s@%s\n", name, G_mapset());
    }

    fclose (fd);

    return 1;
}

static FILE *fopen_cellhd_new(char *name)
{
    return G_fopen_new ("cellhd", name);
}

static int get_reclass_table(FILE *fd, struct Reclass *reclass)
{
    char buf[128];
    int n;
    int first, null_str_size;
    CELL cat;
    long len;

/*
 * allocate the table, expanding as each entry is read
 * note that G_realloc() will become G_malloc() if ptr in
 * NULL
 */
    reclass->min = 0;
    reclass->table = NULL;
    null_str_size = strlen(NULL_STRING);
    n = 0;
    first = 1;
    while (fgets (buf, sizeof buf, fd))
    {
	if (first)
	{
	    first = 0;
	    if (sscanf (buf, "#%d", &cat) == 1)
	    {
		reclass->min = cat;
		continue;
	    }
	}
	if(strncmp(buf, NULL_STRING, null_str_size)==0)
	    G_set_c_null_value(&cat, 1);
        else
	{
  	    if (sscanf (buf, "%d", &cat) != 1)
	        return -1;
        }
	n++;
	len = (long) n * sizeof (CELL);
	if (len != (int)len)		/* check for int overflow */
	{
	    if (reclass->table != NULL)
		G_free (reclass->table);
	    return -2;
	}
	reclass->table = (CELL *) G_realloc ((char *) reclass->table, (int)len);
	reclass->table[n-1] = cat;
    }
    reclass->max = reclass->min + n - 1;
    reclass->num = n;
    return 1;
}