main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: d.vect
  5. * AUTHOR(S): CERL, Radim Blazek, others
  6. * Updated to GRASS7 by Martin Landa <landa.martin gmail.com>
  7. * PURPOSE: Display the vector map in map display
  8. * COPYRIGHT: (C) 2004-2014 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General
  11. * Public License (>=v2). Read the file COPYING that
  12. * comes with GRASS for details.
  13. *
  14. *****************************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #include <dirent.h>
  19. #include <grass/gis.h>
  20. #include <grass/raster.h>
  21. #include <grass/display.h>
  22. #include <grass/dbmi.h>
  23. #include <grass/glocale.h>
  24. #include "plot.h"
  25. #include "local_proto.h"
  26. static int cmp(const void *, const void *);
  27. static char *icon_files(void);
  28. int main(int argc, char **argv)
  29. {
  30. int ret, level;
  31. int stat, type, display;
  32. int chcat;
  33. int has_color, has_fcolor;
  34. struct color_rgb color, fcolor;
  35. double size;
  36. int default_width;
  37. double width_scale;
  38. double minreg, maxreg, reg;
  39. char map_name[GNAME_MAX];
  40. struct GModule *module;
  41. struct Option *map_opt;
  42. struct Option *color_opt, *fcolor_opt, *rgbcol_opt, *zcol_opt;
  43. struct Option *type_opt, *display_opt;
  44. struct Option *icon_opt, *size_opt, *sizecolumn_opt, *rotcolumn_opt;
  45. struct Option *where_opt;
  46. struct Option *field_opt, *cat_opt, *lfield_opt;
  47. struct Option *lcolor_opt, *bgcolor_opt, *bcolor_opt;
  48. struct Option *lsize_opt, *font_opt, *enc_opt, *xref_opt, *yref_opt;
  49. struct Option *attrcol_opt, *maxreg_opt, *minreg_opt;
  50. struct Option *width_opt, *wcolumn_opt, *wscale_opt;
  51. struct Flag *id_flag, *cats_acolors_flag, *sqrt_flag;
  52. char *desc;
  53. struct cat_list *Clist;
  54. LATTR lattr;
  55. struct Map_info Map;
  56. struct Cell_head window;
  57. struct bound_box box;
  58. double overlap;
  59. stat = 0;
  60. /* Initialize the GIS calls */
  61. G_gisinit(argv[0]);
  62. module = G_define_module();
  63. G_add_keyword(_("display"));
  64. G_add_keyword(_("graphics"));
  65. G_add_keyword(_("vector"));
  66. module->description = _("Displays user-specified vector map "
  67. "in the active graphics frame.");
  68. map_opt = G_define_standard_option(G_OPT_V_MAP);
  69. field_opt = G_define_standard_option(G_OPT_V_FIELD_ALL);
  70. field_opt->answer = "1";
  71. field_opt->guisection = _("Selection");
  72. display_opt = G_define_option();
  73. display_opt->key = "display";
  74. display_opt->type = TYPE_STRING;
  75. display_opt->required = YES;
  76. display_opt->multiple = YES;
  77. display_opt->answer = "shape";
  78. display_opt->options = "shape,cat,topo,vert,dir,zcoor";
  79. display_opt->description = _("Display");
  80. desc = NULL;
  81. G_asprintf(&desc,
  82. "shape;%s;cat;%s;topo;%s;vert;%s;dir;%s;zcoor;%s",
  83. _("Display geometry of features"),
  84. _("Display category numbers of features"),
  85. _("Display topology information (nodes, edges)"),
  86. _("Display vertices of features"),
  87. _("Display direction of linear features"),
  88. _("Display z-coordinate of features (only for 3D vector maps)"));
  89. display_opt->descriptions = desc;
  90. /* Query */
  91. type_opt = G_define_standard_option(G_OPT_V_TYPE);
  92. type_opt->answer = "point,line,boundary,area,face";
  93. type_opt->options = "point,line,boundary,centroid,area,face";
  94. type_opt->guisection = _("Selection");
  95. cat_opt = G_define_standard_option(G_OPT_V_CATS);
  96. cat_opt->guisection = _("Selection");
  97. where_opt = G_define_standard_option(G_OPT_DB_WHERE);
  98. where_opt->guisection = _("Selection");
  99. /* Colors */
  100. color_opt = G_define_standard_option(G_OPT_C_FG);
  101. color_opt->label = _("Feature color");
  102. color_opt->guisection = _("Colors");
  103. fcolor_opt = G_define_standard_option(G_OPT_C_BG);
  104. fcolor_opt->key = "fill_color";
  105. fcolor_opt->answer = "200:200:200";
  106. fcolor_opt->label = _("Area fill color");
  107. fcolor_opt->guisection = _("Colors");
  108. rgbcol_opt = G_define_standard_option(G_OPT_DB_COLUMN);
  109. rgbcol_opt->key = "rgb_column";
  110. rgbcol_opt->guisection = _("Colors");
  111. rgbcol_opt->label = _("Colorize features according color definition column");
  112. rgbcol_opt->description = _("Color definition in R:G:B form");
  113. zcol_opt = G_define_standard_option(G_OPT_M_COLR);
  114. zcol_opt->key = "zcolor";
  115. zcol_opt->description = _("Colorize point or area features according to z-coordinate");
  116. zcol_opt->guisection = _("Colors");
  117. /* Lines */
  118. width_opt = G_define_option();
  119. width_opt->key = "width";
  120. width_opt->type = TYPE_INTEGER;
  121. width_opt->answer = "0";
  122. width_opt->guisection = _("Lines");
  123. width_opt->description = _("Line width");
  124. wcolumn_opt = G_define_standard_option(G_OPT_DB_COLUMN);
  125. wcolumn_opt->key = "width_column";
  126. wcolumn_opt->guisection = _("Lines");
  127. wcolumn_opt->label = _("Name of numeric column containing line width");
  128. wcolumn_opt->description = _("These values will be scaled by width_scale");
  129. wscale_opt = G_define_option();
  130. wscale_opt->key = "width_scale";
  131. wscale_opt->type = TYPE_DOUBLE;
  132. wscale_opt->answer = "1";
  133. wscale_opt->guisection = _("Lines");
  134. wscale_opt->description = _("Scale factor for width_column");
  135. /* Symbols */
  136. icon_opt = G_define_option();
  137. icon_opt->key = "icon";
  138. icon_opt->type = TYPE_STRING;
  139. icon_opt->required = NO;
  140. icon_opt->multiple = NO;
  141. icon_opt->guisection = _("Symbols");
  142. icon_opt->answer = "basic/x";
  143. /* This could also use ->gisprompt = "old,symbol,symbol" instead of ->options */
  144. icon_opt->options = icon_files();
  145. icon_opt->description = _("Point and centroid symbol");
  146. size_opt = G_define_option();
  147. size_opt->key = "size";
  148. size_opt->type = TYPE_DOUBLE;
  149. size_opt->answer = "5";
  150. size_opt->guisection = _("Symbols");
  151. size_opt->label = _("Symbol size");
  152. size_opt->description =
  153. _("When used with the size_column option this becomes the scale factor");
  154. sizecolumn_opt = G_define_standard_option(G_OPT_DB_COLUMN);
  155. sizecolumn_opt->key = "size_column";
  156. sizecolumn_opt->guisection = _("Symbols");
  157. sizecolumn_opt->description =
  158. _("Name of numeric column containing symbol size");
  159. rotcolumn_opt = G_define_standard_option(G_OPT_DB_COLUMN);
  160. rotcolumn_opt->key = "rotation_column";
  161. rotcolumn_opt->guisection = _("Symbols");
  162. rotcolumn_opt->label =
  163. _("Name of numeric column containing symbol rotation angle");
  164. rotcolumn_opt->description =
  165. _("Measured in degrees CCW from east");
  166. /* Labels */
  167. lfield_opt = G_define_standard_option(G_OPT_V_FIELD);
  168. lfield_opt->key = "label_layer";
  169. lfield_opt->required = NO;
  170. lfield_opt->guisection = _("Labels");
  171. lfield_opt->label =
  172. _("Layer number for labels (default: the given layer number)");
  173. attrcol_opt = G_define_standard_option(G_OPT_DB_COLUMN);
  174. attrcol_opt->key = "attribute_column";
  175. attrcol_opt->multiple = NO; /* or fix attr.c, around line 102 */
  176. attrcol_opt->guisection = _("Labels");
  177. attrcol_opt->description = _("Name of column to be displayed as a label");
  178. lcolor_opt = G_define_standard_option(G_OPT_C_FG);
  179. lcolor_opt->key = "label_color";
  180. lcolor_opt->answer = "red";
  181. lcolor_opt->label = _("Label color");
  182. lcolor_opt->guisection = _("Labels");
  183. bgcolor_opt = G_define_standard_option(G_OPT_C_BG);
  184. bgcolor_opt->key = "label_bgcolor";
  185. bgcolor_opt->answer = "none";
  186. bgcolor_opt->guisection = _("Labels");
  187. bgcolor_opt->label = _("Label background color");
  188. bcolor_opt = G_define_standard_option(G_OPT_C_BG);
  189. bcolor_opt->key = "label_bcolor";
  190. bcolor_opt->type = TYPE_STRING;
  191. bcolor_opt->answer = "none";
  192. bcolor_opt->guisection = _("Labels");
  193. bcolor_opt->label = _("Label border color");
  194. lsize_opt = G_define_option();
  195. lsize_opt->key = "label_size";
  196. lsize_opt->type = TYPE_INTEGER;
  197. lsize_opt->answer = "8";
  198. lsize_opt->guisection = _("Labels");
  199. lsize_opt->description = _("Label size (pixels)");
  200. font_opt = G_define_option();
  201. font_opt->key = "font";
  202. font_opt->type = TYPE_STRING;
  203. font_opt->guisection = _("Labels");
  204. font_opt->description = _("Font name");
  205. enc_opt = G_define_option();
  206. enc_opt->key = "encoding";
  207. enc_opt->type = TYPE_STRING;
  208. enc_opt->guisection = _("Labels");
  209. enc_opt->description = _("Text encoding");
  210. xref_opt = G_define_option();
  211. xref_opt->key = "xref";
  212. xref_opt->type = TYPE_STRING;
  213. xref_opt->guisection = _("Labels");
  214. xref_opt->answer = "left";
  215. xref_opt->options = "left,center,right";
  216. xref_opt->description = _("Label horizontal justification");
  217. yref_opt = G_define_option();
  218. yref_opt->key = "yref";
  219. yref_opt->type = TYPE_STRING;
  220. yref_opt->guisection = _("Labels");
  221. yref_opt->answer = "center";
  222. yref_opt->options = "top,center,bottom";
  223. yref_opt->description = _("Label vertical justification");
  224. minreg_opt = G_define_option();
  225. minreg_opt->key = "minreg";
  226. minreg_opt->type = TYPE_DOUBLE;
  227. minreg_opt->required = NO;
  228. minreg_opt->description =
  229. _("Minimum region size (average from height and width) "
  230. "when map is displayed");
  231. maxreg_opt = G_define_option();
  232. maxreg_opt->key = "maxreg";
  233. maxreg_opt->type = TYPE_DOUBLE;
  234. maxreg_opt->required = NO;
  235. maxreg_opt->description =
  236. _("Maximum region size (average from height and width) "
  237. "when map is displayed");
  238. /* Colors */
  239. cats_acolors_flag = G_define_flag();
  240. cats_acolors_flag->key = 'c';
  241. cats_acolors_flag->guisection = _("Colors");
  242. cats_acolors_flag->description =
  243. _("Random colors according to category number "
  244. "(or layer number if 'layer=-1' is given)");
  245. /* Query */
  246. id_flag = G_define_flag();
  247. id_flag->key = 'i';
  248. id_flag->guisection = _("Selection");
  249. id_flag->description = _("Use values from 'cats' option as feature id");
  250. sqrt_flag = G_define_flag();
  251. sqrt_flag->key = 'r';
  252. sqrt_flag->label = _("Use square root of the value of size_column");
  253. sqrt_flag->description =
  254. _("This makes circle areas proportionate to the size_column values "
  255. "instead of circle radius");
  256. sqrt_flag->guisection = _("Symbols");
  257. /* Check command line */
  258. if (G_parser(argc, argv))
  259. exit(EXIT_FAILURE);
  260. if (D_open_driver() != 0)
  261. G_fatal_error(_("No graphics device selected. "
  262. "Use d.mon to select graphics device."));
  263. G_get_set_window(&window);
  264. /* Check min/max region */
  265. reg = ((window.east - window.west) + (window.north - window.south)) / 2;
  266. if (minreg_opt->answer) {
  267. minreg = atof(minreg_opt->answer);
  268. if (reg < minreg) {
  269. G_important_message(_("Region size is lower than minreg, nothing displayed"));
  270. exit(EXIT_SUCCESS);
  271. }
  272. }
  273. if (maxreg_opt->answer) {
  274. maxreg = atof(maxreg_opt->answer);
  275. if (reg > maxreg) {
  276. G_important_message(_("Region size is greater than maxreg, nothing displayed"));
  277. exit(EXIT_SUCCESS);
  278. }
  279. }
  280. strcpy(map_name, map_opt->answer);
  281. default_width = atoi(width_opt->answer);
  282. if (default_width < 0)
  283. default_width = 0;
  284. width_scale = atof(wscale_opt->answer);
  285. if (cats_acolors_flag->answer && rgbcol_opt->answer) {
  286. G_warning(_("The -%c flag and <%s> option cannot be used together, "
  287. "the -%c flag will be ignored!"),
  288. cats_acolors_flag->key, rgbcol_opt->key, cats_acolors_flag->key);
  289. cats_acolors_flag->answer = FALSE;
  290. }
  291. color = G_standard_color_rgb(WHITE);
  292. has_color = option_to_color(&color, color_opt->answer);
  293. fcolor = G_standard_color_rgb(WHITE);
  294. has_fcolor = option_to_color(&fcolor, fcolor_opt->answer);
  295. size = atof(size_opt->answer);
  296. /* if where_opt was specified select categories from db
  297. * otherwise parse cat_opt */
  298. Clist = Vect_new_cat_list();
  299. Clist->field = atoi(field_opt->answer);
  300. /* open vector */
  301. level = Vect_open_old2(&Map, map_name, "", field_opt->answer);
  302. chcat = 0;
  303. if (where_opt->answer) {
  304. if (Clist->field < 1)
  305. G_fatal_error(_("Option <%s> must be > 0"), field_opt->key);
  306. chcat = 1;
  307. option_to_where(&Map, Clist, where_opt->answer);
  308. }
  309. else if (cat_opt->answer) {
  310. if (Clist->field < 1 && !id_flag->answer)
  311. G_fatal_error(_("Option <%s> must be > 0"), field_opt->key);
  312. chcat = 1;
  313. ret = Vect_str_to_cat_list(cat_opt->answer, Clist);
  314. if (ret > 0)
  315. G_warning(_n("%d error in cat option", "%d errors in cat option", ret), ret);
  316. }
  317. type = Vect_option_to_types(type_opt);
  318. display = option_to_display(display_opt);
  319. /* labels */
  320. options_to_lattr(&lattr, lfield_opt->answer,
  321. lcolor_opt->answer, bgcolor_opt->answer, bcolor_opt->answer,
  322. atoi(lsize_opt->answer), font_opt->answer, enc_opt->answer,
  323. xref_opt->answer, yref_opt->answer);
  324. D_setup(0);
  325. D_set_reduction(1.0);
  326. G_verbose_message(_("Plotting..."));
  327. if (level >= 2)
  328. Vect_get_map_box(&Map, &box);
  329. if (level >= 2 && (window.north < box.S || window.south > box.N ||
  330. window.east < box.W ||
  331. window.west > G_adjust_easting(box.E, &window))) {
  332. G_warning(_("The bounding box of the map is outside the current region, "
  333. "nothing drawn"));
  334. }
  335. else {
  336. overlap = G_window_percentage_overlap(&window, box.N, box.S,
  337. box.E, box.W);
  338. G_debug(1, "overlap = %f \n", overlap);
  339. if (overlap < 1)
  340. Vect_set_constraint_region(&Map, window.north, window.south,
  341. window.east, window.west,
  342. PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);
  343. /* default line width */
  344. if (!wcolumn_opt->answer)
  345. D_line_width(default_width);
  346. if (display & DISP_SHAPE) {
  347. stat += display_shape(&Map, type, Clist, &window,
  348. has_color ? &color : NULL, has_fcolor ? &fcolor : NULL, chcat,
  349. icon_opt->answer, size, sizecolumn_opt->answer,
  350. sqrt_flag->answer ? TRUE : FALSE, rotcolumn_opt->answer,
  351. id_flag->answer ? TRUE : FALSE,
  352. cats_acolors_flag->answer ? TRUE : FALSE, rgbcol_opt->answer,
  353. default_width, wcolumn_opt->answer, width_scale,
  354. zcol_opt->answer);
  355. if (wcolumn_opt->answer)
  356. D_line_width(default_width);
  357. }
  358. if (has_color) {
  359. D_RGB_color(color.r, color.g, color.b);
  360. if (display & DISP_DIR)
  361. stat += display_dir(&Map, type, Clist, chcat, size);
  362. }
  363. /* reset line width: Do we need to get line width from display
  364. * driver (not implemented)? It will help restore previous line
  365. * width (not just 0) determined by another module (e.g.,
  366. * d.linewidth). */
  367. if (!wcolumn_opt->answer)
  368. D_line_width(0);
  369. if (display & DISP_CAT)
  370. stat += display_label(&Map, type, Clist, &lattr, chcat);
  371. if (attrcol_opt->answer)
  372. stat += display_attr(&Map, type, attrcol_opt->answer, Clist, &lattr, chcat);
  373. if (display & DISP_ZCOOR)
  374. stat += display_zcoor(&Map, type, &lattr);
  375. if (display & DISP_VERT)
  376. stat += display_vert(&Map, type, &lattr, size);
  377. if (display & DISP_TOPO)
  378. stat += display_topo(&Map, type, &lattr, size);
  379. }
  380. D_save_command(G_recreate_command());
  381. D_close_driver();
  382. Vect_close(&Map);
  383. Vect_destroy_cat_list(Clist);
  384. if (stat != 0) {
  385. G_fatal_error(_("Rendering failed"));
  386. }
  387. G_done_msg(" ");
  388. exit(EXIT_SUCCESS);
  389. }
  390. int cmp(const void *a, const void *b)
  391. {
  392. return (strcmp(*(char **)a, *(char **)b));
  393. }
  394. /* adopted from r.colors */
  395. char *icon_files(void)
  396. {
  397. char **list, *ret;
  398. char buf[GNAME_MAX], path[GPATH_MAX], path_i[GPATH_MAX];
  399. int i, count;
  400. size_t len;
  401. DIR *dir, *dir_i;
  402. struct dirent *d, *d_i;
  403. list = NULL;
  404. len = 0;
  405. sprintf(path, "%s/etc/symbol", G_gisbase());
  406. dir = opendir(path);
  407. if (!dir)
  408. return NULL;
  409. count = 0;
  410. /* loop over etc/symbol */
  411. while ((d = readdir(dir))) {
  412. if (d->d_name[0] == '.')
  413. continue;
  414. sprintf(path_i, "%s/etc/symbol/%s", G_gisbase(), d->d_name);
  415. dir_i = opendir(path_i);
  416. if (!dir_i)
  417. continue;
  418. /* loop over each directory in etc/symbols */
  419. while ((d_i = readdir(dir_i))) {
  420. if (d_i->d_name[0] == '.')
  421. continue;
  422. list = G_realloc(list, (count + 1) * sizeof(char *));
  423. sprintf(buf, "%s/%s", d->d_name, d_i->d_name);
  424. list[count++] = G_store(buf);
  425. len += strlen(d->d_name) + strlen(d_i->d_name) + 2; /* '/' + ',' */
  426. }
  427. closedir(dir_i);
  428. }
  429. closedir(dir);
  430. qsort(list, count, sizeof(char *), cmp);
  431. if (len > 0) {
  432. ret = G_malloc((len + 1) * sizeof(char)); /* \0 */
  433. *ret = '\0';
  434. for (i = 0; i < count; i++) {
  435. if (i > 0)
  436. strcat(ret, ",");
  437. strcat(ret, list[i]);
  438. G_free(list[i]);
  439. }
  440. G_free(list);
  441. }
  442. else {
  443. ret = G_store("");
  444. }
  445. return ret;
  446. }