gvmap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**
  2. * @file
  3. * @brief creates a geographical map highlighting clusters
  4. */
  5. /*************************************************************************
  6. * Copyright (c) 2011 AT&T Intellectual Property
  7. * All rights reserved. This program and the accompanying materials
  8. * are made available under the terms of the Eclipse Public License v1.0
  9. * which accompanies this distribution, and is available at
  10. * https://www.eclipse.org/legal/epl-v10.html
  11. *
  12. * Contributors: Details at https://graphviz.org
  13. *************************************************************************/
  14. #include "config.h"
  15. #include "../tools/openFile.h"
  16. #include <stdbool.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #define STANDALONE
  20. #include <sparse/general.h>
  21. #include <sparse/QuadTree.h>
  22. #include <time.h>
  23. #include <sparse/SparseMatrix.h>
  24. #include <getopt.h>
  25. #include <string.h>
  26. #include "make_map.h"
  27. #include <sfdpgen/spring_electrical.h>
  28. #include <sfdpgen/post_process.h>
  29. #include <neatogen/overlap.h>
  30. #include <sparse/clustering.h>
  31. #include <cgraph/ingraphs.h>
  32. #include <sparse/DotIO.h>
  33. #include <sparse/colorutil.h>
  34. #include <sparse/color_palette.h>
  35. #include <util/startswith.h>
  36. #include <util/unreachable.h>
  37. typedef struct {
  38. char* cmd;
  39. char **infiles;
  40. FILE* outfile;
  41. int dim;
  42. double shore_depth_tol;
  43. int nrandom;
  44. double bbox_margin;
  45. int useClusters;
  46. int clusterMethod;
  47. bool plotedges;
  48. int color_scheme;
  49. double line_width;
  50. char *color_scheme_str;
  51. const char *opacity;
  52. int improve_contiguity_n;
  53. int nart;
  54. bool color_optimize;
  55. int maxcluster;
  56. int nedgep;
  57. const char *line_color;
  58. bool include_OK_points;
  59. int highlight_cluster;
  60. int seed; /* seed used to calculate Fiedler vector */
  61. } params_t;
  62. static const char usestr[] =
  63. " where graphfile must contain node positions, and widths and heights for each node. No overlap between nodes should be present. Acceptable options are: \n\
  64. -a k - average number of artificial points added along the bounding box of the labels. If < 0, a suitable value is selected automatically. (-1)\n\
  65. -b v - polygon line width, with v < 0 for no line. (0)\n\
  66. -c k - polygon color scheme (1)\n\
  67. 0 : no polygons\n\
  68. 1 : pastel (default)\n\
  69. 2 : blue to yellow\n\
  70. 3 : white to red\n\
  71. 4 : light grey to red\n\
  72. 5 : primary colors\n\
  73. 6 : sequential single hue red \n\
  74. 7 : Adam color scheme\n\
  75. 8 : Adam blend\n\
  76. 9 : sequential single hue lighter red \n\
  77. 10 : light grey\n\
  78. -c_opacity=xx - 2-character hex string for opacity of polygons\n\
  79. -C k - generate at most k clusters. (0)\n\
  80. -d s - seed used to calculate Fiedler vector for optimal coloring\n\
  81. -D - use top-level cluster subgraphs to specify clustering\n\
  82. -e - show edges\n\
  83. -g c - bounding box color. If not specified, a bounding box is not drawn.\n\
  84. -h k - number of artificial points added to maintain bridge between endpoints (0)\n\
  85. -highlight=k - only draw cluster k\n\
  86. -k - increase randomness of boundary\n\
  87. -l s - specify label\n\
  88. -m v - bounding box margin. If 0, auto-assigned (0)\n\
  89. -o <file> - put output in <file> (stdout)\n\
  90. -O - do NOT do color assignment optimization that maximizes color difference between neighboring countries\n\
  91. -p k - ignored\n\
  92. -r k - number of random points k used to define sea and lake boundaries. If 0, auto assigned. (0)\n\
  93. -s v - depth of the sea and lake shores in points. If < 0, auto assigned. (0)\n\
  94. -t n - improve contiguity up to n times. (0)\n\
  95. -v - verbose\n\
  96. -z c - polygon line color (black)\n";
  97. /*
  98. -q f - output format (3)\n\
  99. 0 : Mathematica\n\
  100. 1 : PostScript\n\
  101. 2 : country map\n\
  102. 3 : dot format\n\
  103. */
  104. /* e.g.,
  105. 1 [cluster=10, clustercolor="#ff0000"]
  106. 2 [cluster=10]
  107. (and no other nodes are in cluster10)
  108. then since we can only use 1 color for the cluster 10, both 1 and 2 will be colored based on the color of node 2. However if you have
  109. 2 [cluster=10]
  110. 1 [cluster=10, clustercolor="#ff0000"]
  111. then you get both colored red.
  112. */
  113. static void usage(char* cmd, int eval)
  114. {
  115. fprintf(stderr, "Usage: %s <options> graphfile\n", cmd);
  116. fputs (usestr, stderr);
  117. graphviz_exit(eval);
  118. }
  119. #define HLPFX "ighlight="
  120. #define N_HLPFX (sizeof(HLPFX)-1)
  121. static void
  122. init(int argc, char **argv, params_t* pm)
  123. {
  124. char* cmd = argv[0];
  125. int c;
  126. double s;
  127. int v, r;
  128. char stmp[3]; /* two character string plus '\0' */
  129. pm->outfile = NULL;
  130. pm->opacity = NULL;
  131. pm->color_scheme_str = NULL;
  132. pm->nrandom = -1;
  133. pm->dim = 2;
  134. pm->shore_depth_tol = 0;
  135. pm->highlight_cluster = 0;
  136. pm->useClusters = 0;
  137. pm->clusterMethod = CLUSTERING_MODULARITY;
  138. pm->plotedges = false;
  139. pm->color_scheme = COLOR_SCHEME_PASTEL;
  140. pm->line_width = 0;
  141. pm->improve_contiguity_n = 0;
  142. pm->nart = -1;
  143. pm->color_optimize = true;
  144. pm->maxcluster = 0;
  145. pm->nedgep = 0;
  146. pm->cmd = cmd;
  147. pm->infiles = NULL;
  148. pm->line_color = "#000000";
  149. pm->include_OK_points = false;
  150. pm->seed = 123;
  151. pm->bbox_margin = 0;
  152. opterr = 0;
  153. while ((c = getopt(argc, argv, ":evODQko:m:s:r:p:c:C:l:b:g:t:a:h:z:d:?")) != -1) {
  154. switch (c) {
  155. case 'm':
  156. if (sscanf(optarg, "%lf", &s) > 0 && s != 0) {
  157. pm->bbox_margin = s;
  158. } else {
  159. usage(cmd, 1);
  160. }
  161. break;
  162. case 'Q':
  163. pm->clusterMethod = CLUSTERING_MQ;
  164. break;
  165. case 's':
  166. if (sscanf(optarg, "%lf", &s) > 0) {
  167. pm->shore_depth_tol = s;
  168. } else {
  169. usage(cmd,1);
  170. }
  171. break;
  172. case 'h':
  173. if (sscanf(optarg, "%d", &v) > 0) {
  174. pm->nedgep = MAX(0, v);
  175. } else if (startswith(optarg, HLPFX) &&
  176. sscanf(optarg + N_HLPFX, "%d", &v) > 0) {
  177. pm->highlight_cluster = MAX(0, v);
  178. } else {
  179. usage(cmd,1);
  180. }
  181. break;
  182. case 'r':
  183. if (sscanf(optarg, "%d", &r) > 0) {
  184. pm->nrandom = r;
  185. }
  186. break;
  187. case 't':
  188. if (sscanf(optarg, "%d", &r) > 0 && r > 0) {
  189. pm->improve_contiguity_n = r;
  190. }
  191. break;
  192. case 'p': // ignored
  193. break;
  194. case 'k':
  195. pm->include_OK_points = true;
  196. break;
  197. case 'v':
  198. Verbose = 1;
  199. break;
  200. case 'D':
  201. pm->useClusters = 1;
  202. break;
  203. case 'e':
  204. pm->plotedges = true;
  205. break;
  206. case 'o':
  207. pm->outfile = openFile(pm->cmd, optarg, "w");
  208. break;
  209. case 'O':
  210. pm->color_optimize = false;
  211. break;
  212. case 'a':
  213. if (sscanf(optarg, "%d", &r) > 0) {
  214. pm->nart = r;
  215. } else {
  216. usage(cmd,1);
  217. }
  218. break;
  219. case 'c':
  220. if (sscanf(optarg,"_opacity=%2s", stmp) > 0 && strlen(stmp) == 2){
  221. pm->opacity = stmp;
  222. } else if (sscanf(optarg, "%d", &r) > 0 && r >= COLOR_SCHEME_NONE &&
  223. r <= COLOR_SCHEME_GREY) {
  224. pm->color_scheme = r;
  225. } else if (knownColorScheme(optarg)) {
  226. pm->color_scheme = COLOR_SCHEME_NONE;
  227. pm->color_scheme_str = optarg;
  228. } else {
  229. fprintf(stderr,"-c option %s is invalid, must be a valid integer or string\n", optarg);
  230. usage(cmd, 1);
  231. }
  232. break;
  233. case 'd':
  234. if (sscanf(optarg,"%d",&v) <= 0){
  235. usage(cmd,1);
  236. }
  237. else
  238. pm->seed = v;
  239. break;
  240. case 'C':
  241. if (!(sscanf(optarg, "%d", &v) > 0 && v >= 0)) {
  242. usage(cmd,1);
  243. }
  244. else
  245. pm->maxcluster = v;
  246. break;
  247. case 'g':
  248. // ignored
  249. break;
  250. case 'z': {
  251. pm->line_color = optarg;
  252. break;
  253. }
  254. case 'b':
  255. if (sscanf(optarg,"%lf",&s) > 0) {
  256. pm->line_width = s;
  257. } else {
  258. fprintf (stderr, "%s: unexpected argument \"%s\" for -b flag\n", cmd, optarg);
  259. }
  260. break;
  261. case 'l':
  262. // ignored
  263. break;
  264. case ':':
  265. fprintf(stderr, "gvpack: option -%c missing argument - ignored\n", optopt);
  266. break;
  267. case '?':
  268. if (optopt == '\0' || optopt == '?')
  269. usage(cmd, 0);
  270. else {
  271. fprintf(stderr, " option -%c unrecognized\n", optopt);
  272. usage(cmd, 1);
  273. }
  274. break;
  275. default:
  276. UNREACHABLE();
  277. }
  278. }
  279. argv += optind;
  280. argc -= optind;
  281. if (argc)
  282. pm->infiles = argv;
  283. if (!pm->outfile)
  284. pm->outfile = stdout;
  285. }
  286. static int
  287. validateCluster (int n, int* grouping, int clust_num)
  288. {
  289. int i;
  290. for (i = 0; i < n; i++) {
  291. if (grouping[i] == clust_num) return clust_num;
  292. }
  293. fprintf (stderr, "Highlighted cluster %d not found - ignored\n", clust_num);
  294. return 0;
  295. }
  296. /// @return 0 on success
  297. static int makeMap(SparseMatrix graph, int n, double *x, double *width,
  298. int *grouping, char **labels, float *fsz, float *rgb_r,
  299. float *rgb_g, float *rgb_b, params_t *pm, Agraph_t *g) {
  300. int dim = pm->dim;
  301. int i;
  302. SparseMatrix poly_lines, polys, poly_point_map;
  303. int nverts, *polys_groups;
  304. double *x_poly;
  305. SparseMatrix country_graph;
  306. int improve_contiguity_n = pm->improve_contiguity_n;
  307. #ifdef TIME
  308. clock_t cpu;
  309. #endif
  310. int nart0;
  311. int nart, nrandom;
  312. #ifdef TIME
  313. cpu = clock();
  314. #endif
  315. nrandom = pm->nrandom; nart0 = nart = pm->nart;
  316. if (pm->highlight_cluster) {
  317. pm->highlight_cluster = validateCluster (n, grouping, pm->highlight_cluster);
  318. }
  319. if (make_map_from_rectangle_groups(pm->include_OK_points, n, dim, x, width,
  320. grouping, graph, pm->bbox_margin, nrandom,
  321. &nart, pm->nedgep, pm->shore_depth_tol,
  322. &nverts, &x_poly, &poly_lines, &polys,
  323. &polys_groups, &poly_point_map,
  324. &country_graph, pm->highlight_cluster)
  325. != 0) {
  326. return -1;
  327. }
  328. if (Verbose) fprintf(stderr,"nart = %d\n",nart);
  329. /* compute a good color permutation */
  330. if (pm->color_optimize && country_graph && rgb_r && rgb_g && rgb_b)
  331. map_optimal_coloring(pm->seed, country_graph, rgb_r, rgb_g, rgb_b);
  332. else if (pm->color_scheme_str){
  333. map_palette_optimal_coloring(pm->color_scheme_str, country_graph,
  334. &rgb_r, &rgb_g, &rgb_b);
  335. }
  336. #ifdef TIME
  337. fprintf(stderr, "map making time = %f\n",((double) (clock() - cpu)) / CLOCKS_PER_SEC);
  338. #endif
  339. /* now we check to see if all points in the same group are also in the same polygon, if not, the map is not very
  340. contiguous so we move point positions to improve contiguity */
  341. if (graph && improve_contiguity_n) {
  342. for (i = 0; i < improve_contiguity_n; i++){
  343. improve_contiguity(n, dim, grouping, poly_point_map, x, graph);
  344. nart = nart0;
  345. (void)make_map_from_rectangle_groups(pm->include_OK_points,
  346. n, dim, x, width, grouping, graph, pm->bbox_margin, nrandom, &nart, pm->nedgep,
  347. pm->shore_depth_tol, &nverts, &x_poly, &poly_lines,
  348. &polys, &polys_groups, &poly_point_map, &country_graph, pm->highlight_cluster);
  349. }
  350. {
  351. SparseMatrix D = SparseMatrix_get_real_adjacency_matrix_symmetrized(graph);
  352. remove_overlap(dim, D, x, width, 1000, 5000.,
  353. ELSCHEME_NONE, 0, NULL, NULL, true);
  354. SparseMatrix_delete(D);
  355. nart = nart0;
  356. (void)make_map_from_rectangle_groups(pm->include_OK_points,
  357. n, dim, x, width, grouping, graph, pm->bbox_margin, nrandom, &nart, pm->nedgep,
  358. pm->shore_depth_tol, &nverts, &x_poly, &poly_lines,
  359. &polys, &polys_groups, &poly_point_map, &country_graph, pm->highlight_cluster);
  360. }
  361. }
  362. Dot_SetClusterColor(g, rgb_r, rgb_g, rgb_b, grouping);
  363. plot_dot_map(g, n, dim, x, polys, poly_lines, pm->line_width, pm->line_color, x_poly, polys_groups, labels, fsz, rgb_r, rgb_g, rgb_b, pm->opacity,
  364. (pm->plotedges?graph:NULL), pm->outfile);
  365. SparseMatrix_delete(polys);
  366. SparseMatrix_delete(poly_lines);
  367. SparseMatrix_delete(poly_point_map);
  368. free(x_poly);
  369. free(polys_groups);
  370. return 0;
  371. }
  372. /// @return 0 on success
  373. static int mapFromGraph(Agraph_t *g, params_t *pm) {
  374. SparseMatrix graph;
  375. int n;
  376. double* width = NULL;
  377. double* x;
  378. char** labels = NULL;
  379. int* grouping;
  380. float* rgb_r = NULL;
  381. float* rgb_g = NULL;
  382. float* rgb_b = NULL;
  383. float* fsz;
  384. initDotIO(g);
  385. graph = Import_coord_clusters_from_dot(g, pm->maxcluster, pm->dim, &n, &width, &x, &grouping,
  386. &rgb_r, &rgb_g, &rgb_b, &fsz, &labels, pm->color_scheme, pm->clusterMethod, pm->useClusters);
  387. const int rc = makeMap(graph, n, x, width, grouping, labels, fsz, rgb_r,
  388. rgb_g, rgb_b, pm, g);
  389. free(rgb_r);
  390. free(rgb_g);
  391. free(rgb_b);
  392. return rc;
  393. }
  394. int main(int argc, char *argv[])
  395. {
  396. params_t pm;
  397. Agraph_t* g;
  398. Agraph_t* prevg = NULL;
  399. ingraph_state ig;
  400. init(argc, argv, &pm);
  401. newIngraph(&ig, pm.infiles);
  402. while ((g = nextGraph (&ig)) != 0) {
  403. if (prevg) agclose (prevg);
  404. if (mapFromGraph(g, &pm) != 0) {
  405. graphviz_exit(EXIT_FAILURE);
  406. }
  407. prevg = g;
  408. }
  409. graphviz_exit(0);
  410. }
  411. /**
  412. * @dir .
  413. * @brief creates a geographical map highlighting clusters
  414. */