main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /****************************************************************************
  2. *
  3. * MODULE: r.cats
  4. *
  5. * AUTHOR(S): Michael Shapiro - CERL
  6. * Hamish Bowman, Dunedin, New Zealand (label creation opts)
  7. *
  8. * PURPOSE: Prints category values and labels associated with
  9. * user-specified raster map layers.
  10. *
  11. * COPYRIGHT: (C) 2006 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. ***************************************************************************/
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/raster.h>
  23. #include <grass/glocale.h>
  24. #include "local_proto.h"
  25. static struct Categories cats;
  26. static char *fs;
  27. int main(int argc, char *argv[])
  28. {
  29. const char *name;
  30. const char *mapset;
  31. long x, y;
  32. double dx;
  33. RASTER_MAP_TYPE map_type;
  34. int i;
  35. int from_stdin = FALSE;
  36. struct GModule *module;
  37. struct
  38. {
  39. struct Option *map, *fs, *cats, *vals, *raster, *file, *fmt_str,
  40. *fmt_coeff;
  41. } parm;
  42. G_gisinit(argv[0]);
  43. module = G_define_module();
  44. G_add_keyword(_("raster"));
  45. G_add_keyword(_("category"));
  46. module->description =
  47. _("Manages category values and labels associated "
  48. "with user-specified raster map layers.");
  49. parm.map = G_define_standard_option(G_OPT_R_MAP);
  50. parm.cats = G_define_standard_option(G_OPT_V_CATS);
  51. parm.cats->multiple = YES;
  52. parm.cats->guisection = _("Selection");
  53. parm.vals = G_define_option();
  54. parm.vals->key = "values";
  55. parm.vals->type = TYPE_DOUBLE;
  56. parm.vals->multiple = YES;
  57. parm.vals->required = NO;
  58. parm.vals->label = _("Comma separated value list");
  59. parm.vals->description = _("Example: 1.4,3.8,13");
  60. parm.vals->guisection = _("Selection");
  61. parm.fs = G_define_standard_option(G_OPT_F_SEP);
  62. parm.fs->answer = "tab";
  63. parm.raster = G_define_standard_option(G_OPT_R_INPUT);
  64. parm.raster->key = "raster";
  65. parm.raster->required = NO;
  66. parm.raster->description =
  67. _("Raster map from which to copy category table");
  68. parm.raster->guisection = _("Define");
  69. parm.file = G_define_standard_option(G_OPT_F_INPUT);
  70. parm.file->key = "rules";
  71. parm.file->required = NO;
  72. parm.file->description =
  73. _("File containing category label rules (or \"-\" to read from stdin)");
  74. parm.file->guisection = _("Define");
  75. parm.fmt_str = G_define_option();
  76. parm.fmt_str->key = "format";
  77. parm.fmt_str->type = TYPE_STRING;
  78. parm.fmt_str->required = NO;
  79. parm.fmt_str->label =
  80. _("Default label or format string for dynamic labeling");
  81. parm.fmt_str->description =
  82. _("Used when no explicit label exists for the category");
  83. parm.fmt_coeff = G_define_option();
  84. parm.fmt_coeff->key = "coefficients";
  85. parm.fmt_coeff->type = TYPE_DOUBLE;
  86. parm.fmt_coeff->required = NO;
  87. parm.fmt_coeff->key_desc = "mult1,offset1,mult2,offset2";
  88. /* parm.fmt_coeff->answer = "0.0,0.0,0.0,0.0"; */
  89. parm.fmt_coeff->label = _("Dynamic label coefficients");
  90. parm.fmt_coeff->description =
  91. _("Two pairs of category multiplier and offsets, for $1 and $2");
  92. if (G_parser(argc, argv))
  93. exit(EXIT_FAILURE);
  94. name = parm.map->answer;
  95. fs = G_option_to_separator(parm.fs);
  96. mapset = G_find_raster2(name, "");
  97. if (mapset == NULL)
  98. G_fatal_error(_("Raster map <%s> not found"), name);
  99. map_type = Rast_map_type(name, mapset);
  100. /* create category labels */
  101. if (parm.raster->answer || parm.file->answer ||
  102. parm.fmt_str->answer || parm.fmt_coeff->answer) {
  103. /* restrict editing to current mapset */
  104. if (strcmp(mapset, G_mapset()) != 0)
  105. G_fatal_error(_("Raster map <%s> not found in current mapset"),
  106. name);
  107. /* use cats from another map */
  108. if (parm.raster->answer) {
  109. int fd;
  110. const char *cmapset;
  111. cmapset = G_find_raster2(parm.raster->answer, "");
  112. if (cmapset == NULL)
  113. G_fatal_error(_("Raster map <%s> not found"),
  114. parm.raster->answer);
  115. fd = Rast_open_old(name, mapset);
  116. Rast_init_cats("", &cats);
  117. if (0 > Rast_read_cats(parm.raster->answer, cmapset, &cats))
  118. G_fatal_error(_("Unable to read category file of raster map <%s@%s>"),
  119. parm.raster->answer, cmapset);
  120. Rast_write_cats(name, &cats);
  121. G_message(_("Category table for <%s> set from <%s>"),
  122. name, parm.raster->answer);
  123. Rast_close(fd);
  124. }
  125. /* load cats from rules file */
  126. if (parm.file->answer) {
  127. FILE *fp;
  128. char **tokens;
  129. int ntokens;
  130. char *e1;
  131. char *e2;
  132. if (strcmp("-", parm.file->answer) == 0) {
  133. from_stdin = TRUE;
  134. fp = stdin;
  135. }
  136. else {
  137. fp = fopen(parm.file->answer, "r");
  138. if (!fp)
  139. G_fatal_error(_("Unable to open file <%s>"),
  140. parm.file->answer);
  141. }
  142. Rast_init_cats("", &cats);
  143. for (;;) {
  144. char buf[1024];
  145. DCELL d1, d2;
  146. int parse_error = 0;
  147. if (!G_getl2(buf, sizeof(buf), fp))
  148. break;
  149. tokens = G_tokenize(buf, fs);
  150. ntokens = G_number_of_tokens(tokens);
  151. if (ntokens == 3) {
  152. d1 = strtod(tokens[0], &e1);
  153. d2 = strtod(tokens[1], &e2);
  154. if (*e1 == 0 && *e2 == 0)
  155. Rast_set_d_cat(&d1, &d2, tokens[2], &cats);
  156. else
  157. parse_error = 1;
  158. }
  159. else if (ntokens == 2) {
  160. d1 = strtod(tokens[0], &e1);
  161. if (*e1 == 0)
  162. Rast_set_d_cat(&d1, &d1, tokens[1], &cats);
  163. else
  164. parse_error = 1;
  165. }
  166. else if (!strlen(buf))
  167. continue;
  168. else
  169. parse_error = 1;
  170. if (parse_error)
  171. G_fatal_error(_("Incorrect format of input rules. "
  172. "Check separators. Invalid line is:\n%s"), buf);
  173. }
  174. G_free_tokens(tokens);
  175. Rast_write_cats(name, &cats);
  176. if (!from_stdin)
  177. fclose(fp);
  178. }
  179. /* set dynamic cat rules for cats without explicit labels */
  180. if (parm.fmt_str->answer || parm.fmt_coeff->answer) {
  181. char *fmt_str;
  182. double m1, a1, m2, a2;
  183. /* read existing values */
  184. Rast_init_cats("", &cats);
  185. if (0 > Rast_read_cats(name, G_mapset(), &cats))
  186. G_warning(_("Unable to read category file of raster map <%s@%s>"),
  187. name, G_mapset());
  188. if (parm.fmt_str->answer) {
  189. fmt_str =
  190. G_malloc(strlen(parm.fmt_str->answer) > strlen(cats.fmt)
  191. ? strlen(parm.fmt_str->answer) +
  192. 1 : strlen(cats.fmt) + 1);
  193. strcpy(fmt_str, parm.fmt_str->answer);
  194. }
  195. else {
  196. fmt_str = G_malloc(strlen(cats.fmt) + 1);
  197. strcpy(fmt_str, cats.fmt);
  198. }
  199. m1 = cats.m1;
  200. a1 = cats.a1;
  201. m2 = cats.m2;
  202. a2 = cats.a2;
  203. if (parm.fmt_coeff->answer) {
  204. m1 = atof(parm.fmt_coeff->answers[0]);
  205. a1 = atof(parm.fmt_coeff->answers[1]);
  206. m2 = atof(parm.fmt_coeff->answers[2]);
  207. a2 = atof(parm.fmt_coeff->answers[3]);
  208. }
  209. Rast_set_cats_fmt(fmt_str, m1, a1, m2, a2, &cats);
  210. Rast_write_cats(name, &cats);
  211. }
  212. Rast_free_cats(&cats);
  213. exit(EXIT_SUCCESS);
  214. }
  215. else {
  216. if (Rast_read_cats(name, mapset, &cats) < 0)
  217. G_fatal_error(_("Unable to read category file of raster map <%s> in <%s>"),
  218. name, mapset);
  219. }
  220. /* describe the category labels */
  221. /* if no cats requested, use r.describe to get the cats */
  222. if (parm.cats->answer == NULL) {
  223. if (map_type == CELL_TYPE) {
  224. get_cats(name, mapset);
  225. while (next_cat(&x))
  226. print_label(x);
  227. exit(EXIT_SUCCESS);
  228. }
  229. }
  230. else {
  231. if (map_type != CELL_TYPE)
  232. G_warning(_("The map is floating point! Ignoring cats list, using vals list"));
  233. else { /* integer map */
  234. for (i = 0; parm.cats->answers[i]; i++)
  235. if (!scan_cats(parm.cats->answers[i], &x, &y)) {
  236. G_usage();
  237. exit(EXIT_FAILURE);
  238. }
  239. for (i = 0; parm.cats->answers[i]; i++) {
  240. scan_cats(parm.cats->answers[i], &x, &y);
  241. while (x <= y)
  242. print_label(x++);
  243. }
  244. exit(EXIT_SUCCESS);
  245. }
  246. }
  247. if (parm.vals->answer == NULL)
  248. G_fatal_error(_("vals argument is required for floating point map!"));
  249. for (i = 0; parm.vals->answers[i]; i++)
  250. if (!scan_vals(parm.vals->answers[i], &dx)) {
  251. G_usage();
  252. exit(EXIT_FAILURE);
  253. }
  254. for (i = 0; parm.vals->answers[i]; i++) {
  255. scan_vals(parm.vals->answers[i], &dx);
  256. print_d_label(dx);
  257. }
  258. exit(EXIT_SUCCESS);
  259. }
  260. int print_label(long x)
  261. {
  262. char *label;
  263. G_squeeze(label = Rast_get_c_cat((CELL *) &x, &cats));
  264. fprintf(stdout, "%ld%s%s\n", x, fs, label);
  265. return 0;
  266. }
  267. int print_d_label(double x)
  268. {
  269. char *label, tmp[40];
  270. DCELL dtmp;
  271. dtmp = x;
  272. G_squeeze(label = Rast_get_d_cat(&dtmp, &cats));
  273. sprintf(tmp, "%.10f", x);
  274. G_trim_decimal(tmp);
  275. fprintf(stdout, "%s%s%s\n", tmp, fs, label);
  276. return 0;
  277. }
  278. int scan_cats(const char *s, long *x, long *y)
  279. {
  280. char dummy[2];
  281. *dummy = 0;
  282. if (sscanf(s, "%ld-%ld%1s", x, y, dummy) == 2)
  283. return (*dummy == 0 && *x <= *y);
  284. *dummy = 0;
  285. if (sscanf(s, "%ld%1s", x, dummy) == 1 && *dummy == 0) {
  286. *y = *x;
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. int scan_vals(const char *s, double *x)
  292. {
  293. char dummy[10];
  294. *dummy = 0;
  295. if (sscanf(s, "%lf%1s", x, dummy) == 1 && *dummy == 0)
  296. return 1;
  297. return 0;
  298. }