main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /****************************************************************************
  2. *
  3. * MODULE: d.rast
  4. * AUTHOR(S): Jim Westervelt (CERL) (original contributor)
  5. * Markus Neteler <neteler itc.it>,
  6. * Bernhard Reiter <bernhard intevation.de>,
  7. * Huidae Cho <grass4u gmail.com>,
  8. * Eric G. Miller <egm2 jps.net>,
  9. * Glynn Clements <glynn gclements.plus.com>,
  10. * Jan-Oliver Wagner <jan intevation.de>,
  11. * Radim Blazek <radim.blazek gmail.com>,
  12. * Martin Landa <landa.martin gmail.com>
  13. * PURPOSE: display raster maps in active graphics display
  14. * COPYRIGHT: (C) 1999-2006, 2011 by the GRASS Development Team
  15. *
  16. * This program is free software under the GNU General
  17. * Public License (>=v2). Read the file COPYING that
  18. * comes with GRASS for details.
  19. *
  20. *****************************************************************************/
  21. #include <stdlib.h>
  22. #include <grass/gis.h>
  23. #include <grass/raster.h>
  24. #include <grass/display.h>
  25. #include "mask.h"
  26. #include "local_proto.h"
  27. #include <grass/glocale.h>
  28. static int parse_catlist(char **, Mask *);
  29. static int parse_vallist(char **, d_Mask *);
  30. d_Mask d_mask;
  31. Mask mask;
  32. int main(int argc, char **argv)
  33. {
  34. char *name;
  35. int overlay;
  36. int invert, fp;
  37. struct GModule *module;
  38. struct Option *map;
  39. struct Option *vallist;
  40. struct Option *bg;
  41. struct Flag *flag_n;
  42. struct Flag *flag_i;
  43. /* Initialize the GIS calls */
  44. G_gisinit(argv[0]);
  45. module = G_define_module();
  46. G_add_keyword(_("display"));
  47. G_add_keyword(_("graphics"));
  48. G_add_keyword(_("raster"));
  49. module->description = _("Displays user-specified raster map in the active "
  50. "graphics frame.");
  51. /* set up command line */
  52. map = G_define_standard_option(G_OPT_R_MAP);
  53. map->description = _("Name of raster map to be displayed");
  54. vallist = G_define_option();
  55. vallist->key = "values";
  56. vallist->key_desc = "value[-value]";
  57. vallist->type = TYPE_STRING;
  58. vallist->required = NO;
  59. vallist->multiple = YES;
  60. vallist->description = _("List of categories or values to be displayed");
  61. vallist->guisection = _("Selection");
  62. bg = G_define_standard_option(G_OPT_C_BG);
  63. bg->key_desc = "color";
  64. bg->gisprompt = "old_color,color,color";
  65. bg->label = _("Background color (for null)");
  66. bg->description = _("Either a standard color name or R:G:B triplet");
  67. bg->guisection = _("Null cells");
  68. flag_n = G_define_flag();
  69. flag_n->key = 'n';
  70. flag_n->description = _("Make null cells opaque");
  71. flag_n->guisection = _("Null cells");
  72. flag_i = G_define_flag();
  73. flag_i->key = 'i';
  74. flag_i->description = _("Invert value list");
  75. flag_i->guisection = _("Selection");
  76. if (G_parser(argc, argv))
  77. exit(EXIT_FAILURE);
  78. name = map->answer;
  79. overlay = !flag_n->answer;
  80. invert = flag_i->answer;
  81. if (D_open_driver() != 0)
  82. G_fatal_error(_("No graphics device selected. "
  83. "Use d.mon to select graphics device."));
  84. fp = Rast_map_is_fp(name, "");
  85. if (vallist->answer) {
  86. if (fp)
  87. parse_vallist(vallist->answers, &d_mask);
  88. else
  89. parse_catlist(vallist->answers, &mask);
  90. }
  91. /* use DCELL even if the map is FCELL */
  92. display(name, overlay, bg->answer, fp ? DCELL_TYPE : CELL_TYPE, invert);
  93. D_save_command(G_recreate_command());
  94. D_close_driver();
  95. exit(EXIT_SUCCESS);
  96. }
  97. static int parse_catlist(char **catlist, Mask * mask)
  98. {
  99. char buf[1024];
  100. char x[2];
  101. FILE *fd;
  102. init_mask_rules(mask);
  103. if (catlist == NULL)
  104. return 0;
  105. for (; *catlist; catlist++) {
  106. if (*catlist[0] == '/') {
  107. fd = fopen(*catlist, "r");
  108. if (fd == NULL) {
  109. perror(*catlist);
  110. G_usage();
  111. exit(EXIT_FAILURE);
  112. }
  113. while (fgets(buf, sizeof buf, fd)) {
  114. if (sscanf(buf, "%1s", x) != 1 || *x == '#')
  115. continue;
  116. parse_mask_rule(buf, mask, *catlist);
  117. }
  118. fclose(fd);
  119. }
  120. else
  121. parse_mask_rule(*catlist, mask, (char *)NULL);
  122. }
  123. return 0;
  124. }
  125. static int parse_vallist(char **vallist, d_Mask * d_mask)
  126. {
  127. char buf[1024];
  128. char x[2];
  129. FILE *fd;
  130. init_d_mask_rules(d_mask);
  131. if (vallist == NULL)
  132. return -1;
  133. for (; *vallist; vallist++) {
  134. if (*vallist[0] == '/') {
  135. fd = fopen(*vallist, "r");
  136. if (fd == NULL) {
  137. perror(*vallist);
  138. G_usage();
  139. exit(EXIT_FAILURE);
  140. }
  141. while (fgets(buf, sizeof buf, fd)) {
  142. if (sscanf(buf, "%1s", x) != 1 || *x == '#')
  143. continue;
  144. parse_d_mask_rule(buf, d_mask, *vallist);
  145. }
  146. fclose(fd);
  147. }
  148. else
  149. parse_d_mask_rule(*vallist, d_mask, (char *)NULL);
  150. }
  151. return 0;
  152. }
  153. int parse_mask_rule(char *catlist, Mask * mask, char *where)
  154. {
  155. long a, b;
  156. char junk[128];
  157. /* #-# */
  158. if (sscanf(catlist, "%ld-%ld", &a, &b) == 2)
  159. add_mask_rule(mask, a, b, 0);
  160. /* inf-# */
  161. else if (sscanf(catlist, "%[^ -\t]-%ld", junk, &a) == 2)
  162. add_mask_rule(mask, a, a, -1);
  163. /* #-inf */
  164. else if (sscanf(catlist, "%ld-%[^ \t]", &a, junk) == 2)
  165. add_mask_rule(mask, a, a, 1);
  166. /* # */
  167. else if (sscanf(catlist, "%ld", &a) == 1)
  168. add_mask_rule(mask, a, a, 0);
  169. else {
  170. if (where)
  171. fprintf(stderr, "%s: ", where);
  172. G_usage();
  173. G_fatal_error(_("[%s]: illegal category specified"), catlist);
  174. }
  175. return 0;
  176. }
  177. int parse_d_mask_rule(char *vallist, d_Mask * d_mask, char *where)
  178. {
  179. double a, b;
  180. char junk[128];
  181. /* #-# */
  182. if (sscanf(vallist, "%lf-%lf", &a, &b) == 2)
  183. add_d_mask_rule(d_mask, a, b, 0);
  184. /* inf-# */
  185. else if (sscanf(vallist, "%[^ -\t]-%lf", junk, &a) == 2)
  186. add_d_mask_rule(d_mask, a, a, -1);
  187. /* #-inf */
  188. else if (sscanf(vallist, "%lf-%[^ \t]", &a, junk) == 2)
  189. add_d_mask_rule(d_mask, a, a, 1);
  190. /* # */
  191. else if (sscanf(vallist, "%lf", &a) == 1)
  192. add_d_mask_rule(d_mask, a, a, 0);
  193. else {
  194. if (where)
  195. fprintf(stderr, "%s: ", where);
  196. G_usage();
  197. G_fatal_error(_("[%s]: illegal value specified"), vallist);
  198. }
  199. return 0;
  200. }