plot1.c 16 KB

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