main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /****************************************************************************
  2. *
  3. * MODULE: r.rescale
  4. * AUTHOR(S): Michael Shapiro, CERL (original contributor)
  5. * Jachym Cepicky <jachym les-ejk.cz>, Jan-Oliver Wagner <jan intevation.de>
  6. * PURPOSE:
  7. * COPYRIGHT: (C) 1999-2006 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. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <grass/gis.h>
  18. #include "local_proto.h"
  19. #include <grass/glocale.h>
  20. int main(int argc, char *argv[])
  21. {
  22. char input[GNAME_MAX+8];
  23. char output[GNAME_MAX+8];
  24. char rules[GNAME_MAX+8];
  25. char title[GPATH_MAX];
  26. const char *args[6];
  27. struct Popen child;
  28. FILE *fp;
  29. long old_min, old_max;
  30. long new_min, new_max;
  31. long new_delta, old_delta;
  32. long value, first, prev;
  33. long cat;
  34. float divisor;
  35. char *old_name;
  36. char *new_name;
  37. struct GModule *module;
  38. struct
  39. {
  40. struct Option *input, *from, *output, *to, *title;
  41. } parm;
  42. G_gisinit(argv[0]);
  43. module = G_define_module();
  44. G_add_keyword(_("raster"));
  45. G_add_keyword(_("rescale"));
  46. module->description =
  47. _("Rescales the range of category values " "in a raster map layer.");
  48. /* Define the different options */
  49. parm.input = G_define_option();
  50. parm.input->key = "input";
  51. parm.input->type = TYPE_STRING;
  52. parm.input->required = YES;
  53. parm.input->gisprompt = "old,cell,raster";
  54. parm.input->description = _("The name of the raster map to be rescaled");
  55. parm.from = G_define_option();
  56. parm.from->key = "from";
  57. parm.from->key_desc = "min,max";
  58. parm.from->type = TYPE_INTEGER;
  59. parm.from->required = NO;
  60. parm.from->description =
  61. _("The input data range to be rescaled (default: full range of input map)");
  62. parm.output = G_define_option();
  63. parm.output->key = "output";
  64. parm.output->type = TYPE_STRING;
  65. parm.output->required = YES;
  66. parm.output->gisprompt = "new,cell,raster";
  67. parm.output->description = _("The resulting raster map name");
  68. parm.to = G_define_option();
  69. parm.to->key = "to";
  70. parm.to->key_desc = "min,max";
  71. parm.to->type = TYPE_INTEGER;
  72. parm.to->required = YES;
  73. parm.to->description = _("The output data range");
  74. parm.title = G_define_option();
  75. parm.title->key = "title";
  76. parm.title->key_desc = "phrase";
  77. parm.title->type = TYPE_STRING;
  78. parm.title->required = NO;
  79. parm.title->description = _("Title for new raster map");
  80. if (G_parser(argc, argv))
  81. exit(EXIT_FAILURE);
  82. old_name = parm.input->answer;
  83. new_name = parm.output->answer;
  84. if (parm.from->answer) {
  85. sscanf(parm.from->answers[0], "%ld", &old_min);
  86. sscanf(parm.from->answers[1], "%ld", &old_max);
  87. }
  88. else
  89. get_range(old_name, &old_min, &old_max);
  90. if (old_min > old_max) {
  91. value = old_min; /* swap */
  92. old_min = old_max;
  93. old_max = value;
  94. }
  95. sscanf(parm.to->answers[0], "%ld", &new_min);
  96. sscanf(parm.to->answers[1], "%ld", &new_max);
  97. if (new_min > new_max) {
  98. value = new_min; /* swap */
  99. new_min = new_max;
  100. new_max = value;
  101. }
  102. G_message(_("Rescale %s[%ld,%ld] to %s[%ld,%ld]"),
  103. old_name, old_min, old_max, new_name, new_min, new_max);
  104. sprintf(input, "input=%s", old_name);
  105. sprintf(output, "output=%s", new_name);
  106. if (parm.title->answer)
  107. sprintf(title, "title=%s", parm.title->answer);
  108. else
  109. sprintf(title, "title=rescale of %s", old_name);
  110. sprintf(rules, "rules=-");
  111. args[0] = "r.reclass";
  112. args[1] = input;
  113. args[2] = output;
  114. args[3] = title;
  115. args[4] = rules;
  116. args[5] = NULL;
  117. fp = G_popen_write(&child, "r.reclass", args);
  118. old_delta = old_max - old_min;
  119. new_delta = new_max - new_min;
  120. divisor = (float)new_delta / (float)old_delta;
  121. prev = new_min;
  122. first = old_min;
  123. for (cat = old_min; cat <= old_max; cat++) {
  124. value = (int)(divisor * (cat - old_min) + new_min + .5);
  125. if (value != prev) {
  126. fprintf(fp, "%ld thru %ld = %ld %ld", first, cat - 1, prev,
  127. first);
  128. if (cat - 1 != first)
  129. fprintf(fp, " thru %ld", cat - 1);
  130. fprintf(fp, "\n");
  131. prev = value;
  132. first = cat;
  133. }
  134. }
  135. fprintf(fp, "%ld thru %ld = %ld %ld", first, cat - 1, prev, first);
  136. if (cat - 1 != first)
  137. fprintf(fp, " thru %ld", cat - 1);
  138. fprintf(fp, "\n");
  139. G_popen_close(&child);
  140. return EXIT_SUCCESS;
  141. }