File: extract.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 (159 lines) | stat: -rw-r--r-- 4,594 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
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <grass/gis.h>
#include <grass/display.h>
#include <grass/colors.h>
#include <grass/vector.h>
#include <grass/glocale.h>

#define WDTH    5

#define M_START 1
#define M_ADD   2
#define M_DEL   3
#define M_END   4

int display(struct Map_info *Map, struct boxlist *List,
            const struct color_rgb *color);

int extract(struct Map_info *In, struct Map_info *Out, int type,
            const struct color_rgb *color, const struct color_rgb *hcolor)
{
    int i, button, mode, line;
    int screen_x, screen_y, cur_screen_x, cur_screen_y;
    double x1, y1, x2, y2;
    struct boxlist *List, *CList;
    struct bound_box box;
    struct line_pnts *Points;
    struct line_cats *Cats;

    Points = Vect_new_line_struct();
    Cats = Vect_new_cats_struct();

    List = Vect_new_boxlist(0);
    CList = Vect_new_boxlist(0);

    /* box.T = PORT_DOUBLE_MAX;
       box.B = -PORT_DOUBLE_MAX; */
    Vect_get_map_box(In, &box);

    mode = M_START;
    G_message(_("Select vector(s) with mouse"));
    G_message(_(" - L: draw box with left mouse button to select"));
    G_message(
        _(" - M: draw box with middle mouse button to remove from display"));
    G_message(_(" - R: quit and save selected vectors to new map\n"));
    while (1) {
        G_message(_("L: add  M: remove  R: quit and save\n"));

        if (mode == M_START) {
            R_get_location_with_pointer(&screen_x, &screen_y, &button);
            cur_screen_x = screen_x;
            cur_screen_y = screen_y;
        }
        else {
            R_get_location_with_box(cur_screen_x, cur_screen_y, &screen_x,
                                    &screen_y, &button);
            x1 = D_d_to_u_col((double)(cur_screen_x));
            y1 = D_d_to_u_row((double)(cur_screen_y));
            x2 = D_d_to_u_col((double)(screen_x));
            y2 = D_d_to_u_row((double)(screen_y));

            if (x1 < x2) {
                box.W = x1;
                box.E = x2;
            }
            else {
                box.W = x2;
                box.E = x1;
            }
            if (y1 < y2) {
                box.S = y1;
                box.N = y2;
            }
            else {
                box.S = y2;
                box.N = y1;
            }
            G_debug(1, "Box: N S E W = %f %f %f %f\n", box.N, box.S, box.E,
                    box.W);
        }

        /* TODO: check if line really intersects box, not only box intersects
         * box */
        switch (button) {
        case 1:
            if (mode == M_START) {
                mode = M_ADD;
            }
            else if (mode == M_ADD) {
                Vect_select_lines_by_box(In, &box, type, CList);
                Vect_boxlist_append_boxlist(List, CList);
                display(In, List, hcolor);
                mode = M_START;
            }
            break;
        case 2:
            if (mode == M_START) {
                mode = M_DEL;
            }
            else if (mode == M_DEL) {
                Vect_select_lines_by_box(In, &box, type, CList);
                Vect_boxlist_delete_boxlist(List, CList);
                display(In, CList, color);
                mode = M_START;
            }
            break;
        case 3:
            for (i = 0; i < List->n_values; i++) {
                line = List->value[i];
                type = Vect_read_line(In, Points, Cats, line);
                Vect_write_line(Out, type, Points, Cats);
            }
            display(In, List, color);

            return 1;
            break;
        }
    };

    Vect_destroy_boxlist(List);
    Vect_destroy_boxlist(CList);

    return 1;
}

int display(struct Map_info *Map, struct boxlist *List,
            const struct color_rgb *color)
{
    int i, j, line, type;
    struct line_pnts *Points;
    double msize;

    msize = 10 * (D_d_to_u_col(2) - D_d_to_u_col(1)); /* do it better */
    G_debug(1, "msize = %f\n", msize);

    Points = Vect_new_line_struct();
    D_RGB_color(color->r, color->g, color->b);

    for (i = 0; i < List->n_values; i++) {
        line = abs(List->id[i]);
        type = Vect_read_line(Map, Points, NULL, line);

        if (type & GV_POINTS)
            D_plot_icon(Points->x[0], Points->y[0], G_ICON_CROSS, 0.0, msize);
        else
            for (j = 0; j < Points->n_points - 1; j++) {
                D_move(Points->x[j], Points->y[j]);
                D_cont(Points->x[j + 1], Points->y[j + 1]);
            }
    }

    R_flush();

    Vect_destroy_line_struct(Points);

    return 0;
}