main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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_CN);
  101. color_opt->label = _("Feature color");
  102. color_opt->guisection = _("Colors");
  103. fcolor_opt = G_define_standard_option(G_OPT_CN);
  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);
  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_CN);
  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_CN);
  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. D_open_driver();
  261. G_get_set_window(&window);
  262. /* Check min/max region */
  263. reg = ((window.east - window.west) + (window.north - window.south)) / 2;
  264. if (minreg_opt->answer) {
  265. minreg = atof(minreg_opt->answer);
  266. if (reg < minreg) {
  267. G_important_message(_("Region size is lower than minreg, nothing displayed"));
  268. exit(EXIT_SUCCESS);
  269. }
  270. }
  271. if (maxreg_opt->answer) {
  272. maxreg = atof(maxreg_opt->answer);
  273. if (reg > maxreg) {
  274. G_important_message(_("Region size is greater than maxreg, nothing displayed"));
  275. exit(EXIT_SUCCESS);
  276. }
  277. }
  278. strcpy(map_name, map_opt->answer);
  279. default_width = atoi(width_opt->answer);
  280. if (default_width < 0)
  281. default_width = 0;
  282. width_scale = atof(wscale_opt->answer);
  283. if (cats_acolors_flag->answer && rgbcol_opt->answer) {
  284. G_warning(_("The -%c flag and <%s> option cannot be used together, "
  285. "the -%c flag will be ignored!"),
  286. cats_acolors_flag->key, rgbcol_opt->key, cats_acolors_flag->key);
  287. cats_acolors_flag->answer = FALSE;
  288. }
  289. color = G_standard_color_rgb(WHITE);
  290. has_color = option_to_color(&color, color_opt->answer);
  291. fcolor = G_standard_color_rgb(WHITE);
  292. has_fcolor = option_to_color(&fcolor, fcolor_opt->answer);
  293. size = atof(size_opt->answer);
  294. /* if where_opt was specified select categories from db
  295. * otherwise parse cat_opt */
  296. Clist = Vect_new_cat_list();
  297. Clist->field = atoi(field_opt->answer);
  298. /* open vector */
  299. level = Vect_open_old2(&Map, map_name, "", field_opt->answer);
  300. chcat = 0;
  301. if (where_opt->answer) {
  302. if (Clist->field < 1)
  303. G_fatal_error(_("Option <%s> must be > 0"), field_opt->key);
  304. chcat = 1;
  305. option_to_where(&Map, Clist, where_opt->answer);
  306. }
  307. else if (cat_opt->answer) {
  308. if (Clist->field < 1 && !id_flag->answer)
  309. G_fatal_error(_("Option <%s> must be > 0"), field_opt->key);
  310. chcat = 1;
  311. ret = Vect_str_to_cat_list(cat_opt->answer, Clist);
  312. if (ret > 0)
  313. G_warning(n_("%d error in cat option", "%d errors in cat option", ret), ret);
  314. }
  315. type = Vect_option_to_types(type_opt);
  316. display = option_to_display(display_opt);
  317. /* labels */
  318. options_to_lattr(&lattr, lfield_opt->answer,
  319. lcolor_opt->answer, bgcolor_opt->answer, bcolor_opt->answer,
  320. atoi(lsize_opt->answer), font_opt->answer, enc_opt->answer,
  321. xref_opt->answer, yref_opt->answer);
  322. D_setup(0);
  323. D_set_reduction(1.0);
  324. G_verbose_message(_("Plotting..."));
  325. if (level >= 2)
  326. Vect_get_map_box(&Map, &box);
  327. if (level >= 2 && (window.north < box.S || window.south > box.N ||
  328. window.east < box.W ||
  329. window.west > G_adjust_easting(box.E, &window))) {
  330. G_warning(_("The bounding box of the map is outside the current region, "
  331. "nothing drawn"));
  332. }
  333. else {
  334. overlap = G_window_percentage_overlap(&window, box.N, box.S,
  335. box.E, box.W);
  336. G_debug(1, "overlap = %f \n", overlap);
  337. if (overlap < 1)
  338. Vect_set_constraint_region(&Map, window.north, window.south,
  339. window.east, window.west,
  340. PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);
  341. /* default line width */
  342. if (!wcolumn_opt->answer)
  343. D_line_width(default_width);
  344. if (display & DISP_SHAPE) {
  345. stat += display_shape(&Map, type, Clist, &window,
  346. has_color ? &color : NULL, has_fcolor ? &fcolor : NULL, chcat,
  347. icon_opt->answer, size, sizecolumn_opt->answer,
  348. sqrt_flag->answer ? TRUE : FALSE, rotcolumn_opt->answer,
  349. id_flag->answer ? TRUE : FALSE,
  350. cats_acolors_flag->answer ? TRUE : FALSE, rgbcol_opt->answer,
  351. default_width, wcolumn_opt->answer, width_scale,
  352. zcol_opt->answer);
  353. if (wcolumn_opt->answer)
  354. D_line_width(default_width);
  355. }
  356. if (has_color) {
  357. D_RGB_color(color.r, color.g, color.b);
  358. if (display & DISP_DIR)
  359. stat += display_dir(&Map, type, Clist, chcat, size);
  360. }
  361. /* reset line width: Do we need to get line width from display
  362. * driver (not implemented)? It will help restore previous line
  363. * width (not just 0) determined by another module (e.g.,
  364. * d.linewidth). */
  365. if (!wcolumn_opt->answer)
  366. D_line_width(0);
  367. if (display & DISP_CAT)
  368. stat += display_label(&Map, type, Clist, &lattr, chcat);
  369. if (attrcol_opt->answer)
  370. stat += display_attr(&Map, type, attrcol_opt->answer, Clist, &lattr, chcat);
  371. if (display & DISP_ZCOOR)
  372. stat += display_zcoor(&Map, type, &lattr);
  373. if (display & DISP_VERT)
  374. stat += display_vert(&Map, type, &lattr, size);
  375. if (display & DISP_TOPO)
  376. stat += display_topo(&Map, type, &lattr, size);
  377. }
  378. D_save_command(G_recreate_command());
  379. D_close_driver();
  380. Vect_close(&Map);
  381. Vect_destroy_cat_list(Clist);
  382. if (stat != 0) {
  383. G_fatal_error(_("Rendering failed"));
  384. }
  385. G_done_msg(" ");
  386. exit(EXIT_SUCCESS);
  387. }
  388. int cmp(const void *a, const void *b)
  389. {
  390. return (strcmp(*(char **)a, *(char **)b));
  391. }
  392. /* adopted from r.colors */
  393. char *icon_files(void)
  394. {
  395. char **list, *ret;
  396. char buf[GNAME_MAX], path[GPATH_MAX], path_i[GPATH_MAX];
  397. int i, count;
  398. size_t len;
  399. DIR *dir, *dir_i;
  400. struct dirent *d, *d_i;
  401. list = NULL;
  402. len = 0;
  403. sprintf(path, "%s/etc/symbol", G_gisbase());
  404. dir = opendir(path);
  405. if (!dir)
  406. return NULL;
  407. count = 0;
  408. /* loop over etc/symbol */
  409. while ((d = readdir(dir))) {
  410. if (d->d_name[0] == '.')
  411. continue;
  412. sprintf(path_i, "%s/etc/symbol/%s", G_gisbase(), d->d_name);
  413. dir_i = opendir(path_i);
  414. if (!dir_i)
  415. continue;
  416. /* loop over each directory in etc/symbols */
  417. while ((d_i = readdir(dir_i))) {
  418. if (d_i->d_name[0] == '.')
  419. continue;
  420. list = G_realloc(list, (count + 1) * sizeof(char *));
  421. sprintf(buf, "%s/%s", d->d_name, d_i->d_name);
  422. list[count++] = G_store(buf);
  423. len += strlen(d->d_name) + strlen(d_i->d_name) + 2; /* '/' + ',' */
  424. }
  425. closedir(dir_i);
  426. }
  427. closedir(dir);
  428. qsort(list, count, sizeof(char *), cmp);
  429. if (len > 0) {
  430. ret = G_malloc((len + 1) * sizeof(char)); /* \0 */
  431. *ret = '\0';
  432. for (i = 0; i < count; i++) {
  433. if (i > 0)
  434. strcat(ret, ",");
  435. strcat(ret, list[i]);
  436. G_free(list[i]);
  437. }
  438. G_free(list);
  439. }
  440. else {
  441. ret = G_store("");
  442. }
  443. return ret;
  444. }