build.c 11 KB

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