main.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. char *tmpstr1, *tmpstr2;
  65. /* Initialize the GIS calls */
  66. G_gisinit(argv[0]);
  67. module = G_define_module();
  68. G_add_keyword(_("display"));
  69. G_add_keyword(_("map annotations"));
  70. G_add_keyword(_("raster"));
  71. module->description =
  72. _("Overlays cell category values on a raster map "
  73. "displayed in the active graphics frame.");
  74. opt.map = G_define_standard_option(G_OPT_R_MAP);
  75. opt.text_color = G_define_standard_option(G_OPT_C);
  76. opt.text_color->key = "text_color";
  77. opt.text_color->label = _("Text color");
  78. opt.text_color->guisection = _("Colors");
  79. opt.grid_color = G_define_standard_option(G_OPT_CN);
  80. opt.grid_color->key = "grid_color";
  81. opt.grid_color->answer = "gray";
  82. opt.grid_color->label = _("Grid color");
  83. opt.grid_color->guisection = _("Colors");
  84. opt.prec = G_define_option();
  85. opt.prec->key = "precision";
  86. opt.prec->type = TYPE_INTEGER;
  87. opt.prec->required = NO;
  88. opt.prec->answer = "1";
  89. opt.prec->options = "0,1,2,3,4,5,6,7,8,9";
  90. opt.prec->description =
  91. _("Number of significant digits (floating point only)");
  92. flg.align = G_define_flag();
  93. flg.align->key = 'a';
  94. flg.align->description = _("Align grids with raster cells");
  95. flg.text_color = G_define_flag();
  96. flg.text_color->key = 'f';
  97. flg.text_color->description = _("Get text color from cell color value");
  98. flg.text_color->guisection = _("Colors");
  99. /* Check command line */
  100. if (G_parser(argc, argv))
  101. exit(EXIT_FAILURE);
  102. map_name = opt.map->answer;
  103. if (strcmp("none", opt.grid_color->answer) == 0)
  104. grid_color = -1;
  105. else
  106. grid_color = D_translate_color(opt.grid_color->answer);
  107. if (flg.text_color->answer)
  108. fixed_color = 0;
  109. else
  110. fixed_color = 1;
  111. /* Read in the map window associated with window */
  112. G_get_window(&window);
  113. if (flg.align->answer) {
  114. struct Cell_head wind;
  115. Rast_get_cellhd(map_name, "", &wind);
  116. /* expand window extent by one wind resolution */
  117. wind.west += wind.ew_res * ((int)((window.west - wind.west) / wind.ew_res) - (window.west < wind.west));
  118. wind.east += wind.ew_res * ((int)((window.east - wind.east) / wind.ew_res) + (window.east > wind.east));
  119. wind.south += wind.ns_res * ((int)((window.south - wind.south) / wind.ns_res) - (window.south < wind.south));
  120. wind.north += wind.ns_res * ((int)((window.north - wind.north) / wind.ns_res) + (window.north > wind.north));
  121. wind.rows = (wind.north - wind.south) / wind.ns_res;
  122. wind.cols = (wind.east - wind.west) / wind.ew_res;
  123. Rast_set_window(&wind);
  124. nrows = wind.rows;
  125. ncols = wind.cols;
  126. t = (wind.north - window.north) * nrows / (wind.north - wind.south);
  127. b = t + (window.north - window.south) * nrows / (wind.north - wind.south);
  128. l = (window.west - wind.west) * ncols / (wind.east - wind.west);
  129. r = l + (window.east - window.west) * ncols / (wind.east - wind.west);
  130. } else {
  131. nrows = window.rows;
  132. ncols = window.cols;
  133. t = 0;
  134. b = nrows;
  135. l = 0;
  136. r = ncols;
  137. }
  138. layer_fd = Rast_open_old(map_name, "");
  139. /* determine the inputmap type (CELL/FCELL/DCELL) */
  140. inmap_type = Rast_get_map_type(layer_fd);
  141. map_type = DCELL_TYPE;
  142. /* number of rows and cols in window */
  143. if ((nrows > 75) || (ncols > 75)) {
  144. G_asprintf(&tmpstr1, n_("%d row", "%d rows", nrows), nrows);
  145. G_asprintf(&tmpstr2, n_("%d col", "%d cols", ncols), ncols);
  146. /* GTC %s will be replaced by strings "X rows" and "Y cols" */
  147. G_warning(_("Current region size: %s X %s\n"
  148. "Your current region setting may be too large. "
  149. "Cells displayed on your graphics window may be too "
  150. "small for cell category number to be visible."),
  151. tmpstr1, tmpstr2);
  152. G_free(tmpstr1);
  153. G_free(tmpstr2);
  154. }
  155. if ((nrows > 200) || (ncols > 200)) {
  156. G_fatal_error(_("Aborting (region larger then 200 rows X 200 cols is not allowed)"));
  157. }
  158. /* Setup driver and check important information */
  159. D_open_driver();
  160. D_setup2(0, 0, t, b, l, r);
  161. D_ns = fabs(D_get_u_to_d_yconv());
  162. D_ew = fabs(D_get_u_to_d_xconv());
  163. /*set the number of significant digits */
  164. sscanf(opt.prec->answer, "%i", &digits);
  165. if (grid_color > 0) { /* ie not "none" */
  166. /* Set grid color */
  167. D_use_color(grid_color);
  168. /* Draw vertical grids */
  169. for (col = 0; col <= ncols; col++)
  170. D_line_abs(col, 0, col, nrows);
  171. /* Draw horizontal grids */
  172. for (row = 0; row <= nrows; row++)
  173. D_line_abs(0, row, ncols, row);
  174. }
  175. /* allocate the cell array */
  176. cell = Rast_allocate_buf(map_type);
  177. /* read the color table in the color structures of the displayed map */
  178. if (Rast_read_colors(map_name, "", &colors) == -1)
  179. G_fatal_error(_("Color file for <%s> not available"), map_name);
  180. /* fixed text color */
  181. if (fixed_color == 1)
  182. D_use_color(D_translate_color(opt.text_color->answer));
  183. /* loop through cells, find value, and draw text for value */
  184. for (row = 0; row < nrows; row++) {
  185. Rast_get_row(layer_fd, cell, row, map_type);
  186. for (col = 0; col < ncols; col++) {
  187. if (fixed_color == 0) {
  188. Rast_get_color(&cell[col], &R, &G, &B, &colors, map_type);
  189. D_RGB_color(R, G, B);
  190. }
  191. draw_number(row, col, cell[col], digits, inmap_type);
  192. }
  193. }
  194. Rast_close(layer_fd);
  195. D_save_command(G_recreate_command());
  196. D_close_driver();
  197. exit(EXIT_SUCCESS);
  198. }
  199. /* --- end of main --- */
  200. int draw_number(int row, int col, double number, int prec, RASTER_MAP_TYPE map_type)
  201. {
  202. int len;
  203. double text_size, rite;
  204. double tt, tb, tl, tr;
  205. char no[32];
  206. double dots_per_line, factor = 0.8;
  207. DCELL dcell = number;
  208. CELL cell = (int)number;
  209. double dx;
  210. /* maybe ugly, but works */
  211. if (map_type == CELL_TYPE) {
  212. if (!Rast_is_c_null_value(&cell))
  213. sprintf(no, "%d", (int)number);
  214. else
  215. sprintf(no, "Null");
  216. }
  217. else {
  218. if (!Rast_is_d_null_value(&dcell))
  219. sprintf(no, "%.*f", prec, number);
  220. else
  221. sprintf(no, "Null");
  222. }
  223. len = strlen(no);
  224. dots_per_line = factor * D_ns;
  225. text_size = factor * dots_per_line;
  226. rite = text_size * len;
  227. while (rite > D_ew) {
  228. factor = factor - 0.01;
  229. text_size = factor * dots_per_line;
  230. rite = text_size * len;
  231. }
  232. D_text_size(text_size, text_size);
  233. D_pos_abs(col, row + 0.7);
  234. D_get_text_box(no, &tt, &tb, &tl, &tr);
  235. dx = (tr + tl) / 2 - (col + 0.5);
  236. D_pos_abs(col - dx, row + 0.7);
  237. D_text(no);
  238. return 0;
  239. }