main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 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 Option *opt1, *opt2, *opt3, *prec;
  57. struct Flag *text_color, *align;
  58. RASTER_MAP_TYPE map_type, inmap_type;
  59. double t, b, l, r;
  60. /* Initialize the GIS calls */
  61. G_gisinit(argv[0]);
  62. module = G_define_module();
  63. G_add_keyword(_("display"));
  64. module->description =
  65. _("Overlays cell category values on a raster map layer "
  66. "displayed to the graphics monitor.");
  67. opt1 = G_define_standard_option(G_OPT_R_MAP);
  68. opt2 = G_define_option();
  69. opt2->key = "grid_color";
  70. opt2->type = TYPE_STRING;
  71. opt2->required = NO;
  72. opt2->answer = "gray";
  73. opt2->gisprompt = "old_color,color,color_none";
  74. opt2->key_desc = "color";
  75. opt2->description = _("Color for drawing grid, or \"none\"");
  76. opt3 = G_define_option();
  77. opt3->key = "text_color";
  78. opt3->type = TYPE_STRING;
  79. opt3->required = NO;
  80. opt3->answer = DEFAULT_FG_COLOR;
  81. opt3->gisprompt = "old_color,color,color";
  82. opt3->key_desc = "color";
  83. opt3->description = _("Color for drawing text");
  84. prec = G_define_option();
  85. prec->key = "dp";
  86. prec->type = TYPE_INTEGER;
  87. prec->required = NO;
  88. prec->answer = "1";
  89. prec->options = "0,1,2,3,4,5,6,7,8,9";
  90. prec->description =
  91. _("Number of significant digits (floating point only)");
  92. align = G_define_flag();
  93. align->key = 'a';
  94. align->description = _("Align grids with raster cells");
  95. text_color = G_define_flag();
  96. text_color->key = 'f';
  97. text_color->description = _("Get text color from cell color value");
  98. /* Check command line */
  99. if (G_parser(argc, argv))
  100. exit(EXIT_FAILURE);
  101. map_name = opt1->answer;
  102. if (strcmp("none", opt2->answer) == 0)
  103. grid_color = -1;
  104. else
  105. grid_color = D_translate_color(opt2->answer);
  106. if (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 (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("!!!");
  144. G_message(_("Current window size:"));
  145. G_message(_("rows: %d"), nrows);
  146. G_message(_("columns: %d"), ncols);
  147. G_message(_("\nYour current window setting may be too large."
  148. " Cells displayed on your graphics window may be too"
  149. " small for cell category number to be visible."));
  150. G_message(" ");
  151. }
  152. if ((nrows > 200) || (ncols > 200)) {
  153. G_fatal_error(_("Aborting."));
  154. }
  155. /* Setup driver and check important information */
  156. if (D_open_driver() != 0)
  157. G_fatal_error(_("No graphics device selected"));
  158. D_setup2(0, 0, t, b, l, r);
  159. D_ns = fabs(D_get_u_to_d_yconv());
  160. D_ew = fabs(D_get_u_to_d_xconv());
  161. /*set the number of significant digits */
  162. sscanf(prec->answer, "%i", &digits);
  163. if (grid_color > 0) { /* ie not "none" */
  164. /* Set grid color */
  165. D_use_color(grid_color);
  166. /* Draw vertical grids */
  167. for (col = 0; col <= ncols; col++)
  168. D_line_abs(col, 0, col, nrows);
  169. /* Draw horizontal grids */
  170. for (row = 0; row <= nrows; row++)
  171. D_line_abs(0, row, ncols, row);
  172. }
  173. /* allocate the cell array */
  174. cell = Rast_allocate_buf(map_type);
  175. /* read the color table in the color structures of the displayed map */
  176. if (Rast_read_colors(map_name, "", &colors) == -1)
  177. G_fatal_error(_("Color file for <%s> not available"), map_name);
  178. /* fixed text color */
  179. if (fixed_color == 1)
  180. D_use_color(D_translate_color(opt3->answer));
  181. /* loop through cells, find value, and draw text for value */
  182. for (row = 0; row < nrows; row++) {
  183. Rast_get_row(layer_fd, cell, row, map_type);
  184. for (col = 0; col < ncols; col++) {
  185. if (fixed_color == 0) {
  186. Rast_get_color(&cell[col], &R, &G, &B, &colors, map_type);
  187. D_RGB_color(R, G, B);
  188. }
  189. draw_number(row, col, cell[col], digits, inmap_type);
  190. }
  191. }
  192. Rast_close(layer_fd);
  193. D_close_driver();
  194. exit(EXIT_SUCCESS);
  195. }
  196. /* --- end of main --- */
  197. int draw_number(int row, int col, double number, int prec, RASTER_MAP_TYPE map_type)
  198. {
  199. int len;
  200. double text_size, rite;
  201. double tt, tb, tl, tr;
  202. char no[32];
  203. double dots_per_line, factor = 0.8;
  204. DCELL dcell = number;
  205. CELL cell = (int)number;
  206. double dx;
  207. /* maybe ugly, but works */
  208. if (map_type == CELL_TYPE) {
  209. if (!Rast_is_c_null_value(&cell))
  210. sprintf(no, "%d", (int)number);
  211. else
  212. sprintf(no, "Null");
  213. }
  214. else {
  215. if (!Rast_is_d_null_value(&dcell))
  216. sprintf(no, "%.*f", prec, number);
  217. else
  218. sprintf(no, "Null");
  219. }
  220. len = strlen(no);
  221. dots_per_line = factor * D_ns;
  222. text_size = factor * dots_per_line;
  223. rite = text_size * len;
  224. while (rite > D_ew) {
  225. factor = factor - 0.01;
  226. text_size = factor * dots_per_line;
  227. rite = text_size * len;
  228. }
  229. D_text_size(text_size, text_size);
  230. D_pos_abs(col, row + 0.7);
  231. D_get_text_box(no, &tt, &tb, &tl, &tr);
  232. dx = (tr + tl) / 2 - (col + 0.5);
  233. D_pos_abs(col - dx, row + 0.7);
  234. D_text(no);
  235. return 0;
  236. }