~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to display/d.rast.num/main.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 ****************************************************************************
 
3
 *
 
4
 * MODULE:       d.rast.num
 
5
 * AUTHOR(S):    Raghavan Srinivasan, Agricultural Engineering, Purdue University
 
6
 * PURPOSE:      Print numbers of category for raster cells
 
7
 * COPYRIGHT:    (C) 2000, 2012 by the GRASS Development Team
 
8
 *
 
9
 *               This program is free software under the GNU General Public
 
10
 *               License (>=v2). Read the file COPYING that comes with GRASS
 
11
 *               for details.
 
12
 *
 
13
 *****************************************************************************/
 
14
 
 
15
/* updated by Andreas Lange, andreas.lange@rhein-main.de 
 
16
 * for text color support.
 
17
 * updated 2004 my MN for FP support
 
18
 */
 
19
 
 
20
/*
 
21
 *   Raghavan Srinivasan, Agricultural Engineering, Purdue University
 
22
 *   srin@ecn.purdue.edu  March 1991
 
23
 *
 
24
 *   d.rast.num
 
25
 *
 
26
 *   Usage:  d.rast.num
 
27
 * 
 
28
 *   This program used Dgrid's sources as a beginning. Purpose of Dnumber
 
29
 *   is to read the cell layer displayed on the graphics monitor and number
 
30
 *   them, if the cell value is other than 0 in an acending order.
 
31
 *   d.rast.num draws a number on the graphic display
 
32
 *   of each cell, so the cell number could be identified when using hydrologic
 
33
 *   models such AGNPS which uses the cell number for all its correspondance.
 
34
 *   
 
35
 */
 
36
 
 
37
#include <stdlib.h>
 
38
#include <string.h>
 
39
#include <math.h>
 
40
#include <grass/gis.h>
 
41
#include <grass/raster.h>
 
42
#include <grass/display.h>
 
43
#include <grass/colors.h>
 
44
#include <grass/glocale.h>
 
45
 
 
46
int draw_number(int, int, double, int, RASTER_MAP_TYPE);
 
47
 
 
48
static double D_ew, D_ns;
 
49
 
 
50
int main(int argc, char **argv)
 
