main.c 18 KB

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