File: read_rules.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (163 lines) | stat: -rw-r--r-- 5,002 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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <grass/raster.h>
#include <grass/glocale.h>
#include "global.h"

#define INCR 20

int report_range(void)
{
    struct FPRange drange;
    struct Range range;
    char buff[1024], buff2[300];
    RASTER_MAP_TYPE inp_type;

    inp_type = Rast_map_type(name, "");
    if (inp_type != CELL_TYPE) {
        if (Rast_read_fp_range(name, "", &drange) <= 0)
            G_fatal_error(_("Unable to read f_range for map %s"), name);

        Rast_get_fp_range_min_max(&drange, &old_dmin, &old_dmax);
        if (Rast_is_d_null_value(&old_dmin) || Rast_is_d_null_value(&old_dmax))
            G_message(_("Data range is empty"));
        else {
            sprintf(buff, "%.10f", old_dmin);
            sprintf(buff2, "%.10f", old_dmax);
            G_trim_decimal(buff);
            G_trim_decimal(buff2);
            G_message(_("Data range of %s is %s to %s (entire map)"), name,
                      buff, buff2);
        }
    }
    if (Rast_read_range(name, "", &range) <= 0)
        G_fatal_error(_("Unable to read range for map <%s>"), name);

    Rast_get_range_min_max(&range, &old_min, &old_max);
    if (Rast_is_c_null_value(&old_min) || Rast_is_c_null_value(&old_max))
        G_message(_("Integer data range of %s is empty"), name);
    else
        G_message(_("Integer data range of %s is %d to %d"), name, (int)old_min,
                  (int)old_max);

    return 0;
}

int read_rules(FILE *fp)
{
    char buf[1024];
    DCELL oLow, oHigh, nLow, nHigh;
    int n;

    in_type = DCELL_TYPE;
    out_type = CELL_TYPE;

    rules = (char **)G_malloc(INCR * sizeof(char *));
    rule_size = INCR;

    if (isatty(fileno(fp))) {
        report_range();
        G_message(_("Enter the rule or 'help' for the format description"));
    }
    Rast_fpreclass_init(&rcl_struct);
    for (;;) {
        if (isatty(fileno(fp)))
            fprintf(stderr, "> ");

        if (!G_getl2(buf, 1024, fp))
            return nrules;

        G_debug(5, "buf = [%s], strlen(buf)=%zu", buf, strlen(buf));

        for (n = 0; buf[n]; n++)
            if (buf[n] == ',')
                buf[n] = ' ';
        G_strip(buf);
        if (*buf == 0)
            continue;
        if (*buf == '#')
            continue;
        if (strcmp(buf, "end") == 0)
            break;

        if (strcmp(buf, "help") == 0) {
            G_message(_("Enter a rule in one of these formats:"));
            G_message(" ");
            G_message(_("old_low:old_high:new_low:new_high"));
            G_message(
                _("old_low:old_high:new_val      (i.e. new_high == new_low)"));
            G_message(
                _("*:old_val:new_val             (interval [inf, old_val])"));
            G_message(
                _("old_val:*:new_val             (interval [old_val, inf])"));
            G_message(" ");
            G_message(_("When finished type \"end\"."));

            continue;
        }

        /* we read and record into quant table all values, even int as doubles
           we convert the range and domain values to the right format when we
           lookup the values in the quant table */
        switch (sscanf(buf, "%lf:%lf:%lf:%lf", &oLow, &oHigh, &nLow, &nHigh)) {
        case 3:
            update_type(&out_type, nLow);
            update_rules(buf);
            Rast_fpreclass_add_rule(&rcl_struct, oLow, oHigh, nLow, nLow);
            break;

        case 4:
            update_type(&out_type, nLow);
            update_type(&out_type, nHigh);
            update_rules(buf);
            Rast_fpreclass_add_rule(&rcl_struct, oLow, oHigh, nLow, nHigh);
            break;

        default:
            if (sscanf(buf, "%lf:*:%lf", &oLow, &nLow) == 2) {
                update_type(&out_type, nLow);
                update_rules(buf);
                Rast_fpreclass_set_pos_infinite_rule(&rcl_struct, oLow, nLow);
            }
            else if (sscanf(buf, "*:%lf:%lf", &oHigh, &nLow) == 2) {
                update_type(&out_type, nLow);
                update_rules(buf);
                Rast_fpreclass_set_neg_infinite_rule(&rcl_struct, oHigh, nLow);
            }
            else
                G_message(_("%s is not a valid rule"), buf);
            break;
        } /* switch */
    }     /* loop */
    return nrules;
}

int update_type(RASTER_MAP_TYPE *map_type, DCELL val)
{
    /* check if val is not an integer number */
    if (make_dcell)
        *map_type = DCELL_TYPE;
    else {
        if ((DCELL)((CELL)val) != val)
            *map_type = FCELL_TYPE;
    }
    return 0;
}

int update_rules(char *buf)
{
    int buf_size;

    buf_size = strlen(buf) + 1; /* +1 for null char */
    if (rule_size <= nrules) {
        while (rule_size <= nrules)
            rule_size += INCR;
        rules = (char **)G_realloc(rules, rule_size * sizeof(char *));
    }
    rules[nrules] = (char *)G_malloc(sizeof(char) * (buf_size + 1));
    strncpy(rules[nrules], buf, buf_size);
    nrules++;

    return 0;
}