2
****************************************************************************
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
9
* This program is free software under the GNU General Public
10
* License (>=v2). Read the file COPYING that comes with GRASS
13
*****************************************************************************/
15
/* updated by Andreas Lange, andreas.lange@rhein-main.de
16
* for text color support.
17
* updated 2004 my MN for FP support
21
* Raghavan Srinivasan, Agricultural Engineering, Purdue University
22
* srin@ecn.purdue.edu March 1991
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.
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>
46
int draw_number(int, int, double, int, RASTER_MAP_TYPE);
48
static double D_ew, D_ns;
50
int main(int argc, char **argv)
54
int fixed_color, grid_color;
57
int nrows, ncols, row, col;
59
struct Cell_head window;
61
struct GModule *module;
63
struct Option *map, *grid_color, *text_color, *prec;
66
struct Flag *text_color, *align;
68
RASTER_MAP_TYPE map_type, inmap_type;
70
char *tmpstr1, *tmpstr2;
72
/* Initialize the GIS calls */
75
module = G_define_module();
76
G_add_keyword(_("display"));
77
G_add_keyword(_("map annotations"));
78
G_add_keyword(_("raster"));
80
_("Overlays cell category values on a raster map "
81
"displayed in the active graphics frame.");
83
opt.map = G_define_standard_option(G_OPT_R_MAP);
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");
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");
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)");
105
flg.align = G_define_flag();
106
flg.align->key = 'a';
107
flg.align->description = _("Align grids with raster cells");
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");
114
/* Check command line */
115
if (G_parser(argc, argv))
118
map_name = opt.map->answer;
120
if (strcmp("none", opt.grid_color->answer) == 0)
123
grid_color = D_translate_color(opt.grid_color->answer);
125
if (flg.text_color->answer)
130
/* Read in the map window associated with window */
132
G_get_window(&window);
134
if (flg.align->answer) {
135
struct Cell_head wind;
137
Rast_get_cellhd(map_name, "", &wind);
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));
145
wind.rows = (wind.north - wind.south) / wind.ns_res;
146
wind.cols = (wind.east - wind.west) / wind.ew_res;
148
Rast_set_window(&wind);
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);
167
layer_fd = Rast_open_old(map_name, "");
169
/* determine the inputmap type (CELL/FCELL/DCELL) */
170
inmap_type = Rast_get_map_type(layer_fd);
171
map_type = DCELL_TYPE;
173
/* number of rows and cols in window */
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."),
188
if ((nrows > 200) || (ncols > 200)) {
189
G_fatal_error(_("Aborting (region larger then 200 rows X 200 cols is not allowed)"));
192
/* Setup driver and check important information */
196
D_setup2(0, 0, t, b, l, r);
198
D_ns = fabs(D_get_u_to_d_yconv());
199
D_ew = fabs(D_get_u_to_d_xconv());
201
/*set the number of significant digits */
202
sscanf(opt.prec->answer, "%i", &digits);
204
if (grid_color > 0) { /* ie not "none" */
206
D_use_color(grid_color);
208
/* Draw vertical grids */
209
for (col = 0; col <= ncols; col++)
210
D_line_abs(col, 0, col, nrows);
212
/* Draw horizontal grids */
213
for (row = 0; row <= nrows; row++)
214
D_line_abs(0, row, ncols, row);
217
/* allocate the cell array */
218
cell = Rast_allocate_buf(map_type);
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);
224
/* fixed text color */
225
if (fixed_color == 1)
226
D_use_color(D_translate_color(opt.text_color->answer));
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);
232
for (col = 0; col < ncols; col++) {
234
if (fixed_color == 0) {
235
Rast_get_color(&cell[col], &R, &G, &B, &colors, map_type);
236
D_RGB_color(R, G, B);
239
draw_number(row, col, cell[col], digits, inmap_type);
243
Rast_close(layer_fd);
245
D_save_command(G_recreate_command());
251
/* --- end of main --- */
254
int draw_number(int row, int col, double number, int prec, RASTER_MAP_TYPE map_type)
257
double text_size, rite;
258
double tt, tb, tl, tr;
260
double dots_per_line, factor = 0.8;
261
DCELL dcell = number;
262
CELL cell = (int)number;
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);
273
if (!Rast_is_d_null_value(&dcell))
274
sprintf(no, "%.*f", prec, number);
280
dots_per_line = factor * D_ns;
281
text_size = factor * dots_per_line;
282
rite = text_size * len;
284
while (rite > D_ew) {
285
factor = factor - 0.01;
286
text_size = factor * dots_per_line;
287
rite = text_size * len;
290
D_text_size(text_size, text_size);
292
D_pos_abs(col, row + 0.7);
293
D_get_text_box(no, &tt, &tb, &tl, &tr);
295
dx = (tr + tl) / 2 - (col + 0.5);
296
D_pos_abs(col - dx, row + 0.7);