main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /****************************************************************************
  2. *
  3. * MODULE: r.what
  4. * AUTHOR(S): Michael Shapiro, CERL (original contributor)
  5. * Markus Neteler <neteler itc.it>,Brad Douglas <rez touchofmadness.com>,
  6. * Huidae Cho <grass4u gmail.com>, Glynn Clements <glynn gclements.plus.com>,
  7. * Hamish Bowman <hamish_b yahoo.com>, Soeren Gebbert <soeren.gebbert gmx.de>
  8. * PURPOSE:
  9. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/raster.h>
  22. #include <grass/glocale.h>
  23. static const char *fmt;
  24. static int do_value(const char *buf, RASTER_MAP_TYPE type,
  25. struct Colors *colors)
  26. {
  27. CELL ival;
  28. DCELL fval;
  29. int red, grn, blu;
  30. switch (type) {
  31. case CELL_TYPE:
  32. if (sscanf(buf, "%d", &ival) != 1) {
  33. fprintf(stdout, "*: *\n");
  34. return 0;
  35. }
  36. if (!Rast_get_c_color(&ival, &red, &grn, &blu, colors)) {
  37. fprintf(stdout, "%d: *\n", ival);
  38. return 0;
  39. }
  40. fprintf(stdout, "%d: ", ival);
  41. fprintf(stdout, fmt, red, grn, blu);
  42. fprintf(stdout, "\n");
  43. return 1;
  44. case FCELL_TYPE:
  45. case DCELL_TYPE:
  46. if (sscanf(buf, "%lf", &fval) != 1) {
  47. fprintf(stdout, "*: *\n");
  48. return 0;
  49. }
  50. if (!Rast_get_d_color(&fval, &red, &grn, &blu, colors)) {
  51. fprintf(stdout, "%.15g: *\n", fval);
  52. return 0;
  53. }
  54. fprintf(stdout, "%.15g: ", fval);
  55. fprintf(stdout, fmt, red, grn, blu);
  56. fprintf(stdout, "\n");
  57. return 1;
  58. default:
  59. G_fatal_error("Invalid map type %d", type);
  60. return 0;
  61. }
  62. }
  63. int main(int argc, char **argv)
  64. {
  65. struct GModule *module;
  66. struct
  67. {
  68. struct Option *input, *value, *format;
  69. } opt;
  70. struct
  71. {
  72. struct Flag *i;
  73. } flag;
  74. const char *name;
  75. struct Colors colors;
  76. RASTER_MAP_TYPE type;
  77. G_gisinit(argv[0]);
  78. module = G_define_module();
  79. G_add_keyword(_("raster"));
  80. G_add_keyword(_("querying"));
  81. G_add_keyword(_("color table"));
  82. module->description = _("Queries colors for a raster map layer.");
  83. opt.input = G_define_option();
  84. opt.input->key = "input";
  85. opt.input->type = TYPE_STRING;
  86. opt.input->required = YES;
  87. opt.input->multiple = NO;
  88. opt.input->gisprompt = "old,cell,raster";
  89. opt.input->description = _("Name of existing raster map to query colors");
  90. opt.value = G_define_option();
  91. opt.value->key = "value";
  92. opt.value->type = TYPE_DOUBLE;
  93. opt.value->required = NO;
  94. opt.value->multiple = YES;
  95. opt.value->description = _("Values to query colors for");
  96. opt.format = G_define_option();
  97. opt.format->key = "format";
  98. opt.format->type = TYPE_STRING;
  99. opt.format->required = NO;
  100. opt.format->answer = "%d:%d:%d";
  101. opt.format->description = _("Output format (printf-style)");
  102. flag.i = G_define_flag();
  103. flag.i->key = 'i';
  104. flag.i->description = _("Read values from stdin");
  105. if (G_parser(argc, argv))
  106. exit(EXIT_FAILURE);
  107. if (!opt.value->answer && !flag.i->answer)
  108. G_fatal_error(_("Either \"-i\" or \"value=\" must be given"));
  109. name = opt.input->answer;
  110. type = Rast_map_type(name, "");
  111. if (type < 0)
  112. G_fatal_error("Unable to determine type of input map %s", name);
  113. if (Rast_read_colors(name, "", &colors) < 0)
  114. G_fatal_error("Unable to read colors for input map %s", name);
  115. fmt = opt.format->answer;
  116. if (flag.i->answer) {
  117. for (;;) {
  118. char buf[64];
  119. if (!fgets(buf, sizeof(buf), stdin))
  120. break;
  121. do_value(buf, type, &colors);
  122. }
  123. }
  124. else if (opt.value->answer) {
  125. const char *ans;
  126. int i;
  127. for (i = 0; ans = opt.value->answers[i], ans; i++)
  128. do_value(ans, type, &colors);
  129. }
  130. return EXIT_SUCCESS;
  131. }