File: support.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 (107 lines) | stat: -rw-r--r-- 3,640 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
#include <grass/gis.h>
#include <grass/raster.h>

/**
 * Merges the statf[] arrays for each thread into
 * Thread 0 for use in the support computation.
 *
 * The function takes in a 2D array thread_statf[][], where
 * the rows represent each thread and each column is
 * a file that is being used to patch. The result is
 * that thread_statf[0] is an array of size [nfiles] that holds
 * the merged stats information.
 *
 * The function takes in a pointer to thread_statf[][], directly
 * modifying it
 */
void merge_threads(struct Cell_stats **thread_statf, int nprocs, int nfiles)
{
    CELL next_cell_stat;
    long merge_count;
    for (int i = 0; i < nfiles; i++) {
        for (int t = 1; t < nprocs; t++) {
            Rast_rewind_cell_stats(thread_statf[t] + i);
            while (Rast_next_cell_stat(&next_cell_stat, &merge_count,
                                       thread_statf[t] + i)) {
                if (next_cell_stat &&
                    !Rast_find_cell_stat(next_cell_stat, &merge_count,
                                         thread_statf[0] + i)) {
                    Rast_update_cell_stats(&next_cell_stat, 1,
                                           thread_statf[0] + i);
                }
            }
        }
    }
}

/*
 * creates new category and color structures from the patching
 * files category and color files
 *
 * the first patch file is used as the basis. Its cats and colr
 * are read into the final cats/colr structures.
 * Then the other patching layers cats/colr are added to the
 * final cats/colr only if these patching layers actually
 * contributed new categories to the final result
 */

int support(char **names, struct Cell_stats *statf, int nfiles,
            struct Categories *cats, int *cats_ok, struct Colors *colr,
            int *colr_ok, RASTER_MAP_TYPE out_type)
{
    int i;
    struct Categories pcats;
    struct Colors pcolr;
    CELL n;
    long count;
    int red, grn, blu;
    int do_cats, do_colr;

    *cats_ok = 1;
    *colr_ok = 1;
    if (Rast_read_cats(names[0], "", cats) < 0)
        *cats_ok = 0;
    G_suppress_warnings(1);
    if (Rast_read_colors(names[0], "", colr) < 0)
        *colr_ok = 0;
    G_suppress_warnings(0);

    if (*cats_ok == 0 && *colr_ok == 0)
        return 0;

    for (i = 1; i < nfiles; i++) {
        do_cats = *cats_ok && (Rast_read_cats(names[i], "", &pcats) >= 0);
        G_suppress_warnings(1);
        do_colr = *colr_ok && (Rast_read_colors(names[i], "", &pcolr) >= 0);
        G_suppress_warnings(0);
        if (!do_cats && !do_colr)
            continue;
        if (out_type == CELL_TYPE) {
            Rast_rewind_cell_stats(statf + i);
            while (Rast_next_cell_stat(&n, &count, statf + i))
                if (n && !Rast_find_cell_stat(n, &count, statf)) {
                    if (do_cats) {
                        Rast_update_cell_stats(&n, 1, statf);
                        Rast_set_c_cat(
                            &n, &n, Rast_get_c_cat((CELL *)&n, &pcats), cats);
                    }
                    if (do_colr) {
                        Rast_get_c_color(&n, &red, &grn, &blu, &pcolr);
                        Rast_set_c_color(n, red, grn, blu, colr);
                    }
                }
        }
        else {
            /* the color would be the color of the first map,
             * possibly not covering the range of the other maps */
            *colr_ok = 0;
        }

        if (do_cats)
            Rast_free_cats(&pcats);
        if (do_colr)
            /* otherwise this memory is used in colr pointer */
            Rast_free_colors(&pcolr);
    }
    return 1;
}