51
{
 
52
    DCELL *cell;
 
53
    char *map_name;
 
54
    int fixed_color, grid_color;
 
55
    int R, G, B;
 
56
    int layer_fd;
 
57
    int nrows, ncols, row, col;
 
58
    int digits;
 
59
    struct Cell_head window;
 
60
    struct Colors colors;
 
61
    struct GModule *module;
 
62
    struct _opt {
 
63
        struct Option *map, *grid_color, *text_color, *prec;
 
64
    } opt;
 
65
    struct _flg {
 
66
        struct Flag *text_color, *align;
 
67
    } flg;
 
68
    RASTER_MAP_TYPE map_type, inmap_type;
 
69
    double t, b, l, r;
 
70
    char *tmpstr1, *tmpstr2;
 
71
 
 
72
    /* Initialize the GIS calls */
 
73
    G_gisinit(argv[0]);
 
74
 
 
75
    module = G_define_module();
 
76
    G_add_keyword(_("display"));
 
77
    G_add_keyword(_("map annotations"));
 
78
    G_add_keyword(_("raster"));
 
79
    module->description =
 
80
        _("Overlays cell category values on a raster map "
 
81
          "displayed in the active graphics frame.");
 
82
 
 
83
    opt.map = G_define_standard_option(G_OPT_R_MAP);
 
84
 
 
85
    opt.text_color = G_define_standard_option(G_OPT_C);
 
86
    opt.text_color->key = "text_color";
 
87
    opt.text_color->label = _("Text color");
 
88
    opt.text_color->guisection = _("Colors");
 
89
 
 
90
    opt.grid_color = G_define_standard_option(G_OPT_CN);
 
91
    opt.grid_color->key = "grid_color";
 
92
    opt.grid_color->answer = "gray";
 
93
    opt.grid_color->label = _("Grid color");
 
94
    opt.grid_color->guisection = _("Colors");
 
95
 
 
96
    opt.prec = G_define_option();
 
97
    opt.prec->key = "precision";
 
98
    opt.prec->type = TYPE_INTEGER;
 
99
    opt.prec->required = NO;
 
100
    opt.prec->answer = "1";
 
101
    opt.prec->options = "0,1,2,3,4,5,6,7,8,9";
 
102
    opt.prec->description =
 
103
        _("Number of significant digits (floating point only)");
 
104
 
 
105
    flg.align = G_define_flag();
 
106
    flg.align->key = 'a';
 
107
    flg.align->description = _("Align grids with raster cells");
 
108
 
 
109
    flg.text_color = G_define_flag();
 
110
    flg.text_color->key = 'f';
 
111
    flg.text_color->description = _("Get text color from cell color value");
 
112
    flg.text_color->guisection = _("Colors");
 
113
    
 
114
    /* Check command line */
 
115
    if (G_parser(argc, argv))
 
116
        exit(EXIT_FAILURE);
 
117
 
 
118
    map_name = opt.map->answer;
 
119
 
 
120
    if (strcmp("none", opt.grid_color->answer) == 0)
 
121
        grid_color = -1;
 
122
    else
 
123
        grid_color = D_translate_color(opt.grid_color->answer);
 
124
 
 
125
    if (flg.text_color->answer)
 
126
        fixed_color = 0;
 
127
    else
 
128
        fixed_color = 1;
 
129
 
 
130
    /* Read in the map window associated with window */
 
131
 
 
132
    G_get_window(&window);
 
133
 
 
134
    if (flg.align->answer) {
 
135
        struct Cell_head wind;
 
136
 
 
137
        Rast_get_cellhd(map_name, "", &wind);
 
138
 
 
139
        /* expand window extent by one wind resolution */
 
140
        wind.west += wind.ew_res * ((int)((window.west - wind.west) / wind.ew_res) - (window.west < wind.west));
 
141
        wind.east += wind.ew_res * ((int)((window.east - wind.east) / wind.ew_res) + (window.east > wind.east));
 
142
        wind.south += wind.ns_res * ((int)((window.south - wind.south) / wind.ns_res) - (window.south < wind.south));
 
143
        wind.north += wind.ns_res * ((int)((window.north - wind.north) / wind.ns_res) + (window.north > wind.north));
 
144
 
 
145
        wind.rows = (wind.north - wind.south) / wind.ns_res;
 
146
        wind.cols = (wind.east - wind.west) / wind.ew_res;
 
147
 
 
148
        Rast_set_window(&wind);
 
149
 
 
150
        nrows = wind.rows;
 
151
        ncols = wind.cols;
 
152
 
 
153
        t = (wind.north - window.north) * nrows / (wind.north - wind.south);
 
154
        b = t + (window.north - window.south) * nrows / (wind.north - wind.south);
 
155
        l = (window.west - wind.west) * ncols / (wind.east - wind.west);
 
156
        r = l + (window.east - window.west) * ncols / (wind.east - wind.west);
 
157
    } else {
 
158
        nrows = window.rows;
 
159
        ncols = window.cols;
 
160
 
 
161
        t = 0;
 
162
        b = nrows;
 
163
        l = 0;
 
164
        r = ncols;
 
165
    }
 
166
 
 
167
    layer_fd = Rast_open_old(map_name, "");
 
168
 
 
169
    /* determine the inputmap type (CELL/FCELL/DCELL) */
 
170
    inmap_type = Rast_get_map_type(layer_fd);
 
171
    map_type = DCELL_TYPE;
 
172
 
 
173
    /* number of rows and cols in window */
 
174
 
 
175
    if ((nrows > 75) || (ncols > 75)) {
 
176
        G_asprintf(&tmpstr1, _n("%d row", "%d rows", nrows), nrows);
 
177
        G_asprintf(&tmpstr2, _n("%d col", "%d cols", ncols), ncols);
 
178
        /* GTC %s will be replaced by strings "X rows" and "Y cols" */
 
179
        G_warning(_("Current region size: %s X %s\n"
 
180
                    "Your current region setting may be too large. "
 
181
                    "Cells displayed on your graphics window may be too "
 
182
                    "small for cell category number to be visible."),
 
183
                    tmpstr1, tmpstr2);
 
184
        G_free(tmpstr1);
 
185
        G_free(tmpstr2); 
 
186
          
 
187
    }
 
188
    if ((nrows > 200) || (ncols > 200)) {
 
189
        G_fatal_error(_("Aborting (region larger then 200 rows X 200 cols is not allowed)"));
 
190
    }
 
191
 
 
192
    /* Setup driver and check important information */
 
193
 
 
194
    D_open_driver();
 
195
    
 
196
    D_setup2(0, 0, t, b, l, r);
 
197
 
 
198
    D_ns = fabs(D_get_u_to_d_yconv());
 
199
    D_ew = fabs(D_get_u_to_d_xconv());
 
200
 
 
201
    /*set the number of significant digits */
 
202
    sscanf(opt.prec->answer, "%i", &digits);
 
203
 
 
204
    if (grid_color > 0) {       /* ie not "none" */
 
205
        /* Set grid color */
 
206
        D_use_color(grid_color);
 
207
 
 
208
        /* Draw vertical grids */
 
209
        for (col = 0; col <= ncols; col++)
 
210
            D_line_abs(col, 0, col, nrows);
 
211
 
 
212
        /* Draw horizontal grids */
 
213
        for (row = 0; row <= nrows; row++)
 
214
            D_line_abs(0, row, ncols, row);
 
215
    }
 
216
 
 
217
    /* allocate the cell array */
 
218
    cell = Rast_allocate_buf(map_type);
 
219
 
 
220
    /* read the color table in the color structures of the displayed map */
 
221
    if (Rast_read_colors(map_name, "", &colors) == -1)
 
222
        G_fatal_error(_("Color file for <%s> not available"), map_name);
 
223
 
 
224
    /* fixed text color */
 
225
    if (fixed_color == 1)
 
226
        D_use_color(D_translate_color(opt.text_color->answer));
 
227
 
 
228
    /* loop through cells, find value, and draw text for value */
 
229
    for (row = 0; row < nrows; row++) {
 
230
        Rast_get_row(layer_fd, cell, row, map_type);
 
231
 
 
232
        for (col = 0; col < ncols; col++) {
 
233
 
 
234
            if (fixed_color == 0) {
 
235
                Rast_get_color(&cell[col], &R, &G, &B, &colors, map_type);
 
236
                D_RGB_color(R, G, B);
 
237
            }
 
238
 
 
239
            draw_number(row, col, cell[col], digits, inmap_type);
 
240
        }
 
241
    }
 
242
 
 
243
    Rast_close(layer_fd);
 
244
 
 
245
    D_save_command(G_recreate_command());
 
246
    D_close_driver();
 
247
 
 
248
    exit(EXIT_SUCCESS);
 
249
}
 
