main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /****************************************************************************
  2. *
  3. * MODULE: d.what.rast
  4. * AUTHOR(S): Michael Shapiro (CERL) (original contributor)
  5. * Markus Neteler <neteler itc.it>,
  6. * Andreas Lange <andreas.lange rhein-main.de>,
  7. * Bernhard Reiter <bernhard intevation.de>,
  8. * Huidae Cho <grass4u gmail.com>,
  9. * Eric G. Miller <egm2 jps.net>,
  10. * Glynn Clements <glynn gclements.plus.com>,
  11. * Hamish Bowman <hamish_b yahoo.com>
  12. * PURPOSE: interactive query of cat/label of raster map in display
  13. * COPYRIGHT: (C) 1999-2006 by the GRASS Development Team
  14. *
  15. * This program is free software under the GNU General Public
  16. * License (>=v2). Read the file COPYING that comes with GRASS
  17. * for details.
  18. *
  19. *****************************************************************************/
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "what.h"
  23. #include <grass/display.h>
  24. #include <grass/raster.h>
  25. #include <grass/glocale.h>
  26. #include "local_proto.h"
  27. struct Categories *cats;
  28. int *fd;
  29. char **rast;
  30. int nrasts;
  31. char **name;
  32. char **mapset;
  33. int main(int argc, char **argv)
  34. {
  35. int i, j;
  36. int width, mwidth;
  37. struct Flag *once, *terse, *colrow;
  38. struct Option *opt1, *fs;
  39. struct GModule *module;
  40. /* Initialize the GIS calls */
  41. G_gisinit(argv[0]);
  42. module = G_define_module();
  43. G_add_keyword(_("display"));
  44. G_add_keyword(_("raster"));
  45. G_add_keyword(_("position"));
  46. G_add_keyword(_("querying"));
  47. module->description =
  48. _("Allows the user to interactively query the category contents "
  49. "of multiple raster map layers at user specified locations "
  50. "within the current geographic region.");
  51. rast = NULL;
  52. opt1 = G_define_option();
  53. opt1->key = "map";
  54. opt1->type = TYPE_STRING;
  55. opt1->required = YES;
  56. opt1->multiple = YES;
  57. opt1->gisprompt = "old,cell,raster";
  58. opt1->description = _("Name of existing raster map(s)");
  59. opt1->key_desc = "name";
  60. fs = G_define_option();
  61. fs->key = "fs";
  62. fs->type = TYPE_STRING;
  63. fs->required = NO;
  64. fs->answer = ":";
  65. fs->description = _("Field separator (terse mode only)");
  66. fs->key_desc = "character";
  67. once = G_define_flag();
  68. once->key = '1';
  69. once->description = _("Identify just one location");
  70. terse = G_define_flag();
  71. terse->key = 't';
  72. terse->description = _("Terse output. For parsing by programs");
  73. colrow = G_define_flag();
  74. colrow->key = 'c';
  75. colrow->description =
  76. _("Print out col/row for the entire map in grid resolution of the region");
  77. if (G_parser(argc, argv))
  78. exit(EXIT_FAILURE);
  79. if (opt1->answers && opt1->answers[0])
  80. rast = opt1->answers;
  81. if (D_open_driver() != 0)
  82. G_fatal_error(_("No graphics device selected"));
  83. D_setup(0);
  84. if (rast) {
  85. for (i = 0; rast[i]; i++) ;
  86. nrasts = i;
  87. fd = (int *)G_malloc(nrasts * sizeof(int));
  88. name = (char **)G_malloc(nrasts * sizeof(char *));
  89. mapset = (char **)G_malloc(nrasts * sizeof(char *));
  90. cats =
  91. (struct Categories *)G_malloc(nrasts * sizeof(struct Categories));
  92. width = mwidth = 0;
  93. for (i = 0; i < nrasts; i++) {
  94. name[i] = (char *)G_malloc(GNAME_MAX);
  95. mapset[i] = (char *)G_malloc(GMAPSET_MAX);
  96. if ((fd[i] = opencell(rast[i], name[i], mapset[i])) < 0)
  97. G_fatal_error(_("Raster map <%s> not found"), rast[i]);
  98. j = strlen(name[i]);
  99. if (j > width)
  100. width = j;
  101. j = strlen(mapset[i]);
  102. if (j > mwidth)
  103. mwidth = j;
  104. if (Rast_read_cats(name[i], mapset[i], &cats[i]) < 0)
  105. cats[i].ncats = -1;
  106. }
  107. }
  108. what(once->answer, terse->answer, colrow->answer, fs->answer, width,
  109. mwidth);
  110. D_close_driver();
  111. exit(EXIT_SUCCESS);
  112. }