plot1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /* plot1() - Level One vector reading */
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/Vect.h>
  5. #include <grass/display.h>
  6. #include <grass/raster.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. /*local functions*/
  34. static void local_plot_poly(double *xf, double *yf, int n, int type);
  35. /*global render switch*/
  36. int render;
  37. /* *************************************************************** */
  38. /* function to plot polygons and polylines ***************** */
  39. /* the parameter type switches render mode ***************** */
  40. /* *************************************************************** */
  41. static void local_plot_poly(double *xf, double *yf, int n, int type)
  42. {
  43. static int *xi, *yi;
  44. static int nalloc;
  45. int i;
  46. if (nalloc < n)
  47. {
  48. nalloc = n;
  49. xi = G_realloc(xi, nalloc * sizeof(int));
  50. yi = G_realloc(yi, nalloc * sizeof(int));
  51. }
  52. for (i = 0; i < n; i++)
  53. {
  54. xi[i] = (int) floor(0.5 + D_u_to_d_col(xf[i]));
  55. yi[i] = (int) floor(0.5 + D_u_to_d_row(yf[i]));
  56. }
  57. if (type == RENDER_POLYGON)
  58. R_polygon_abs(xi, yi, n);
  59. else
  60. R_polyline_abs(xi, yi, n);
  61. }
  62. /* *************************************************************** */
  63. /* function to use different render methods for polylines ******** */
  64. /* *************************************************************** */
  65. void plot_polyline(double *xf, double *yf, int n)
  66. {
  67. int i;
  68. switch (render)
  69. {
  70. case RENDER_DP:
  71. D_polyline(xf, yf, n);
  72. break;
  73. case RENDER_DPC:
  74. D_polyline_clip(xf, yf, n);
  75. break;
  76. case RENDER_DPL:
  77. D_polyline_cull(xf, yf, n);
  78. break;
  79. case RENDER_RPA:
  80. local_plot_poly(xf, yf, n, RENDER_POLYLINE);
  81. break;
  82. case RENDER_GPP:
  83. default:
  84. for (i = 1; i < n; i++)
  85. G_plot_line(xf[i-1], yf[i-1], xf[i], yf[i]);
  86. break;
  87. }
  88. }
  89. /* *************************************************************** */
  90. /* function to use different render methods for polygons ******** */
  91. /* *************************************************************** */
  92. void plot_polygon(double *xf, double *yf, int n)
  93. {
  94. switch (render)
  95. {
  96. case RENDER_GPP:
  97. G_plot_polygon(xf, yf, n);
  98. break;
  99. case RENDER_DP:
  100. D_polygon(xf, yf, n);
  101. break;
  102. case RENDER_DPC:
  103. D_polygon_clip(xf, yf, n);
  104. break;
  105. case RENDER_DPL:
  106. D_polygon_cull(xf, yf, n);
  107. break;
  108. case RENDER_RPA:
  109. local_plot_poly(xf, yf, n, RENDER_POLYGON);
  110. break;
  111. default:
  112. G_plot_polygon(xf, yf, n);
  113. break;
  114. }
  115. }
  116. /* *************************************************************** */
  117. /* *************************************************************** */
  118. /* *************************************************************** */
  119. int plot1 (
  120. struct Map_info *Map, int type, int area, struct cat_list *Clist,
  121. const struct color_rgb *color, const struct color_rgb *fcolor,
  122. int chcat, SYMBOL *Symb, int size, int id_flag,
  123. int table_colors_flag, int cats_color_flag, char *rgb_column,
  124. int default_width, char *width_column, double width_scale, int z_color_flag, char *style)
  125. {
  126. int i, ltype, nlines = 0, line, cat = -1;
  127. double *x, *y;
  128. struct line_pnts *Points, *PPoints;
  129. struct line_cats *Cats;
  130. double msize;
  131. int x0, y0;
  132. struct field_info *fi = NULL;
  133. dbDriver *driver = NULL;
  134. dbCatValArray cvarr_rgb, cvarr_width;
  135. dbCatVal *cv_rgb = NULL, *cv_width = NULL;
  136. int nrec_rgb = 0, nrec_width = 0;
  137. int open_db;
  138. int custom_rgb = FALSE;
  139. char colorstring[12]; /* RRR:GGG:BBB */
  140. int red, grn, blu;
  141. RGBA_Color *line_color, *fill_color, *primary_color;
  142. unsigned char which;
  143. int width;
  144. line_color = G_malloc(sizeof(RGBA_Color));
  145. fill_color = G_malloc(sizeof(RGBA_Color));
  146. primary_color = G_malloc(sizeof(RGBA_Color));
  147. primary_color->a = RGBA_COLOR_OPAQUE;
  148. /* change function prototype to pass RGBA_Color instead of color_rgb? */
  149. if(color) {
  150. line_color->r = color->r;
  151. line_color->g = color->g;
  152. line_color->b = color->b;
  153. line_color->a = RGBA_COLOR_OPAQUE;
  154. }
  155. else
  156. line_color->a = RGBA_COLOR_NONE;
  157. if(fcolor) {
  158. fill_color->r = fcolor->r;
  159. fill_color->g = fcolor->g;
  160. fill_color->b = fcolor->b;
  161. fill_color->a = RGBA_COLOR_OPAQUE;
  162. }
  163. else
  164. fill_color->a = RGBA_COLOR_NONE;
  165. msize = size * ( D_d_to_u_col(2.0) - D_d_to_u_col(1.0) ); /* do it better */
  166. Points = Vect_new_line_struct ();
  167. PPoints = Vect_new_line_struct ();
  168. Cats = Vect_new_cats_struct ();
  169. open_db = table_colors_flag || width_column;
  170. if(open_db){
  171. fi = Vect_get_field (Map, (Clist->field > 0 ? Clist->field : 1));
  172. if (fi == NULL) {
  173. G_fatal_error (_("Cannot read field info"));
  174. }
  175. driver = db_start_driver_open_database(fi->driver, fi->database);
  176. if (driver == NULL)
  177. G_fatal_error (_("Cannot open database %s by driver %s"), fi->database, fi->driver);
  178. }
  179. if( table_colors_flag ) {
  180. /* for reading RRR:GGG:BBB color strings from table */
  181. if ( rgb_column == NULL || *rgb_column == '\0' )
  182. G_fatal_error(_("Color definition column not specified."));
  183. db_CatValArray_init (&cvarr_rgb);
  184. nrec_rgb = db_select_CatValArray(driver, fi->table, fi->key,
  185. rgb_column, NULL, &cvarr_rgb);
  186. G_debug (3, "nrec_rgb (%s) = %d", rgb_column, nrec_rgb);
  187. if (cvarr_rgb.ctype != DB_C_TYPE_STRING)
  188. G_fatal_error (_("Color definition column (%s) not a string. "
  189. "Column must be of form RRR:GGG:BBB where RGB values range 0-255."), rgb_column);
  190. if ( nrec_rgb < 0 )
  191. G_fatal_error (_("Cannot select data (%s) from table"), rgb_column);
  192. G_debug (2, "\n%d records selected from table", nrec_rgb);
  193. for ( i = 0; i < cvarr_rgb.n_values; i++ ) {
  194. G_debug (4, "cat = %d %s = %s", cvarr_rgb.value[i].cat, rgb_column,
  195. db_get_string(cvarr_rgb.value[i].val.s));
  196. }
  197. }
  198. if ( width_column ) {
  199. if ( *width_column == '\0' )
  200. G_fatal_error(_("Line width column not specified."));
  201. db_CatValArray_init (&cvarr_width);
  202. nrec_width = db_select_CatValArray(driver, fi->table, fi->key,
  203. width_column, NULL, &cvarr_width);
  204. G_debug (3, "nrec_width (%s) = %d", width_column, nrec_width);
  205. if (cvarr_width.ctype != DB_C_TYPE_INT && cvarr_width.ctype != DB_C_TYPE_DOUBLE)
  206. G_fatal_error (_("Line width column (%s) not a number."), width_column);
  207. if ( nrec_width < 0 )
  208. G_fatal_error (_("Cannot select data (%s) from table"), width_column);
  209. G_debug (2, "\n%d records selected from table", nrec_width);
  210. for ( i = 0; i < cvarr_width.n_values; i++ ) {
  211. G_debug (4, "cat = %d %s = %d", cvarr_width.value[i].cat, width_column,
  212. (cvarr_width.ctype==DB_C_TYPE_INT?cvarr_width.value[i].val.i:
  213. (int)cvarr_width.value[i].val.d));
  214. }
  215. }
  216. if (open_db)
  217. db_close_database_shutdown_driver(driver);
  218. Vect_rewind ( Map );
  219. /* Is it necessary to reset line/label color in each loop ? */
  220. if ( color && !table_colors_flag && !cats_color_flag)
  221. R_RGB_color(color->r, color->g, color->b);
  222. if ( Vect_level ( Map ) >= 2 )
  223. nlines = Vect_get_num_lines ( Map );
  224. line = 0;
  225. while (1) {
  226. if ( Vect_level ( Map ) >= 2 ) {
  227. line++;
  228. if ( line > nlines ) return 0;
  229. if ( !Vect_line_alive (Map, line) ) continue;
  230. ltype = Vect_read_line (Map, Points, Cats, line);
  231. } else {
  232. ltype = Vect_read_next_line (Map, Points, Cats);
  233. switch ( ltype )
  234. {
  235. case -1:
  236. G_warning(_("Unable to read vector map"));
  237. return -1;
  238. case -2: /* EOF */
  239. return 0;
  240. }
  241. }
  242. if ( !(type & ltype) ) continue;
  243. if ( Points->n_points == 0 )
  244. continue;
  245. if ( chcat ) {
  246. int found = 0;
  247. if ( id_flag ) { /* use line id */
  248. if ( !(Vect_cat_in_cat_list ( line, Clist)) )
  249. continue;
  250. } else {
  251. for ( i = 0; i < Cats->n_cats; i++ ) {
  252. if ( Cats->field[i] == Clist->field && Vect_cat_in_cat_list ( Cats->cat[i], Clist) ) {
  253. found = 1;
  254. break;
  255. }
  256. }
  257. if (!found) continue;
  258. }
  259. } else
  260. if ( Clist->field > 0 ) {
  261. int found = 0;
  262. for ( i = 0; i < Cats->n_cats; i++ ) {
  263. if ( Cats->field[i] == Clist->field ) {
  264. found = 1;
  265. break;
  266. }
  267. }
  268. /* lines with no category will be displayed */
  269. if (Cats->n_cats > 0 && !found) continue;
  270. }
  271. /* z height colors */
  272. if( z_color_flag && Vect_is_3d (Map)) {
  273. BOUND_BOX box;
  274. double zval;
  275. struct Colors colors;
  276. Vect_get_map_box (Map, &box );
  277. zval = Points->z[0];
  278. G_debug (3, "display line %d, cat %d, x: %f, y: %f, z: %f", line,
  279. cat, Points->x[0], Points->y[0], Points->z[0]);
  280. custom_rgb = TRUE;
  281. G_make_fp_colors(&colors, style, box.B, box.T);
  282. G_get_raster_color(&zval, &red, &grn, &blu, &colors, DCELL_TYPE);
  283. G_debug (3, "b %d, g: %d, r %d", blu, grn, red);
  284. }
  285. if( table_colors_flag ) {
  286. /* only first category */
  287. cat = Vect_get_line_cat ( Map, line,
  288. (Clist->field > 0 ? Clist->field :
  289. (Cats->n_cats > 0 ? Cats->field[0] : 1)));
  290. if (cat >= 0) {
  291. G_debug (3, "display element %d, cat %d", line, cat);
  292. /* Read RGB colors from db for current area # */
  293. if (db_CatValArray_get_value (&cvarr_rgb, cat, &cv_rgb) != DB_OK) {
  294. custom_rgb = FALSE;
  295. }
  296. else {
  297. sprintf (colorstring, "%s", db_get_string(cv_rgb -> val.s));
  298. if (*colorstring != '\0') {
  299. G_debug (3, "element %d: colorstring: %s", line, colorstring);
  300. if ( G_str_to_color(colorstring, &red, &grn, &blu) == 1) {
  301. custom_rgb = TRUE;
  302. G_debug (3, "element:%d cat %d r:%d g:%d b:%d", line, cat, red, grn, blu);
  303. }
  304. else {
  305. custom_rgb = FALSE;
  306. G_warning(_("Error in color definition column (%s), element %d "
  307. "with cat %d: colorstring [%s]"), rgb_column, line, cat, colorstring);
  308. }
  309. }
  310. else {
  311. custom_rgb = FALSE;
  312. G_warning (_("Error in color definition column (%s), element %d with cat %d"),
  313. rgb_column, line, cat);
  314. }
  315. }
  316. } /* end if cat */
  317. else {
  318. custom_rgb = FALSE;
  319. }
  320. } /* end if table_colors_flag */
  321. /* random colors */
  322. if( cats_color_flag ) {
  323. custom_rgb = FALSE;
  324. if(Clist->field > 0){
  325. cat = Vect_get_line_cat ( Map, line, Clist->field );
  326. if( cat >= 0 ) {
  327. G_debug (3, "display element %d, cat %d", line, cat);
  328. /* fetch color number from category */
  329. which = (cat % palette_ncolors);
  330. G_debug (3,"cat:%d which color:%d r:%d g:%d b:%d", cat, which,
  331. palette[which].R, palette[which].G, palette[which].B);
  332. custom_rgb = TRUE;
  333. red = palette[which].R;
  334. grn = palette[which].G;
  335. blu = palette[which].B;
  336. }
  337. } else
  338. 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", Cats->field[0],
  342. which, palette[which].R, palette[which].G, palette[which].B);
  343. custom_rgb = TRUE;
  344. red = palette[which].R;
  345. grn = palette[which].G;
  346. blu = palette[which].B;
  347. }
  348. }
  349. if( nrec_width ) {
  350. /* only first category */
  351. cat = Vect_get_line_cat ( Map, line,
  352. (Clist->field > 0 ? Clist->field :
  353. (Cats->n_cats > 0 ? Cats->field[0] : 1)));
  354. if (cat >= 0) {
  355. G_debug (3, "display element %d, cat %d", line, cat);
  356. /* Read line width from db for current area # */
  357. if (db_CatValArray_get_value (&cvarr_width, cat, &cv_width) != DB_OK) {
  358. width = default_width;
  359. }
  360. else {
  361. width = width_scale * (cvarr_width.ctype==DB_C_TYPE_INT?
  362. cv_width->val.i:(int)cv_width->val.d);
  363. if (width < 0) {
  364. G_warning(_("Error in line width column (%s), element %d "
  365. "with cat %d: line width [%d]"), width_column, line, cat, width);
  366. width = default_width;
  367. }
  368. }
  369. } /* end if cat */
  370. else {
  371. width = default_width;
  372. }
  373. D_line_width(width);
  374. } /* end if nrec_width */
  375. /* enough of the prep work, lets start plotting stuff */
  376. x = Points->x;
  377. y = Points->y;
  378. if ( (ltype & GV_POINTS) && Symb != NULL ) {
  379. if( !(color || fcolor || custom_rgb) )
  380. continue;
  381. G_plot_where_xy(x[0], y[0], &x0, &y0);
  382. /* skip if the point is outside of the display window */
  383. /* xy<0 tests make it go ever-so-slightly faster */
  384. if( x0 < 0 || y0 < 0 ||
  385. x0 > D_get_d_east() || x0 < D_get_d_west() ||
  386. y0 > D_get_d_south() || y0 < D_get_d_north() )
  387. continue;
  388. /* use random or RGB column color if given, otherwise reset */
  389. /* centroids always use default color to stand out from underlying area */
  390. if (custom_rgb && (ltype != GV_CENTROID) ) {
  391. primary_color->r = (unsigned char)red;
  392. primary_color->g = (unsigned char)grn;
  393. primary_color->b = (unsigned char)blu;
  394. D_symbol2(Symb, x0, y0, primary_color, line_color);
  395. }
  396. else
  397. D_symbol(Symb, x0, y0, line_color, fill_color);
  398. } else if (color || custom_rgb || (z_color_flag && Vect_is_3d (Map))) {
  399. if (!table_colors_flag && !cats_color_flag && !z_color_flag)
  400. R_RGB_color(color->r, color->g, color->b);
  401. else {
  402. if (custom_rgb)
  403. R_RGB_color((unsigned char)red, (unsigned char)grn, (unsigned char)blu);
  404. else
  405. R_RGB_color(color->r, color->g, color->b);
  406. }
  407. /* Plot the lines */
  408. if ( Points->n_points == 1 ) /* line with one coor */
  409. D_polydots_clip(x, y, Points->n_points);
  410. else /*use different user defined render methods*/
  411. plot_polyline(x, y, Points->n_points);
  412. }
  413. }
  414. Vect_destroy_line_struct (Points);
  415. Vect_destroy_cats_struct (Cats);
  416. return 0; /* not reached */
  417. }