raster_main.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /****************************************************************************
  2. *
  3. * MODULE: r.colors.out
  4. *
  5. * AUTHOR(S): Glynn Clements
  6. *
  7. * PURPOSE: Allows export of the color table for a raster map.
  8. *
  9. * COPYRIGHT: (C) 2008, 2010-2011 Glynn Clements and the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General
  12. * Public License (>=v2). Read the file COPYING that
  13. * comes with GRASS for details.
  14. *
  15. ***************************************************************************/
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <grass/gis.h>
  19. #include <grass/raster.h>
  20. #include <grass/glocale.h>
  21. /* Run in raster mode */
  22. int main(int argc, char **argv)
  23. {
  24. struct GModule *module;
  25. struct
  26. {
  27. struct Option *map, *file;
  28. } opt;
  29. struct
  30. {
  31. struct Flag *p;
  32. } flag;
  33. const char *file;
  34. FILE * fp;
  35. struct Colors colors;
  36. struct FPRange range;
  37. G_gisinit(argv[0]);
  38. module = G_define_module();
  39. G_add_keyword(_("raster"));
  40. G_add_keyword(_("color table"));
  41. G_add_keyword(_("export"));
  42. module->description =
  43. _("Exports the color table associated with a raster map.");
  44. opt.map = G_define_standard_option(G_OPT_R_MAP);
  45. opt.file = G_define_standard_option(G_OPT_F_OUTPUT);
  46. opt.file->key = "rules";
  47. opt.file->label = _("Path to output rules file");
  48. opt.file->description = _("If not given write to standard output");
  49. opt.file->required = NO;
  50. flag.p = G_define_flag();
  51. flag.p->key = 'p';
  52. flag.p->description = _("Output values as percentages");
  53. if (G_parser(argc, argv))
  54. exit(EXIT_FAILURE);
  55. file = opt.file->answer;
  56. if (Rast_read_colors(opt.map->answer, "", &colors) < 0)
  57. G_fatal_error(_("Unable to read color table for raster map <%s>"),
  58. opt.map->answer);
  59. Rast_read_fp_range(opt.map->answer, "", &range);
  60. if (!file || strcmp(file, "-") == 0)
  61. fp = stdout;
  62. else {
  63. fp = fopen(file, "w");
  64. if (!fp)
  65. G_fatal_error(_("Unable to open output file <%s>"), file);
  66. }
  67. Rast_print_colors(&colors, range.min, range.max, fp,
  68. flag.p->answer ? 1 : 0);
  69. exit(EXIT_SUCCESS);
  70. }