main.c 8.2 KB

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