250
 
 
251
/* --- end of main --- */
 
252
 
 
253
 
 
254
int draw_number(int row, int col, double number, int prec, RASTER_MAP_TYPE map_type)
 
255
{
 
256
    int len;
 
257
    double text_size, rite;
 
258
    double tt, tb, tl, tr;
 
259
    char no[32];
 
260
    double dots_per_line, factor = 0.8;
 
261
    DCELL dcell = number;
 
262
    CELL cell = (int)number;
 
263
    double dx;
 
264
 
 
265
    /* maybe ugly, but works */
 
266
    if (map_type == CELL_TYPE) {
 
267
        if (!Rast_is_c_null_value(&cell))
 
268
            sprintf(no, "%d", (int)number);
 
269
        else
 
270
            sprintf(no, "Null");
 
271
    }
 
272
    else {
 
273
        if (!Rast_is_d_null_value(&dcell))
 
274
            sprintf(no, "%.*f", prec, number);
 
275
        else
 
276
            sprintf(no, "Null");
 
277
    }
 
278
    len = strlen(no);
 
279
 
 
280
    dots_per_line = factor * D_ns;
 
281
    text_size = factor * dots_per_line;
 
282
    rite = text_size * len;
 
283
 
 
284
    while (rite > D_ew) {
 
285
        factor = factor - 0.01;
 
286
        text_size = factor * dots_per_line;
 
287
        rite = text_size * len;
 
288
    }
 
289
 
 
290
    D_text_size(text_size, text_size);
 
291
 
 
292
    D_pos_abs(col, row + 0.7);
 
293
    D_get_text_box(no, &tt, &tb, &tl, &tr);
 
294
 
 
295
    dx = (tr + tl) / 2 - (col + 0.5);
 
296
    D_pos_abs(col - dx, row + 0.7);
 
297
    D_text(no);
 
298
 
 
299
    return 0;
 
300
}