main.c 18 KB

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