support.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Update a history file. Some of the digit file information is placed in
  3. * the hist file.
  4. * returns 0 - successful creation of history file
  5. * -1 - error
  6. */
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <grass/gis.h>
  10. #include <grass/dbmi.h>
  11. #include <grass/Vect.h>
  12. #include <grass/glocale.h>
  13. #include "local.h"
  14. int update_hist(char *raster_name, char *vector_name,
  15. char *vector_mapset, long scale)
  16. {
  17. struct History hist;
  18. if (raster_name == NULL)
  19. return (-1);
  20. if (G_read_history(raster_name, G_mapset(), &hist) < 0)
  21. return -1;
  22. strcpy(hist.title, raster_name);
  23. /* store information from digit file into history */
  24. sprintf(hist.datsrc_1, "Vector Map: %s in mapset %s", vector_name,
  25. vector_mapset);
  26. sprintf(hist.datsrc_2, "Original scale from vector map: 1:%ld", scale); /* 4.0 */
  27. /* store command line options */
  28. G_command_history(&hist);
  29. return (G_write_history(raster_name, &hist));
  30. }
  31. int update_colors(char *raster_name)
  32. {
  33. struct Range range;
  34. struct Colors colors;
  35. CELL min, max;
  36. G_read_range(raster_name, G_mapset(), &range);
  37. G_get_range_min_max(&range, &min, &max);
  38. G_make_rainbow_colors(&colors, min, max);
  39. G_write_colors(raster_name, G_mapset(), &colors);
  40. return 0;
  41. }
  42. int update_fcolors(char *raster_name)
  43. {
  44. struct FPRange range;
  45. struct Colors colors;
  46. DCELL min, max;
  47. G_read_fp_range(raster_name, G_mapset(), &range);
  48. G_get_fp_range_min_max(&range, &min, &max);
  49. G_make_rainbow_colors(&colors, (CELL) min, (CELL) max);
  50. G_write_colors(raster_name, G_mapset(), &colors);
  51. return 0;
  52. }
  53. int update_cats(char *raster_name)
  54. {
  55. /* TODO: maybe attribute transfer from vector map?
  56. Use G_set_raster_cat() somewhere */
  57. struct Categories cats;
  58. G_strip(raster_name);
  59. G_init_cats((CELL) 0, raster_name, &cats);
  60. G_write_cats(raster_name, &cats);
  61. return 0;
  62. }
  63. int update_dbcolors(char *rast_name, char *vector_map, int field,
  64. char *rgb_column, int is_fp, char *attr_column)
  65. {
  66. int i;
  67. /* Map */
  68. struct Map_info Map;
  69. /* Attributes */
  70. int nrec;
  71. struct field_info *Fi;
  72. dbDriver *Driver;
  73. dbCatValArray cvarr;
  74. /* colors */
  75. int cat;
  76. struct Colors colors;
  77. struct My_color_rule
  78. {
  79. int red;
  80. int green;
  81. int blue;
  82. double d;
  83. int i;
  84. } *my_color_rules;
  85. int colors_n_values = 0;
  86. int red;
  87. int grn;
  88. int blu;
  89. /* init colors structure */
  90. G_init_colors(&colors);
  91. /* open vector map and database driver */
  92. Vect_open_old(&Map, vector_map, G_find_vector2(vector_map, ""));
  93. db_CatValArray_init(&cvarr);
  94. if ((Fi = Vect_get_field(&Map, field)) == NULL)
  95. G_fatal_error(_("Database connection not defined for layer %d"),
  96. field);
  97. if ((Driver =
  98. db_start_driver_open_database(Fi->driver, Fi->database)) == NULL)
  99. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  100. Fi->database, Fi->driver);
  101. /* get number of records in attr_column */
  102. if ((nrec =
  103. db_select_CatValArray(Driver, Fi->table, Fi->key, attr_column, NULL,
  104. &cvarr)) == -1)
  105. G_fatal_error(_("Unknown column <%s> in table <%s>"), attr_column,
  106. Fi->table);
  107. if (nrec < 0)
  108. G_fatal_error(_("No records selected from table <%s>"), Fi->table);
  109. G_debug(3, "nrec = %d", nrec);
  110. /* allocate space for color rules */
  111. my_color_rules =
  112. (struct My_color_rule *)G_malloc(sizeof(struct My_color_rule) * nrec);
  113. /* for each attribute */
  114. for (i = 0; i < cvarr.n_values; i++) {
  115. char colorstring[12];
  116. dbValue value;
  117. /* selecect color attribute and category */
  118. cat = cvarr.value[i].cat;
  119. if (db_select_value
  120. (Driver, Fi->table, Fi->key, cat, rgb_column, &value) < 0) {
  121. G_warning(_("No records selected"));
  122. continue;
  123. }
  124. sprintf(colorstring, "%s", value.s.string);
  125. /* convert color string to three color integers */
  126. if (*colorstring != '\0') {
  127. G_debug(3, "element colorstring: %s", colorstring);
  128. if (G_str_to_color(colorstring, &red, &grn, &blu) == 1) {
  129. G_debug(3, "cat %d r:%d g:%d b:%d", cat, red, grn, blu);
  130. }
  131. else {
  132. G_warning(_("Error in color definition column (%s) "
  133. "with cat %d: colorstring [%s]"), rgb_column, cat,
  134. colorstring);
  135. G_warning(_("Color set to [200:200:200]"));
  136. red = grn = blu = 200;
  137. }
  138. }
  139. else {
  140. G_warning(_("Error in color definition column (%s), with cat %d"),
  141. rgb_column, cat);
  142. }
  143. /* append color rules to my_color_rules array, they will be set
  144. * later all togheter */
  145. colors_n_values++;
  146. my_color_rules[i].red = red;
  147. my_color_rules[i].green = grn;
  148. my_color_rules[i].blue = blu;
  149. if (is_fp) {
  150. my_color_rules[i].d = cvarr.value[i].val.d;
  151. G_debug(2, "val: %f rgb: %s", cvarr.value[i].val.d, colorstring);
  152. }
  153. else {
  154. my_color_rules[i].i = cvarr.value[i].val.i;
  155. G_debug(2, "val: %d rgb: %s", cvarr.value[i].val.i, colorstring);
  156. }
  157. } /* /for each value in database */
  158. /* close the database driver */
  159. db_close_database_shutdown_driver(Driver);
  160. /* set the color rules: for each rule */
  161. for (i = 0; i < colors_n_values - 1; i++) {
  162. if (is_fp) { /* add floating point color rule */
  163. G_add_d_raster_color_rule(&my_color_rules[i].d,
  164. my_color_rules[i].red,
  165. my_color_rules[i].green,
  166. my_color_rules[i].blue,
  167. &my_color_rules[i + 1].d,
  168. my_color_rules[i + 1].red,
  169. my_color_rules[i + 1].green,
  170. my_color_rules[i + 1].blue, &colors);
  171. }
  172. else { /* add CELL color rule */
  173. G_add_color_rule((CELL) my_color_rules[i].i,
  174. my_color_rules[i].red, my_color_rules[i].green,
  175. my_color_rules[i].blue,
  176. (CELL) my_color_rules[i + 1].i,
  177. my_color_rules[i + 1].red,
  178. my_color_rules[i + 1].green,
  179. my_color_rules[i + 1].blue, &colors);
  180. }
  181. }
  182. /* write the rules */
  183. G_write_colors(rast_name, G_mapset(), &colors);
  184. return 1;
  185. }
  186. /* add labels to raster cells */
  187. int update_labels(char *rast_name, char *vector_map, int field,
  188. char *label_column, int use, int val, char *attr_column)
  189. {
  190. int i;
  191. int fd;
  192. /* Map */
  193. struct Map_info Map;
  194. /* Attributes */
  195. int nrec;
  196. struct field_info *Fi;
  197. dbDriver *Driver;
  198. dbCatValArray cvarr;
  199. int col_type;
  200. /* labels */
  201. struct Categories rast_cats;
  202. int labels_n_values = 0;
  203. struct My_labels_rule
  204. {
  205. dbString label;
  206. double d;
  207. int i;
  208. } *my_labels_rules;
  209. /* init raster categories */
  210. G_init_cats((CELL) 0, "Categories", &rast_cats);
  211. if (!(fd = G_open_cell_old(rast_name, G_mapset())))
  212. G_fatal_error(_("Unable to open raster map <%s>"), rast_name);
  213. switch (use) {
  214. case USE_ATTR:
  215. {
  216. G_set_raster_cats_title("Labels", &rast_cats);
  217. int is_fp = G_raster_map_is_fp(rast_name, G_mapset());
  218. /* open vector map and database driver */
  219. Vect_open_old(&Map, vector_map, G_find_vector2(vector_map, ""));
  220. db_CatValArray_init(&cvarr);
  221. if (!(Fi = Vect_get_field(&Map, field)))
  222. G_fatal_error(_("Database connection not defined for layer %d"),
  223. field);
  224. if (!
  225. (Driver =
  226. db_start_driver_open_database(Fi->driver, Fi->database)))
  227. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  228. Fi->database, Fi->driver);
  229. /* get number of records in label_column */
  230. if ((nrec =
  231. db_select_CatValArray(Driver, Fi->table, Fi->key,
  232. attr_column, NULL, &cvarr)) == -1)
  233. G_fatal_error(_("Unknown column <%s> in table <%s>"),
  234. attr_column, Fi->table);
  235. if (nrec < 0)
  236. G_fatal_error(_("No records selected from table <%s>"),
  237. Fi->table);
  238. G_debug(3, "nrec = %d", nrec);
  239. my_labels_rules =
  240. (struct My_labels_rule *)
  241. G_malloc(sizeof(struct My_labels_rule) * nrec);
  242. /* get column type */
  243. if (!label_column) {
  244. G_warning(_("Label column was not specified, no labels will be written"));
  245. break;
  246. }
  247. else {
  248. if ((col_type =
  249. db_column_Ctype(Driver, Fi->table,
  250. label_column)) == -1) {
  251. G_fatal_error(_("Column <%s> not found"), label_column);
  252. }
  253. }
  254. /* for each attribute */
  255. for (i = 0; i < cvarr.n_values; i++) {
  256. char tmp[64];
  257. dbValue value;
  258. int cat = cvarr.value[i].cat;
  259. if (db_select_value
  260. (Driver, Fi->table, Fi->key, cat, label_column,
  261. &value) < 0) {
  262. G_warning(_("No records selected"));
  263. continue;
  264. }
  265. labels_n_values++;
  266. db_init_string(&my_labels_rules[i].label);
  267. /* switch the column type */
  268. switch (col_type) {
  269. case DB_C_TYPE_DOUBLE:
  270. sprintf(tmp, "%lf", db_get_value_double(&value));
  271. db_set_string(&my_labels_rules[i].label, tmp);
  272. break;
  273. case DB_C_TYPE_INT:
  274. sprintf(tmp, "%d", db_get_value_int(&value));
  275. db_set_string(&my_labels_rules[i].label, tmp);
  276. break;
  277. case DB_C_TYPE_STRING:
  278. db_set_string(&my_labels_rules[i].label,
  279. db_get_value_string(&value));
  280. break;
  281. default:
  282. G_warning(_("Column type (%s) not supported"),
  283. db_sqltype_name(col_type));
  284. }
  285. /* add the raster category to label */
  286. if (is_fp)
  287. my_labels_rules[i].d = cvarr.value[i].val.d;
  288. else
  289. my_labels_rules[i].i = cvarr.value[i].val.i;
  290. } /* for each value in database */
  291. /* close the database driver */
  292. db_close_database_shutdown_driver(Driver);
  293. /* set the color rules: for each rule */
  294. if (is_fp) {
  295. /* add label */
  296. for (i = 0; i < labels_n_values - 1; i++)
  297. G_set_raster_cat(&my_labels_rules[i].d,
  298. &my_labels_rules[i + 1].d,
  299. db_get_string(&my_labels_rules[i].label),
  300. &rast_cats, DCELL_TYPE);
  301. }
  302. else {
  303. for (i = 0; i < labels_n_values; i++)
  304. G_set_cat(my_labels_rules[i].i,
  305. db_get_string(&my_labels_rules[i].label),
  306. &rast_cats);
  307. }
  308. }
  309. break;
  310. case USE_VAL:
  311. {
  312. char msg[64];
  313. RASTER_MAP_TYPE map_type;
  314. struct FPRange fprange;
  315. struct Range range;
  316. map_type = G_raster_map_type(rast_name, G_mapset());
  317. G_set_raster_cats_title("Values", &rast_cats);
  318. if (map_type == CELL_TYPE) {
  319. CELL min, max;
  320. G_read_range(rast_name, G_mapset(), &range);
  321. G_get_range_min_max(&range, &min, &max);
  322. sprintf(msg, "Value %d", val);
  323. G_set_raster_cat(&min, &max, msg, &rast_cats, map_type);
  324. }
  325. else {
  326. DCELL fmin, fmax;
  327. G_read_fp_range(rast_name, G_mapset(), &fprange);
  328. G_get_fp_range_min_max(&fprange, &fmin, &fmax);
  329. sprintf(msg, "Value %.4f", (double)val);
  330. G_set_raster_cat(&fmin, &fmax, msg, &rast_cats, map_type);
  331. }
  332. }
  333. break;
  334. case USE_CAT:
  335. {
  336. int row, rows;
  337. void *rowbuf;
  338. struct Cell_stats stats;
  339. CELL n;
  340. RASTER_MAP_TYPE map_type;
  341. char *mapset;
  342. long count;
  343. mapset = G_mapset();
  344. if (!(fd = G_open_cell_old(rast_name, mapset)))
  345. G_fatal_error(_("Unable to open raster map <%s>"), rast_name);
  346. map_type = G_raster_map_type(rast_name, mapset);
  347. if (!(rowbuf = G_allocate_raster_buf(map_type)))
  348. G_fatal_error(_("Cannot allocate memory for row buffer"));
  349. G_init_cell_stats(&stats);
  350. G_set_raster_cats_title("Categories", &rast_cats);
  351. rows = G_window_rows();
  352. for (row = 0; row < rows; row++) {
  353. if (G_get_raster_row(fd, rowbuf, row, map_type) < 0)
  354. G_fatal_error(_("Unable to read raster map <%s> row %d"),
  355. rast_name, row);
  356. G_update_cell_stats(rowbuf, G_window_cols(), &stats);
  357. }
  358. G_rewind_cell_stats(&stats);
  359. while (G_next_cell_stat(&n, &count, &stats)) {
  360. char msg[80];
  361. sprintf(msg, "Category %d", n);
  362. G_set_raster_cat(&n, &n, msg, &rast_cats, map_type);
  363. }
  364. G_free(rowbuf);
  365. }
  366. break;
  367. case USE_D:
  368. {
  369. DCELL fmin, fmax;
  370. RASTER_MAP_TYPE map_type;
  371. char *mapset;
  372. int i;
  373. char msg[64];
  374. mapset = G_mapset();
  375. map_type = G_raster_map_type(rast_name, mapset);
  376. G_set_raster_cats_title("Degrees", &rast_cats);
  377. for (i = 1; i <= 360; i++) {
  378. sprintf(msg, "%d degrees", i);
  379. if (i == 360) {
  380. fmin = 359.5;
  381. fmax = 360.0;
  382. G_set_raster_cat(&fmin, &fmax, msg, &rast_cats, map_type);
  383. fmin = 0.0;
  384. fmax = 0.5;
  385. }
  386. else {
  387. fmin = i - 0.5;
  388. fmax = i + 0.5;
  389. }
  390. G_set_raster_cat(&fmin, &fmax, msg, &rast_cats, map_type);
  391. }
  392. }
  393. break;
  394. case USE_Z:
  395. /* TODO or not TODO */
  396. break;
  397. default:
  398. G_fatal_error(_("Unknown use type: %d"), use);
  399. break;
  400. }
  401. G_close_cell(fd);
  402. if (G_write_cats(rast_name, &rast_cats) <= 0)
  403. G_warning(_("Unable to write categories for raster map <%s>"),
  404. rast_name);
  405. G_free_cats(&rast_cats);
  406. return 1;
  407. }