r_colortable.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Function: ctablfile
  2. **
  3. ** Author: Paul W. Carlson April 1992
  4. */
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <grass/colors.h>
  8. #include <grass/raster.h>
  9. #include <grass/glocale.h>
  10. #include "colortable.h"
  11. #include "clr.h"
  12. #include "local_proto.h"
  13. #define KEY(x) (strcmp(key,x)==0)
  14. static char *help[] = {
  15. "where x y",
  16. "width table_width",
  17. "height fptable_height",
  18. "lwidth line_width",
  19. "raster raster_name",
  20. "range min max",
  21. "cols columns",
  22. "font fontname",
  23. "fontsize fontsize",
  24. "color color",
  25. "nodata Y|n",
  26. "tickbar y|N"
  27. "discrete y|n"
  28. ""
  29. };
  30. int read_colortable(void)
  31. {
  32. char buf[1024];
  33. char *key, *data;
  34. char name[GNAME_MAX], mapset[GMAPSET_MAX];
  35. int fontsize, cols, nodata, tickbar, discrete;
  36. double w, h, x, y, lw;
  37. int range_override;
  38. double min, max, tmpD;
  39. int r, g, b, ret;
  40. PSCOLOR color;
  41. fontsize = 10;
  42. set_color(&color, 0, 0, 0);
  43. cols = 1;
  44. h = w = x = y = 0.0;
  45. lw = 1;
  46. ct.font = G_store("Helvetica");
  47. ct.nodata = TRUE;
  48. ct.tickbar = FALSE;
  49. ct.discrete = -1; /* default: TRUE for CELL map, FALSE for FP maps */
  50. range_override = FALSE;
  51. while (input(2, buf, help)) {
  52. if (!key_data(buf, &key, &data))
  53. continue;
  54. if (KEY("where")) {
  55. if (sscanf(data, "%lf %lf", &x, &y) != 2) {
  56. x = y = 0.0;
  57. error(key, data, _("illegal where request"));
  58. }
  59. else
  60. continue;
  61. }
  62. if (KEY("width")) {
  63. if (sscanf(data, "%lf", &w) != 1 || w <= 0) {
  64. error(key, data, _("illegal width request"));
  65. }
  66. else
  67. continue;
  68. }
  69. if (KEY("height")) {
  70. if (sscanf(data, "%lf", &h) != 1 || h <= 0) {
  71. error(key, data, _("illegal height request"));
  72. }
  73. else
  74. continue;
  75. }
  76. if (KEY("lwidth")) {
  77. if (sscanf(data, "%lf", &lw) != 1 || lw < 0) {
  78. error(key, data, _("illegal width request"));
  79. }
  80. else
  81. continue;
  82. }
  83. if (KEY("raster")) {
  84. if (scan_gis("cell", "raster", key, data, name, mapset, 0)) {
  85. ct.name = G_store(name);
  86. ct.mapset = G_store(mapset);
  87. continue;
  88. }
  89. }
  90. if (KEY("range")) {
  91. if (sscanf(data, "%lf %lf", &min, &max) != 2) {
  92. range_override = FALSE;
  93. error(key, data, _("illegal range request"));
  94. }
  95. else {
  96. range_override = TRUE;
  97. if (min > max) { /* flip if needed */
  98. tmpD = min;
  99. min = max;
  100. max = tmpD;
  101. }
  102. continue;
  103. }
  104. }
  105. if (KEY("cols")) {
  106. if (sscanf(data, "%d", &cols) != 1) {
  107. cols = 1;
  108. error(key, data, _("illegal columns request"));
  109. }
  110. else
  111. continue;
  112. }
  113. if (KEY("fontsize")) {
  114. fontsize = atoi(data);
  115. if (fontsize < 4 || fontsize > 50)
  116. fontsize = 0;
  117. continue;
  118. }
  119. if (KEY("color")) {
  120. ret = G_str_to_color(data, &r, &g, &b);
  121. if (ret == 1)
  122. set_color(&color, r, g, b);
  123. else if (ret == 2) /* i.e. "none" */
  124. /* unset_color(&color); */
  125. error(key, data, _("Unsupported color request (colortable)"));
  126. else
  127. error(key, data, _("illegal color request (colortable)"));
  128. continue;
  129. }
  130. if (KEY("font")) {
  131. get_font(data);
  132. ct.font = G_store(data);
  133. continue;
  134. }
  135. if (KEY("nodata")) {
  136. nodata = yesno(key, data);
  137. ct.nodata = nodata;
  138. continue;
  139. }
  140. if (KEY("tickbar")) {
  141. tickbar = yesno(key, data);
  142. ct.tickbar = tickbar;
  143. continue;
  144. }
  145. if (KEY("discrete")) {
  146. discrete = yesno(key, data);
  147. ct.discrete = discrete;
  148. continue;
  149. }
  150. error(key, data, _("illegal colortable sub-request"));
  151. }
  152. ct.x = x;
  153. ct.y = y;
  154. if (fontsize)
  155. ct.fontsize = fontsize;
  156. /* Check for Raster */
  157. if (!ct.name) {
  158. if (!PS.cell_name) {
  159. error(key, data, _("No raster selected for colortable!"));
  160. }
  161. else {
  162. ct.name = PS.cell_name;
  163. ct.mapset = PS.cell_mapset;
  164. }
  165. }
  166. /* set default if legend type was not specified */
  167. if (ct.discrete == -1) {
  168. if (Rast_map_is_fp(ct.name, ct.mapset))
  169. ct.discrete = FALSE;
  170. else
  171. ct.discrete = TRUE;
  172. }
  173. ct.min = min;
  174. ct.max = max;
  175. ct.range_override = range_override;
  176. ct.width = w;
  177. ct.height = h;
  178. ct.lwidth = lw;
  179. ct.color = color;
  180. ct.cols = cols;
  181. return 0;
  182. }