plot1.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /* plot1() - Level One vector reading */
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/raster.h>
  5. #include <grass/vector.h>
  6. #include <grass/display.h>
  7. #include "plot.h"
  8. #include "local_proto.h"
  9. #include <grass/symbol.h>
  10. #include <grass/glocale.h>
  11. #include <grass/dbmi.h>
  12. #define RENDER_POLYLINE 0
  13. #define RENDER_POLYGON 1
  14. int palette_ncolors = 16;
  15. struct rgb_color palette[16] = {
  16. {198, 198, 198}, /* 1: light gray */
  17. {127, 127, 127}, /* 2: medium/dark gray */
  18. {255, 0, 0}, /* 3: bright red */
  19. {139, 0, 0}, /* 4: dark red */
  20. {0, 255, 0}, /* 5: bright green */
  21. {0, 139, 0}, /* 6: dark green */
  22. {0, 0, 255}, /* 7: bright blue */
  23. {0, 0, 139}, /* 8: dark blue */
  24. {255, 255, 0}, /* 9: yellow */
  25. {139, 126, 10}, /* 10: olivey brown */
  26. {255, 165, 0}, /* 11: orange */
  27. {255, 192, 203}, /* 12: pink */
  28. {255, 0, 255}, /* 13: magenta */
  29. {139, 0, 139}, /* 14: dark magenta */
  30. {0, 255, 255}, /* 15: cyan */
  31. {0, 139, 139} /* 16: dark cyan */
  32. };
  33. /* *************************************************************** */
  34. /* *************************************************************** */
  35. /* *************************************************************** */
  36. int plot1(struct Map_info *Map, int type, int area, struct cat_list *Clist,
  37. const struct color_rgb *color, const struct color_rgb *fcolor,
  38. int chcat, char *symbol_name, double size, char *size_column,
  39. char *rot_column, int id_flag, int table_colors_flag,
  40. int cats_color_flag, char *rgb_column, int default_width,
  41. char *width_column, double width_scale, int z_color_flag,
  42. char *style)
  43. {
  44. int i, ltype, nlines = 0, line, cat = -1;
  45. double *x, *y;
  46. struct line_pnts *Points, *PPoints;
  47. struct line_cats *Cats;
  48. double x0, y0;
  49. struct field_info *fi = NULL;
  50. dbDriver *driver = NULL;
  51. dbCatValArray cvarr_rgb, cvarr_width, cvarr_size, cvarr_rot;
  52. dbCatVal *cv_rgb = NULL, *cv_width = NULL, *cv_size = NULL, *cv_rot = NULL;
  53. int nrec_rgb = 0, nrec_width = 0, nrec_size = 0, nrec_rot = 0;
  54. int nerror_rgb;
  55. int n_points, n_lines, n_centroids, n_boundaries, n_faces;
  56. int open_db;
  57. int custom_rgb = FALSE;
  58. char colorstring[12]; /* RRR:GGG:BBB */
  59. int red, grn, blu;
  60. RGBA_Color *line_color, *fill_color, *primary_color;
  61. unsigned char which;
  62. int width;
  63. SYMBOL *Symb = NULL;
  64. double var_size, rotation;
  65. var_size = size;
  66. rotation = 0.0;
  67. nerror_rgb = 0;
  68. line_color = G_malloc(sizeof(RGBA_Color));
  69. fill_color = G_malloc(sizeof(RGBA_Color));
  70. primary_color = G_malloc(sizeof(RGBA_Color));
  71. primary_color->a = RGBA_COLOR_OPAQUE;
  72. /* change function prototype to pass RGBA_Color instead of color_rgb? */
  73. if (color) {
  74. line_color->r = color->r;
  75. line_color->g = color->g;
  76. line_color->b = color->b;
  77. line_color->a = RGBA_COLOR_OPAQUE;
  78. }
  79. else
  80. line_color->a = RGBA_COLOR_NONE;
  81. if (fcolor) {
  82. fill_color->r = fcolor->r;
  83. fill_color->g = fcolor->g;
  84. fill_color->b = fcolor->b;
  85. fill_color->a = RGBA_COLOR_OPAQUE;
  86. }
  87. else
  88. fill_color->a = RGBA_COLOR_NONE;
  89. Points = Vect_new_line_struct();
  90. PPoints = Vect_new_line_struct();
  91. Cats = Vect_new_cats_struct();
  92. open_db = table_colors_flag || width_column || size_column || rot_column;
  93. if (open_db) {
  94. fi = Vect_get_field(Map, (Clist->field > 0 ? Clist->field : 1));
  95. if (fi == NULL) {
  96. G_fatal_error(_("Database connection not defined for layer %d"),
  97. (Clist->field > 0 ? Clist->field : 1));
  98. }
  99. driver = db_start_driver_open_database(fi->driver, fi->database);
  100. if (driver == NULL)
  101. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  102. fi->database, fi->driver);
  103. }
  104. if (table_colors_flag) {
  105. /* for reading RRR:GGG:BBB color strings from table */
  106. if (rgb_column == NULL || *rgb_column == '\0')
  107. G_fatal_error(_("Color definition column not specified"));
  108. db_CatValArray_init(&cvarr_rgb);
  109. nrec_rgb = db_select_CatValArray(driver, fi->table, fi->key,
  110. rgb_column, NULL, &cvarr_rgb);
  111. G_debug(3, "nrec_rgb (%s) = %d", rgb_column, nrec_rgb);
  112. if (cvarr_rgb.ctype != DB_C_TYPE_STRING)
  113. G_fatal_error(_("Color definition column (%s) not a string. "
  114. "Column must be of form RRR:GGG:BBB where RGB values range 0-255."),
  115. rgb_column);
  116. if (nrec_rgb < 0)
  117. G_fatal_error(_("Cannot select data (%s) from table"),
  118. rgb_column);
  119. G_debug(2, "\n%d records selected from table", nrec_rgb);
  120. for (i = 0; i < cvarr_rgb.n_values; i++) {
  121. G_debug(4, "cat = %d %s = %s", cvarr_rgb.value[i].cat,
  122. rgb_column, db_get_string(cvarr_rgb.value[i].val.s));
  123. }
  124. }
  125. if (width_column) {
  126. if (*width_column == '\0')
  127. G_fatal_error(_("Line width column not specified."));
  128. db_CatValArray_init(&cvarr_width);
  129. nrec_width = db_select_CatValArray(driver, fi->table, fi->key,
  130. width_column, NULL, &cvarr_width);
  131. G_debug(3, "nrec_width (%s) = %d", width_column, nrec_width);
  132. if (cvarr_width.ctype != DB_C_TYPE_INT &&
  133. cvarr_width.ctype != DB_C_TYPE_DOUBLE)
  134. G_fatal_error(_("Line width column (%s) is not numeric."),
  135. width_column);
  136. if (nrec_width < 0)
  137. G_fatal_error(_("Cannot select data (%s) from table"),
  138. width_column);
  139. G_debug(2, " %d records selected from table", nrec_width);
  140. for (i = 0; i < cvarr_width.n_values; i++) {
  141. G_debug(4, "(width) cat = %d %s = %d", cvarr_width.value[i].cat,
  142. width_column,
  143. (cvarr_width.ctype ==
  144. DB_C_TYPE_INT ? cvarr_width.value[i].val.
  145. i : (int)cvarr_width.value[i].val.d));
  146. }
  147. }
  148. if (size_column) {
  149. if (*size_column == '\0')
  150. G_fatal_error(_("Symbol size column not specified."));
  151. db_CatValArray_init(&cvarr_size);
  152. nrec_size = db_select_CatValArray(driver, fi->table, fi->key,
  153. size_column, NULL, &cvarr_size);
  154. G_debug(3, "nrec_size (%s) = %d", size_column, nrec_size);
  155. if (cvarr_size.ctype != DB_C_TYPE_INT &&
  156. cvarr_size.ctype != DB_C_TYPE_DOUBLE)
  157. G_fatal_error(_("Symbol size column (%s) is not numeric."),
  158. size_column);
  159. if (nrec_size < 0)
  160. G_fatal_error(_("Cannot select data (%s) from table"),
  161. size_column);
  162. G_debug(2, " %d records selected from table", nrec_size);
  163. for (i = 0; i < cvarr_size.n_values; i++) {
  164. G_debug(4, "(size) cat = %d %s = %.2f", cvarr_size.value[i].cat,
  165. size_column,
  166. (cvarr_size.ctype ==
  167. DB_C_TYPE_INT ? (double)cvarr_size.value[i].val.i
  168. : cvarr_size.value[i].val.d));
  169. }
  170. }
  171. if (rot_column) {
  172. if (*rot_column == '\0')
  173. G_fatal_error(_("Symbol rotation column not specified."));
  174. db_CatValArray_init(&cvarr_rot);
  175. nrec_rot = db_select_CatValArray(driver, fi->table, fi->key,
  176. rot_column, NULL, &cvarr_rot);
  177. G_debug(3, "nrec_rot (%s) = %d", rot_column, nrec_rot);
  178. if (cvarr_rot.ctype != DB_C_TYPE_INT &&
  179. cvarr_rot.ctype != DB_C_TYPE_DOUBLE)
  180. G_fatal_error(_("Symbol rotation column (%s) is not numeric."),
  181. rot_column);
  182. if (nrec_rot < 0)
  183. G_fatal_error(_("Cannot select data (%s) from table"),
  184. rot_column);
  185. G_debug(2, " %d records selected from table", nrec_rot);
  186. for (i = 0; i < cvarr_rot.n_values; i++) {
  187. G_debug(4, "(rot) cat = %d %s = %.2f", cvarr_rot.value[i].cat,
  188. rot_column,
  189. (cvarr_rot.ctype ==
  190. DB_C_TYPE_INT ? (double)cvarr_rot.value[i].val.i
  191. : cvarr_rot.value[i].val.d));
  192. }
  193. }
  194. if( !(nrec_size || nrec_rot) ) {
  195. Symb = S_read(symbol_name);
  196. if (Symb == NULL)
  197. G_warning(_("Unable to read symbol, unable to display points"));
  198. else
  199. S_stroke(Symb, size, 0.0, 0);
  200. }
  201. if (open_db)
  202. db_close_database_shutdown_driver(driver);
  203. Vect_rewind(Map);
  204. /* Is it necessary to reset line/label color in each loop ? */
  205. if (color && !table_colors_flag && !cats_color_flag)
  206. D_RGB_color(color->r, color->g, color->b);
  207. if (Vect_level(Map) >= 2)
  208. nlines = Vect_get_num_lines(Map);
  209. line = 0;
  210. n_points = n_lines = 0;
  211. n_centroids = n_boundaries = 0;
  212. n_faces = 0;
  213. while (1) {
  214. line++;
  215. if (Vect_level(Map) >= 2) {
  216. if (line > nlines)
  217. break;
  218. if (!Vect_line_alive(Map, line))
  219. continue;
  220. ltype = Vect_read_line(Map, Points, Cats, line);
  221. }
  222. else {
  223. ltype = Vect_read_next_line(Map, Points, Cats);
  224. if (ltype == -1) {
  225. G_fatal_error(_("Unable to read vector map"));
  226. }
  227. else if (ltype == -2) { /* EOF */
  228. break;
  229. }
  230. }
  231. if (!(type & ltype))
  232. continue;
  233. if (Points->n_points == 0)
  234. continue;
  235. if (chcat) {
  236. int found = 0;
  237. if (id_flag) { /* use line id */
  238. if (!(Vect_cat_in_cat_list(line, Clist)))
  239. continue;
  240. }
  241. else {
  242. for (i = 0; i < Cats->n_cats; i++) {
  243. if (Cats->field[i] == Clist->field &&
  244. Vect_cat_in_cat_list(Cats->cat[i], Clist)) {
  245. found = 1;
  246. break;
  247. }
  248. }
  249. if (!found)
  250. continue;
  251. }
  252. }
  253. else if (Clist->field > 0) {
  254. int found = 0;
  255. for (i = 0; i < Cats->n_cats; i++) {
  256. if (Cats->field[i] == Clist->field) {
  257. found = 1;
  258. break;
  259. }
  260. }
  261. /* lines with no category will be displayed */
  262. if (Cats->n_cats > 0 && !found)
  263. continue;
  264. }
  265. /* z height colors */
  266. if (z_color_flag && Vect_is_3d(Map)) {
  267. struct bound_box box;
  268. double zval;
  269. struct Colors colors;
  270. Vect_get_map_box(Map, &box);
  271. zval = Points->z[0];
  272. G_debug(3, "display line %d, cat %d, x: %f, y: %f, z: %f", line,
  273. cat, Points->x[0], Points->y[0], Points->z[0]);
  274. custom_rgb = TRUE;
  275. Rast_make_fp_colors(&colors, style, box.B, box.T);
  276. Rast_get_color(&zval, &red, &grn, &blu, &colors, DCELL_TYPE);
  277. G_debug(3, "b %d, g: %d, r %d", blu, grn, red);
  278. }
  279. if (table_colors_flag) {
  280. /* only first category */
  281. Vect_cat_get(Cats,
  282. (Clist->field > 0 ? Clist->field :
  283. (Cats->n_cats >
  284. 0 ? Cats->field[0] : 1)), &cat);
  285. if (cat >= 0) {
  286. G_debug(3, "display element %d, cat %d", line, cat);
  287. /* Read RGB colors from db for current area # */
  288. if (db_CatValArray_get_value(&cvarr_rgb, cat, &cv_rgb) !=
  289. DB_OK) {
  290. custom_rgb = FALSE;
  291. }
  292. else {
  293. sprintf(colorstring, "%s", db_get_string(cv_rgb->val.s));
  294. if (*colorstring != '\0') {
  295. G_debug(3, "element %d: colorstring: %s", line,
  296. colorstring);
  297. if (G_str_to_color(colorstring, &red, &grn, &blu) ==
  298. 1) {
  299. custom_rgb = TRUE;
  300. G_debug(3, "element:%d cat %d r:%d g:%d b:%d",
  301. line, cat, red, grn, blu);
  302. }
  303. else {
  304. custom_rgb = FALSE;
  305. G_important_message(_("Error in color definition column '%s', feature id %d "
  306. "with cat %d: colorstring '%s'"),
  307. rgb_column, line, cat, colorstring);
  308. nerror_rgb++;
  309. }
  310. }
  311. else {
  312. custom_rgb = FALSE;
  313. G_important_message(_("Error in color definition column '%s', feature id %d "
  314. "with cat %d"),
  315. rgb_column, line, cat);
  316. nerror_rgb++;
  317. }
  318. }
  319. } /* end if cat */
  320. else {
  321. custom_rgb = FALSE;
  322. }
  323. } /* end if table_colors_flag */
  324. /* random colors */
  325. if (cats_color_flag) {
  326. custom_rgb = FALSE;
  327. if (Clist->field > 0) {
  328. Vect_cat_get(Cats, Clist->field, &cat);
  329. if (cat >= 0) {
  330. G_debug(3, "display element %d, cat %d", line, cat);
  331. /* fetch color number from category */
  332. which = (cat % palette_ncolors);
  333. G_debug(3, "cat:%d which color:%d r:%d g:%d b:%d", cat,
  334. which, palette[which].R, palette[which].G,
  335. palette[which].B);
  336. custom_rgb = TRUE;
  337. red = palette[which].R;
  338. grn = palette[which].G;
  339. blu = palette[which].B;
  340. }
  341. }
  342. else if (Cats->n_cats > 0) {
  343. /* fetch color number from layer */
  344. which = (Cats->field[0] % palette_ncolors);
  345. G_debug(3, "layer:%d which color:%d r:%d g:%d b:%d",
  346. Cats->field[0], which, palette[which].R,
  347. palette[which].G, palette[which].B);
  348. custom_rgb = TRUE;
  349. red = palette[which].R;
  350. grn = palette[which].G;
  351. blu = palette[which].B;
  352. }
  353. }
  354. if (nrec_width) {
  355. /* only first category */
  356. Vect_cat_get(Cats,
  357. (Clist->field > 0 ? Clist->field :
  358. (Cats->n_cats >
  359. 0 ? Cats->field[0] : 1)), &cat);
  360. if (cat >= 0) {
  361. G_debug(3, "display element %d, cat %d", line, cat);
  362. /* Read line width from db for current area # */
  363. if (db_CatValArray_get_value(&cvarr_width, cat, &cv_width) !=
  364. DB_OK) {
  365. width = default_width;
  366. }
  367. else {
  368. width =
  369. width_scale * (cvarr_width.ctype ==
  370. DB_C_TYPE_INT ? cv_width->val.i
  371. : (int)cv_width->val.d);
  372. if (width < 0) {
  373. G_warning(_("Error in line width column (%s), element %d "
  374. "with cat %d: line width [%d]"),
  375. width_column, line, cat, width);
  376. width = default_width;
  377. }
  378. }
  379. } /* end if cat */
  380. else {
  381. width = default_width;
  382. }
  383. D_line_width(width);
  384. } /* end if nrec_width */
  385. /* enough of the prep work, lets start plotting stuff */
  386. x = Points->x;
  387. y = Points->y;
  388. if ((ltype & GV_POINTS) && (Symb != NULL || (nrec_size || nrec_rot)) ) {
  389. if (!(color || fcolor || custom_rgb))
  390. continue;
  391. x0 = x[0];
  392. y0 = y[0];
  393. /* skip if the point is outside of the display window */
  394. /* xy<0 tests make it go ever-so-slightly faster */
  395. if (x0 > D_get_u_east() || x0 < D_get_u_west() ||
  396. y0 < D_get_u_south() || y0 > D_get_u_north())
  397. continue;
  398. /* dynamic symbol size */
  399. if (nrec_size) {
  400. /* only first category */
  401. Vect_cat_get(Cats,
  402. (Clist->field > 0 ? Clist->field :
  403. (Cats->n_cats > 0 ?
  404. Cats->field[0] : 1)), &cat);
  405. if (cat >= 0) {
  406. G_debug(3, "display element %d, cat %d", line, cat);
  407. /* Read symbol size from db for current symbol # */
  408. if (db_CatValArray_get_value(&cvarr_size, cat, &cv_size) !=
  409. DB_OK) {
  410. var_size = size;
  411. }
  412. else {
  413. var_size = size *
  414. (cvarr_size.ctype == DB_C_TYPE_INT ?
  415. (double)cv_size->val.i : cv_size->val.d);
  416. if (var_size < 0.0) {
  417. G_warning(_("Error in symbol size column (%s), element %d "
  418. "with cat %d: symbol size [%f]"),
  419. size_column, line, cat, var_size);
  420. var_size = size;
  421. }
  422. }
  423. } /* end if cat */
  424. else {
  425. var_size = size;
  426. }
  427. } /* end if nrec_size */
  428. /* dynamic symbol rotation */
  429. if (nrec_rot) {
  430. /* only first category */
  431. Vect_cat_get(Cats,
  432. (Clist->field > 0 ? Clist->field :
  433. (Cats->n_cats > 0 ?
  434. Cats->field[0] : 1)), &cat);
  435. if (cat >= 0) {
  436. G_debug(3, "display element %d, cat %d", line, cat);
  437. /* Read symbol rotation from db for current symbol # */
  438. if (db_CatValArray_get_value(&cvarr_rot, cat, &cv_rot) !=
  439. DB_OK) {
  440. rotation = 0.0;
  441. }
  442. else {
  443. rotation =
  444. (cvarr_rot.ctype == DB_C_TYPE_INT ?
  445. (double)cv_rot->val.i : cv_rot->val.d);
  446. }
  447. } /* end if cat */
  448. else {
  449. rotation = 0.0;
  450. }
  451. } /* end if nrec_rot */
  452. if(nrec_size || nrec_rot) {
  453. G_debug(3, ". dynamic symbol: cat=%d size=%.2f rotation=%.2f",
  454. cat, var_size, rotation);
  455. /* symbol stroking is cumulative, so we need to reread it each time */
  456. if(Symb) /* unclean free() on first iteration if variables are not init'd to NULL? */
  457. G_free(Symb);
  458. Symb = S_read(symbol_name);
  459. if (Symb == NULL)
  460. G_warning(_("Unable to read symbol, unable to display points"));
  461. else
  462. S_stroke(Symb, var_size, rotation, 0);
  463. }
  464. /* use random or RGB column color if given, otherwise reset */
  465. /* centroids always use default color to stand out from underlying area */
  466. if (custom_rgb && (ltype != GV_CENTROID)) {
  467. primary_color->r = (unsigned char)red;
  468. primary_color->g = (unsigned char)grn;
  469. primary_color->b = (unsigned char)blu;
  470. D_symbol2(Symb, x0, y0, primary_color, line_color);
  471. }
  472. else
  473. D_symbol(Symb, x0, y0, line_color, fill_color);
  474. /* reset to defaults */
  475. var_size = size;
  476. rotation = 0.0;
  477. }
  478. else if (color || custom_rgb || (z_color_flag && Vect_is_3d(Map))) {
  479. if (!table_colors_flag && !cats_color_flag && !z_color_flag)
  480. D_RGB_color(color->r, color->g, color->b);
  481. else {
  482. if (custom_rgb)
  483. D_RGB_color((unsigned char)red, (unsigned char)grn,
  484. (unsigned char)blu);
  485. else
  486. D_RGB_color(color->r, color->g, color->b);
  487. }
  488. /* Plot the lines */
  489. if (Points->n_points == 1) /* line with one coor */
  490. D_polydots_abs(x, y, Points->n_points);
  491. else /*use different user defined render methods */
  492. D_polyline_abs(x, y, Points->n_points);
  493. }
  494. switch (ltype) {
  495. case GV_POINT:
  496. n_points++;
  497. break;
  498. case GV_LINE:
  499. n_lines++;
  500. break;
  501. case GV_CENTROID:
  502. n_centroids++;
  503. break;
  504. case GV_BOUNDARY:
  505. n_boundaries++;
  506. break;
  507. case GV_FACE:
  508. n_faces++;
  509. break;
  510. default:
  511. break;
  512. }
  513. }
  514. if (nerror_rgb > 0) {
  515. G_warning(_("Error in color definition column '%s': %d features affected"),
  516. rgb_column, nerror_rgb);
  517. }
  518. if (n_points > 0)
  519. G_verbose_message(_("%d points plotted"), n_points);
  520. if (n_lines > 0)
  521. G_verbose_message(_("%d lines plotted"), n_lines);
  522. if (n_centroids > 0)
  523. G_verbose_message(_("%d centroids plotted"), n_centroids);
  524. if (n_boundaries > 0)
  525. G_verbose_message(_("%d boundaries plotted"), n_boundaries);
  526. if (n_faces > 0)
  527. G_verbose_message(_("%d faces plotted"), n_faces);
  528. Vect_destroy_line_struct(Points);
  529. Vect_destroy_cats_struct(Cats);
  530. return 0; /* not reached */
  531. }