read_rules.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <grass/raster.h>
  5. #include <grass/glocale.h>
  6. #include "global.h"
  7. #define INCR 20
  8. int report_range(void)
  9. {
  10. struct FPRange drange;
  11. struct Range range;
  12. char buff[1024], buff2[300];
  13. RASTER_MAP_TYPE inp_type;
  14. inp_type = Rast_map_type(name, "");
  15. if (inp_type != CELL_TYPE) {
  16. if (Rast_read_fp_range(name, "", &drange) <= 0)
  17. G_fatal_error(_("Unable to read f_range for map %s"), name);
  18. Rast_get_fp_range_min_max(&drange, &old_dmin, &old_dmax);
  19. if (Rast_is_d_null_value(&old_dmin) || Rast_is_d_null_value(&old_dmax))
  20. G_message(_("Data range is empty"));
  21. else {
  22. sprintf(buff, "%.10f", old_dmin);
  23. sprintf(buff2, "%.10f", old_dmax);
  24. G_trim_decimal(buff);
  25. G_trim_decimal(buff2);
  26. G_message(_("Data range of %s is %s to %s (entire map)"), name,
  27. buff, buff2);
  28. }
  29. }
  30. if (Rast_read_range(name, "", &range) <= 0)
  31. G_fatal_error(_("Unable to read range for map <%s>"), name);
  32. Rast_get_range_min_max(&range, &old_min, &old_max);
  33. if (Rast_is_c_null_value(&old_min) || Rast_is_c_null_value(&old_max))
  34. G_message(_("Integer data range of %s is empty"), name);
  35. else
  36. G_message(_("Integer data range of %s is %d to %d"),
  37. name, (int)old_min, (int)old_max);
  38. return 0;
  39. }
  40. int read_rules(FILE * fp)
  41. {
  42. char buf[1024];
  43. DCELL oLow, oHigh, nLow, nHigh;
  44. int line, n;
  45. in_type = out_type = CELL_TYPE;
  46. rules = (char **)G_malloc(INCR * sizeof(char *));
  47. rule_size = INCR;
  48. if (isatty(fileno(fp))) {
  49. report_range();
  50. G_message(_("Enter the rule or 'help' for the format description"));
  51. }
  52. Rast_fpreclass_init(&rcl_struct);
  53. for (line = 1;; line++) {
  54. if (isatty(fileno(fp)))
  55. fprintf(stderr, "> ");
  56. if (!G_getl2(buf, 1024, fp))
  57. return nrules;
  58. G_debug(5, "buf = [%s], strlen(buf)=%d", buf, strlen(buf));
  59. for (n = 0; buf[n]; n++)
  60. if (buf[n] == ',')
  61. buf[n] = ' ';
  62. G_strip(buf);
  63. if (*buf == 0)
  64. continue;
  65. if (*buf == '#')
  66. continue;
  67. if (strcmp(buf, "end") == 0)
  68. break;
  69. if (strcmp(buf, "help") == 0) {
  70. G_message(_("Enter a rule in one of these formats:"));
  71. G_message(" ");
  72. G_message(_("old_low:old_high:new_low:new_high"));
  73. G_message(_("old_low:old_high:new_val (i.e. new_high == new_low)"));
  74. G_message(_("*:old_val:new_val (interval [inf, old_val])"));
  75. G_message(_("old_val:*:new_val (interval [old_val, inf])"));
  76. G_message(" ");
  77. G_message(_("When finished type \"end\"."));
  78. continue;
  79. }
  80. /* we read and record into quant table all values, even int as doubles
  81. we convert the range and domain values to the right format when we
  82. lookup the values in the quant table */
  83. switch (sscanf(buf, "%lf:%lf:%lf:%lf", &oLow, &oHigh, &nLow, &nHigh)) {
  84. case 3:
  85. update_type(&in_type, oLow);
  86. update_type(&in_type, oHigh);
  87. update_type(&out_type, nLow);
  88. update_rules(buf);
  89. Rast_fpreclass_add_rule(&rcl_struct, oLow, oHigh, nLow, nLow);
  90. break;
  91. case 4:
  92. update_type(&in_type, oLow);
  93. update_type(&in_type, oHigh);
  94. update_type(&out_type, nLow);
  95. update_type(&out_type, nHigh);
  96. update_rules(buf);
  97. Rast_fpreclass_add_rule(&rcl_struct, oLow, oHigh, nLow, nHigh);
  98. break;
  99. default:
  100. if (sscanf(buf, "%lf:*:%lf", &oLow, &nLow) == 2) {
  101. update_type(&in_type, oLow);
  102. update_type(&out_type, nLow);
  103. update_rules(buf);
  104. Rast_fpreclass_set_pos_infinite_rule(&rcl_struct, oLow, nLow);
  105. }
  106. else if (sscanf(buf, "*:%lf:%lf", &oHigh, &nLow) == 2) {
  107. update_type(&in_type, oHigh);
  108. update_type(&out_type, nLow);
  109. update_rules(buf);
  110. Rast_fpreclass_set_neg_infinite_rule(&rcl_struct, oHigh, nLow);
  111. }
  112. else
  113. G_message(_("%s is not a valid rule"), buf);
  114. break;
  115. } /* switch */
  116. } /* loop */
  117. return nrules;
  118. }
  119. int update_type(RASTER_MAP_TYPE * map_type, DCELL val)
  120. {
  121. /* check if val is not an integer number */
  122. if (make_dcell)
  123. *map_type = DCELL_TYPE;
  124. else {
  125. if ((DCELL) ((CELL) val) != val)
  126. *map_type = FCELL_TYPE;
  127. }
  128. return 0;
  129. }
  130. int update_rules(char *buf)
  131. {
  132. int buf_size;
  133. buf_size = strlen(buf) + 1; /* +1 for null char */
  134. if (rule_size <= nrules) {
  135. while (rule_size <= nrules)
  136. rule_size += INCR;
  137. rules = (char **)G_realloc(rules, rule_size * sizeof(char *));
  138. }
  139. rules[nrules] = (char *)G_malloc(sizeof(char) * (buf_size + 1));
  140. strncpy(rules[nrules], buf, buf_size);
  141. nrules++;
  142. return 0;
  143. }