build.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*!
  2. \file build.c
  3. \brief Vector library - Building topology
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  11. Update to GRASS 5.7 Radim Blazek and David D. Gray.
  12. \date 2001-2008
  13. */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. #include <grass/glocale.h>
  18. #include <grass/gis.h>
  19. #include <grass/Vect.h>
  20. #ifndef HAVE_OGR
  21. static int format()
  22. {
  23. G_fatal_error(_("Requested format is not compiled in this version"));
  24. return 0;
  25. }
  26. #endif
  27. static int (*Build_array[]) () = {
  28. Vect_build_nat
  29. #ifdef HAVE_OGR
  30. , Vect_build_ogr
  31. #else
  32. , format
  33. #endif
  34. };
  35. /*!
  36. \brief Build topology for vector map
  37. \param Map vector map
  38. \return 1 on success
  39. \return 0 on error
  40. */
  41. int Vect_build(struct Map_info *Map)
  42. {
  43. return Vect_build_partial(Map, GV_BUILD_ALL);
  44. }
  45. /*!
  46. \brief Return current highest built level (part)
  47. \param Map vector map
  48. \return current highest built level
  49. */
  50. int Vect_get_built(struct Map_info *Map)
  51. {
  52. return (Map->plus.built);
  53. }
  54. /*!
  55. \brief Build partial topology for vector map.
  56. Should only be used in special cases of vector processing.
  57. This functions optionally builds only some parts of topology. Highest level is specified by build
  58. parameter which may be:
  59. - GV_BUILD_NONE - nothing is build;
  60. - GV_BUILD_BASE - basic topology, nodes, spatial index;
  61. - GV_BUILD_AREAS - build areas and islands, but islands are not attached to areas;
  62. - GV_BUILD_ATTACH_ISLES - attach islands to areas;
  63. - GV_BUILD_CENTROIDS - assign centroids to areas;
  64. - GV_BUILD_ALL - top level, the same as GV_BUILD_CENTROIDS.
  65. If functions is called with build lower than current value of the Map, the level is downgraded to
  66. requested value.
  67. All calls to Vect_write_line(), Vect_rewrite_line(), Vect_delete_line() respect the last value of
  68. build used in this function.
  69. Values lower than GV_BUILD_ALL are supported only by GV_FORMAT_NATIVE,
  70. other formats ignore build and build always GV_BUILD_ALL
  71. Note that the functions has effect only if requested level is higher than current level, to rebuild
  72. part of topology, call first downgrade and then upgrade, for example:
  73. Vect_build()
  74. Vect_build_partial(,GV_BUILD_BASE,)
  75. Vect_build_partial(,GV_BUILD_AREAS,)
  76. \param Map vector map
  77. \param build highest level of build
  78. \return 1 on success, 0 on error
  79. */
  80. int Vect_build_partial(struct Map_info *Map, int build)
  81. {
  82. struct Plus_head *plus;
  83. int ret;
  84. G_debug(3, "Vect_build(): build = %d", build);
  85. /* If topology is already build (map on level2), set level to 1 so that lines will
  86. * be read by V1_read_ (all lines) */
  87. Map->level = 1; /* may be not needed, because V1_read is used directly by Vect_build_ */
  88. Map->support_updated = 1;
  89. Map->plus.Spidx_built = 1;
  90. plus = &(Map->plus);
  91. if (build > GV_BUILD_NONE) {
  92. G_message(_("Building topology for vector map <%s>..."),
  93. Vect_get_name(Map));
  94. }
  95. plus->with_z = Map->head.with_z;
  96. plus->spidx_with_z = Map->head.with_z;
  97. if (build == GV_BUILD_ALL) {
  98. dig_cidx_free(plus); /* free old (if any) category index) */
  99. dig_cidx_init(plus);
  100. }
  101. ret = ((*Build_array[Map->format]) (Map, build));
  102. if (ret == 0) {
  103. return 0;
  104. }
  105. if (build > GV_BUILD_NONE) {
  106. G_verbose_message(_("Topology was built"));
  107. }
  108. Map->level = LEVEL_2;
  109. plus->mode = GV_MODE_WRITE;
  110. if (build == GV_BUILD_ALL) {
  111. plus->cidx_up_to_date = 1; /* category index was build */
  112. dig_cidx_sort(plus);
  113. }
  114. if (build > GV_BUILD_NONE) {
  115. G_message(_("Number of nodes: %d"), plus->n_nodes);
  116. G_message(_("Number of primitives: %d"), plus->n_lines);
  117. G_message(_("Number of points: %d"), plus->n_plines);
  118. G_message(_("Number of lines: %d"), plus->n_llines);
  119. G_message(_("Number of boundaries: %d"), plus->n_blines);
  120. G_message(_("Number of centroids: %d"), plus->n_clines);
  121. if (plus->n_flines > 0)
  122. G_message(_("Number of faces: %d"), plus->n_flines);
  123. if (plus->n_klines > 0)
  124. G_message(_("Number of kernels: %d"), plus->n_klines);
  125. }
  126. if (plus->built >= GV_BUILD_AREAS) {
  127. int line, nlines, area, nareas, err_boundaries, err_centr_out,
  128. err_centr_dupl, err_nocentr;
  129. P_LINE *Line;
  130. struct Plus_head *Plus;
  131. /* Count errors (it does not take much time comparing to build process) */
  132. Plus = &(Map->plus);
  133. nlines = Vect_get_num_lines(Map);
  134. err_boundaries = err_centr_out = err_centr_dupl = 0;
  135. for (line = 1; line <= nlines; line++) {
  136. Line = Plus->Line[line];
  137. if (!Line)
  138. continue;
  139. if (Line->type == GV_BOUNDARY &&
  140. (Line->left == 0 || Line->right == 0)) {
  141. G_debug(3, "line = %d left = %d right = %d", line, Line->left,
  142. Line->right);
  143. err_boundaries++;
  144. }
  145. if (Line->type == GV_CENTROID) {
  146. if (Line->left == 0)
  147. err_centr_out++;
  148. else if (Line->left < 0)
  149. err_centr_dupl++;
  150. }
  151. }
  152. err_nocentr = 0;
  153. nareas = Vect_get_num_areas(Map);
  154. for (area = 1; area <= nareas; area++) {
  155. if (!Vect_area_alive(Map, area))
  156. continue;
  157. line = Vect_get_area_centroid(Map, area);
  158. if (line == 0)
  159. err_nocentr++;
  160. }
  161. G_message(_("Number of areas: %d"), plus->n_areas);
  162. G_message(_("Number of isles: %d"), plus->n_isles);
  163. if (err_boundaries)
  164. G_message(_("Number of incorrect boundaries: %d"),
  165. err_boundaries);
  166. if (err_centr_out)
  167. G_message(_("Number of centroids outside area: %d"),
  168. err_centr_out);
  169. if (err_centr_dupl)
  170. G_message(_("Number of duplicate centroids: %d"),
  171. err_centr_dupl);
  172. if (err_nocentr)
  173. G_message(_("Number of areas without centroid: %d"),
  174. err_nocentr);
  175. }
  176. else if (build > GV_BUILD_NONE) {
  177. G_message(_("Number of areas: -"));
  178. G_message(_("Number of isles: -"));
  179. }
  180. return 1;
  181. }
  182. /*!
  183. \brief Save topology file for vector map
  184. \param Map vector map
  185. \return 1 on success, 0 on error
  186. */
  187. int Vect_save_topo(struct Map_info *Map)
  188. {
  189. struct Plus_head *plus;
  190. char fname[GPATH_MAX], buf[GPATH_MAX];
  191. GVFILE fp;
  192. G_debug(1, "Vect_save_topo()");
  193. plus = &(Map->plus);
  194. /* write out all the accumulated info to the plus file */
  195. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  196. G__file_name(fname, buf, GV_TOPO_ELEMENT, Map->mapset);
  197. G_debug(1, "Open topo: %s", fname);
  198. dig_file_init(&fp);
  199. fp.file = fopen(fname, "w");
  200. if (fp.file == NULL) {
  201. G_warning(_("Unable to open topo file for write <%s>"), fname);
  202. return 0;
  203. }
  204. /* set portable info */
  205. dig_init_portable(&(plus->port), dig__byte_order_out());
  206. if (0 > dig_write_plus_file(&fp, plus)) {
  207. G_warning(_("Error writing out topo file"));
  208. return 0;
  209. }
  210. fclose(fp.file);
  211. return 1;
  212. }
  213. /*!
  214. \brief Dump topology to file
  215. \param Map vector map
  216. \param out file for output (stdout/stderr for example)
  217. \return 1 on success
  218. \return 0 on error
  219. */
  220. int Vect_topo_dump(struct Map_info *Map, FILE * out)
  221. {
  222. int i, j, line, isle;
  223. P_NODE *Node;
  224. P_LINE *Line;
  225. P_AREA *Area;
  226. P_ISLE *Isle;
  227. BOUND_BOX box;
  228. struct Plus_head *plus;
  229. plus = &(Map->plus);
  230. fprintf(out, "---------- TOPOLOGY DUMP ----------\n");
  231. /* box */
  232. Vect_box_copy(&box, &(plus->box));
  233. fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", box.N, box.S,
  234. box.E, box.W, box.T, box.B);
  235. /* nodes */
  236. fprintf(out, "Nodes (%d nodes, alive + dead ):\n", plus->n_nodes);
  237. for (i = 1; i <= plus->n_nodes; i++) {
  238. if (plus->Node[i] == NULL) {
  239. continue;
  240. }
  241. Node = plus->Node[i];
  242. fprintf(out, "node = %d, n_lines = %d, xy = %f, %f\n", i,
  243. Node->n_lines, Node->x, Node->y);
  244. for (j = 0; j < Node->n_lines; j++) {
  245. line = Node->lines[j];
  246. Line = plus->Line[abs(line)];
  247. fprintf(out, " line = %3d, type = %d, angle = %f\n", line,
  248. Line->type, Node->angles[j]);
  249. }
  250. }
  251. /* lines */
  252. fprintf(out, "Lines (%d lines, alive + dead ):\n", plus->n_lines);
  253. for (i = 1; i <= plus->n_lines; i++) {
  254. if (plus->Line[i] == NULL) {
  255. continue;
  256. }
  257. Line = plus->Line[i];
  258. fprintf(out, "line = %d, type = %d, offset = %ld n1 = %d, n2 = %d, "
  259. "left/area = %d, right = %d\n",
  260. i, Line->type, Line->offset, Line->N1, Line->N2,
  261. Line->left, Line->right);
  262. fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", Line->N,
  263. Line->S, Line->E, Line->W, Line->T, Line->B);
  264. }
  265. /* areas */
  266. fprintf(out, "Areas (%d areas, alive + dead ):\n", plus->n_areas);
  267. for (i = 1; i <= plus->n_areas; i++) {
  268. if (plus->Area[i] == NULL) {
  269. continue;
  270. }
  271. Area = plus->Area[i];
  272. fprintf(out, "area = %d, n_lines = %d, n_isles = %d centroid = %d\n",
  273. i, Area->n_lines, Area->n_isles, Area->centroid);
  274. fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", Area->N,
  275. Area->S, Area->E, Area->W, Area->T, Area->B);
  276. for (j = 0; j < Area->n_lines; j++) {
  277. line = Area->lines[j];
  278. Line = plus->Line[abs(line)];
  279. fprintf(out, " line = %3d\n", line);
  280. }
  281. for (j = 0; j < Area->n_isles; j++) {
  282. isle = Area->isles[j];
  283. fprintf(out, " isle = %3d\n", isle);
  284. }
  285. }
  286. /* isles */
  287. fprintf(out, "Islands (%d islands, alive + dead ):\n", plus->n_isles);
  288. for (i = 1; i <= plus->n_isles; i++) {
  289. if (plus->Isle[i] == NULL) {
  290. continue;
  291. }
  292. Isle = plus->Isle[i];
  293. fprintf(out, "isle = %d, n_lines = %d area = %d\n", i, Isle->n_lines,
  294. Isle->area);
  295. fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", Isle->N,
  296. Isle->S, Isle->E, Isle->W, Isle->T, Isle->B);
  297. for (j = 0; j < Isle->n_lines; j++) {
  298. line = Isle->lines[j];
  299. Line = plus->Line[abs(line)];
  300. fprintf(out, " line = %3d\n", line);
  301. }
  302. }
  303. return 1;
  304. }
  305. /*!
  306. \brief Save spatial index file
  307. \param Map vector map
  308. \return 1 on success
  309. \return 0 on error
  310. */
  311. int Vect_save_spatial_index(struct Map_info *Map)
  312. {
  313. struct Plus_head *plus;
  314. char fname[GPATH_MAX], buf[GPATH_MAX];
  315. GVFILE fp;
  316. G_debug(1, "Vect_save_spatial_index()");
  317. plus = &(Map->plus);
  318. /* write out rtrees to the sidx file */
  319. sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  320. G__file_name(fname, buf, GV_SIDX_ELEMENT, Map->mapset);
  321. G_debug(1, "Open sidx: %s", fname);
  322. dig_file_init(&fp);
  323. fp.file = fopen(fname, "w");
  324. if (fp.file == NULL) {
  325. G_warning(_("Unable open spatial index file for write <%s>"), fname);
  326. return 0;
  327. }
  328. /* set portable info */
  329. dig_init_portable(&(plus->spidx_port), dig__byte_order_out());
  330. if (0 > dig_write_spidx(&fp, plus)) {
  331. G_warning(_("Error writing out spatial index file"));
  332. return 0;
  333. }
  334. fclose(fp.file);
  335. return 1;
  336. }
  337. /*!
  338. \brief Dump spatial index to file
  339. \param Map vector map
  340. \param out file for output (stdout/stderr for example)
  341. \return 1 on success
  342. \return 0 on error
  343. */
  344. int Vect_spatial_index_dump(struct Map_info *Map, FILE * out)
  345. {
  346. if (!(Map->plus.Spidx_built)) {
  347. Vect_build_sidx_from_topo(Map);
  348. }
  349. fprintf(out, "---------- SPATIAL INDEX DUMP ----------\n");
  350. dig_dump_spidx(out, &(Map->plus));
  351. return 1;
  352. }