build.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*!
  2. \file lib/vector/Vlib/build.c
  3. \brief Vector library - Building topology
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2010 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <stdarg.h>
  14. #include <unistd.h>
  15. #include <grass/glocale.h>
  16. #include <grass/vector.h>
  17. #ifndef HAVE_OGR
  18. static int format()
  19. {
  20. G_fatal_error(_("Requested format is not compiled in this version"));
  21. return 0;
  22. }
  23. #endif
  24. static int (*Build_array[]) () = {
  25. Vect_build_nat
  26. #ifdef HAVE_OGR
  27. , Vect_build_ogr
  28. , Vect_build_ogr
  29. #else
  30. , format
  31. , format
  32. #endif
  33. };
  34. /*!
  35. \brief Build topology for vector map
  36. \param Map vector map
  37. \return 1 on success
  38. \return 0 on error
  39. */
  40. int Vect_build(struct Map_info *Map)
  41. {
  42. return Vect_build_partial(Map, GV_BUILD_ALL);
  43. }
  44. /*!
  45. \brief Return current highest built level (part)
  46. \param Map vector map
  47. \return current highest built level
  48. */
  49. int Vect_get_built(const struct Map_info *Map)
  50. {
  51. return (Map->plus.built);
  52. }
  53. /*!
  54. \brief Build partial topology for vector map.
  55. Should only be used in special cases of vector processing.
  56. This functions optionally builds only some parts of topology. Highest level is specified by build
  57. parameter which may be:
  58. - GV_BUILD_NONE - nothing is build;
  59. - GV_BUILD_BASE - basic topology, nodes, spatial index;
  60. - GV_BUILD_AREAS - build areas and islands, but islands are not attached to areas;
  61. - GV_BUILD_ATTACH_ISLES - attach islands to areas;
  62. - GV_BUILD_CENTROIDS - assign centroids to areas;
  63. - GV_BUILD_ALL - top level, the same as GV_BUILD_CENTROIDS.
  64. If functions is called with build lower than current value of the
  65. Map, the level is downgraded to requested value.
  66. All calls to Vect_write_line(), Vect_rewrite_line(),
  67. Vect_delete_line() respect the last value of build used in this
  68. function.
  69. Values lower than GV_BUILD_ALL are supported only by
  70. GV_FORMAT_NATIVE, other formats ignore build and build always
  71. GV_BUILD_ALL
  72. Note that the functions has effect only if requested level is
  73. higher than current level, to rebuild part of topology, call first
  74. downgrade and then upgrade, for example:
  75. - Vect_build()
  76. - Vect_build_partial(,GV_BUILD_BASE,)
  77. - Vect_build_partial(,GV_BUILD_AREAS,)
  78. \param Map vector map
  79. \param build highest level of build
  80. \return 1 on success
  81. \return 0 on error
  82. */
  83. int Vect_build_partial(struct Map_info *Map, int build)
  84. {
  85. struct Plus_head *plus;
  86. int ret;
  87. G_debug(3, "Vect_build(): build = %d", build);
  88. /* If topology is already build (map on level2), set level to 1 so that lines will
  89. * be read by V1_read_ (all lines) */
  90. Map->level = 1; /* may be not needed, because V1_read is used directly by Vect_build_ */
  91. if (Map->format != GV_FORMAT_OGR_DIRECT)
  92. Map->support_updated = 1;
  93. if (Map->plus.Spidx_built == 0)
  94. Vect_open_sidx(Map, 2);
  95. plus = &(Map->plus);
  96. if (build > GV_BUILD_NONE) {
  97. G_message(_("Building topology for vector map <%s>..."),
  98. Vect_get_full_name(Map));
  99. }
  100. plus->with_z = Map->head.with_z;
  101. plus->spidx_with_z = Map->head.with_z;
  102. if (build == GV_BUILD_ALL) {
  103. dig_cidx_free(plus); /* free old (if any) category index) */
  104. dig_cidx_init(plus);
  105. }
  106. ret = ((*Build_array[Map->format]) (Map, build));
  107. if (ret == 0) {
  108. return 0;
  109. }
  110. if (build > GV_BUILD_NONE) {
  111. G_verbose_message(_("Topology was built"));
  112. }
  113. Map->level = LEVEL_2;
  114. plus->mode = GV_MODE_WRITE;
  115. if (build == GV_BUILD_ALL) {
  116. plus->cidx_up_to_date = 1; /* category index was build */
  117. dig_cidx_sort(plus);
  118. }
  119. if (build > GV_BUILD_NONE) {
  120. G_message(_("Number of nodes: %d"), plus->n_nodes);
  121. G_message(_("Number of primitives: %d"), plus->n_lines);
  122. G_message(_("Number of points: %d"), plus->n_plines);
  123. G_message(_("Number of lines: %d"), plus->n_llines);
  124. G_message(_("Number of boundaries: %d"), plus->n_blines);
  125. G_message(_("Number of centroids: %d"), plus->n_clines);
  126. if (plus->n_flines > 0)
  127. G_message(_("Number of faces: %d"), plus->n_flines);
  128. if (plus->n_klines > 0)
  129. G_message(_("Number of kernels: %d"), plus->n_klines);
  130. }
  131. if (plus->built >= GV_BUILD_AREAS) {
  132. int line, nlines, area, nareas, err_boundaries, err_centr_out,
  133. err_centr_dupl, err_nocentr;
  134. struct P_line *Line;
  135. struct Plus_head *Plus;
  136. /* Count errors (it does not take much time comparing to build process) */
  137. Plus = &(Map->plus);
  138. nlines = Vect_get_num_lines(Map);
  139. err_boundaries = err_centr_out = err_centr_dupl = 0;
  140. for (line = 1; line <= nlines; line++) {
  141. Line = Plus->Line[line];
  142. if (!Line)
  143. continue;
  144. if (Line->type == GV_BOUNDARY &&
  145. (Line->left == 0 || Line->right == 0)) {
  146. G_debug(3, "line = %d left = %d right = %d", line, Line->left,
  147. Line->right);
  148. err_boundaries++;
  149. }
  150. if (Line->type == GV_CENTROID) {
  151. if (Line->left == 0)
  152. err_centr_out++;
  153. else if (Line->left < 0)
  154. err_centr_dupl++;
  155. }
  156. }
  157. err_nocentr = 0;
  158. nareas = Vect_get_num_areas(Map);
  159. for (area = 1; area <= nareas; area++) {
  160. if (!Vect_area_alive(Map, area))
  161. continue;
  162. line = Vect_get_area_centroid(Map, area);
  163. if (line == 0)
  164. err_nocentr++;
  165. }
  166. G_message(_("Number of areas: %d"), plus->n_areas);
  167. G_message(_("Number of isles: %d"), plus->n_isles);
  168. if (err_boundaries)
  169. G_message(_("Number of incorrect boundaries: %d"),
  170. err_boundaries);
  171. if (err_centr_out)
  172. G_message(_("Number of centroids outside area: %d"),
  173. err_centr_out);
  174. if (err_centr_dupl)
  175. G_message(_("Number of duplicate centroids: %d"), err_centr_dupl);
  176. if (err_nocentr)
  177. G_message(_("Number of areas without centroid: %d"), err_nocentr);
  178. }
  179. else if (build > GV_BUILD_NONE) {
  180. G_message(_("Number of areas: -"));
  181. G_message(_("Number of isles: -"));
  182. }
  183. return 1;
  184. }
  185. /*!
  186. \brief Save topology file for vector map
  187. \param Map vector map
  188. \return 1 on success
  189. \return 0 on error
  190. */
  191. int Vect_save_topo(struct Map_info *Map)
  192. {
  193. struct Plus_head *plus;
  194. char fname[GPATH_MAX], buf[GPATH_MAX];
  195. struct gvfile fp;
  196. G_debug(1, "Vect_save_topo()");
  197. plus = &(Map->plus);
  198. /* write out all the accumulated info to the plus file */
  199. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  200. G_file_name(fname, buf, GV_TOPO_ELEMENT, Map->mapset);
  201. G_debug(1, "Open topo: %s", fname);
  202. dig_file_init(&fp);
  203. fp.file = fopen(fname, "w");
  204. if (fp.file == NULL) {
  205. G_warning(_("Unable to open topo file for write <%s>"), fname);
  206. return 0;
  207. }
  208. /* set portable info */
  209. dig_init_portable(&(plus->port), dig__byte_order_out());
  210. if (0 > dig_write_plus_file(&fp, plus)) {
  211. G_warning(_("Error writing out topo file"));
  212. return 0;
  213. }
  214. fclose(fp.file);
  215. return 1;
  216. }
  217. /*!
  218. \brief Dump topology to file
  219. \param Map vector map
  220. \param out file for output (stdout/stderr for example)
  221. \return 1 on success
  222. \return 0 on error
  223. */
  224. int Vect_topo_dump(const struct Map_info *Map, FILE *out)
  225. {
  226. int i, j, line, isle;
  227. struct P_node *Node;
  228. struct P_line *Line;
  229. struct P_area *Area;
  230. struct P_isle *Isle;
  231. struct bound_box box;
  232. const struct Plus_head *plus;
  233. plus = &(Map->plus);
  234. fprintf(out, "---------- TOPOLOGY DUMP ----------\n");
  235. /* box */
  236. Vect_box_copy(&box, &(plus->box));
  237. fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", box.N, box.S,
  238. box.E, box.W, box.T, box.B);
  239. /* nodes */
  240. fprintf(out, "Nodes (%d nodes, alive + dead ):\n", plus->n_nodes);
  241. for (i = 1; i <= plus->n_nodes; i++) {
  242. if (plus->Node[i] == NULL) {
  243. continue;
  244. }
  245. Node = plus->Node[i];
  246. fprintf(out, "node = %d, n_lines = %d, xyz = %f, %f, %f\n", i,
  247. Node->n_lines, Node->x, Node->y, Node->z);
  248. for (j = 0; j < Node->n_lines; j++) {
  249. line = Node->lines[j];
  250. Line = plus->Line[abs(line)];
  251. fprintf(out, " line = %3d, type = %d, angle = %f\n", line,
  252. Line->type, Node->angles[j]);
  253. }
  254. }
  255. /* lines */
  256. fprintf(out, "Lines (%d lines, alive + dead ):\n", plus->n_lines);
  257. for (i = 1; i <= plus->n_lines; i++) {
  258. if (plus->Line[i] == NULL) {
  259. continue;
  260. }
  261. Line = plus->Line[i];
  262. fprintf(out, "line = %d, type = %d, offset = %lu n1 = %d, n2 = %d, "
  263. "left/area = %d, right = %d\n",
  264. i, Line->type, (unsigned long)Line->offset, Line->N1,
  265. Line->N2, Line->left, Line->right);
  266. fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", Line->N,
  267. Line->S, Line->E, Line->W, Line->T, Line->B);
  268. }
  269. /* areas */
  270. fprintf(out, "Areas (%d areas, alive + dead ):\n", plus->n_areas);
  271. for (i = 1; i <= plus->n_areas; i++) {
  272. if (plus->Area[i] == NULL) {
  273. continue;
  274. }
  275. Area = plus->Area[i];
  276. fprintf(out, "area = %d, n_lines = %d, n_isles = %d centroid = %d\n",
  277. i, Area->n_lines, Area->n_isles, Area->centroid);
  278. fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", Area->N,
  279. Area->S, Area->E, Area->W, Area->T, Area->B);
  280. for (j = 0; j < Area->n_lines; j++) {
  281. line = Area->lines[j];
  282. Line = plus->Line[abs(line)];
  283. fprintf(out, " line = %3d\n", line);
  284. }
  285. for (j = 0; j < Area->n_isles; j++) {
  286. isle = Area->isles[j];
  287. fprintf(out, " isle = %3d\n", isle);
  288. }
  289. }
  290. /* isles */
  291. fprintf(out, "Islands (%d islands, alive + dead ):\n", plus->n_isles);
  292. for (i = 1; i <= plus->n_isles; i++) {
  293. if (plus->Isle[i] == NULL) {
  294. continue;
  295. }
  296. Isle = plus->Isle[i];
  297. fprintf(out, "isle = %d, n_lines = %d area = %d\n", i, Isle->n_lines,
  298. Isle->area);
  299. fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", Isle->N,
  300. Isle->S, Isle->E, Isle->W, Isle->T, Isle->B);
  301. for (j = 0; j < Isle->n_lines; j++) {
  302. line = Isle->lines[j];
  303. Line = plus->Line[abs(line)];
  304. fprintf(out, " line = %3d\n", line);
  305. }
  306. }
  307. return 1;
  308. }
  309. /*!
  310. \brief Create spatial index if necessary.
  311. To be used in modules.
  312. Map must be opened on level 2.
  313. \param[in,out] Map pointer to vector map
  314. \return 0 OK
  315. \return 1 error
  316. */
  317. int Vect_build_sidx(struct Map_info *Map)
  318. {
  319. if (Map->level < 2) {
  320. G_fatal_error(_("Unable to build spatial index from topology, "
  321. "vector map is not opened at topology level 2"));
  322. }
  323. if (!(Map->plus.Spidx_built)) {
  324. return (Vect_build_sidx_from_topo(Map));
  325. }
  326. return 0;
  327. }
  328. /*!
  329. \brief Create spatial index from topology if necessary
  330. \param Map pointer to vector map
  331. \return 0 OK
  332. \return 1 error
  333. */
  334. int Vect_build_sidx_from_topo(struct Map_info *Map)
  335. {
  336. int i, total, done;
  337. struct Plus_head *plus;
  338. struct bound_box box;
  339. struct P_line *Line;
  340. struct P_node *Node;
  341. struct P_area *Area;
  342. struct P_isle *Isle;
  343. G_debug(3, "Vect_build_sidx_from_topo()");
  344. plus = &(Map->plus);
  345. Vect_open_sidx(Map, 2);
  346. total = plus->n_nodes + plus->n_lines + plus->n_areas + plus->n_isles;
  347. /* Nodes */
  348. for (i = 1; i <= plus->n_nodes; i++) {
  349. G_percent(i, total, 3);
  350. Node = plus->Node[i];
  351. if (!Node)
  352. G_fatal_error(_("BUG (Vect_build_sidx_from_topo): node does not exist"));
  353. dig_spidx_add_node(plus, i, Node->x, Node->y, Node->z);
  354. }
  355. /* Lines */
  356. done = plus->n_nodes;
  357. for (i = 1; i <= plus->n_lines; i++) {
  358. G_percent(done + i, total, 3);
  359. Line = plus->Line[i];
  360. if (!Line)
  361. G_fatal_error(_("BUG (Vect_build_sidx_from_topo): line does not exist"));
  362. box.N = Line->N;
  363. box.S = Line->S;
  364. box.E = Line->E;
  365. box.W = Line->W;
  366. box.T = Line->T;
  367. box.B = Line->B;
  368. dig_spidx_add_line(plus, i, &box);
  369. }
  370. /* Areas */
  371. done += plus->n_lines;
  372. for (i = 1; i <= plus->n_areas; i++) {
  373. G_percent(done + i, total, 3);
  374. Area = plus->Area[i];
  375. if (!Area)
  376. G_fatal_error(_("BUG (Vect_build_sidx_from_topo): area does not exist"));
  377. box.N = Area->N;
  378. box.S = Area->S;
  379. box.E = Area->E;
  380. box.W = Area->W;
  381. box.T = Area->T;
  382. box.B = Area->B;
  383. dig_spidx_add_area(plus, i, &box);
  384. }
  385. /* Isles */
  386. done += plus->n_areas;
  387. for (i = 1; i <= plus->n_isles; i++) {
  388. G_percent(done + i, total, 3);
  389. Isle = plus->Isle[i];
  390. if (!Isle)
  391. G_fatal_error(_("BUG (Vect_build_sidx_from_topo): isle does not exist"));
  392. box.N = Isle->N;
  393. box.S = Isle->S;
  394. box.E = Isle->E;
  395. box.W = Isle->W;
  396. box.T = Isle->T;
  397. box.B = Isle->B;
  398. dig_spidx_add_isle(plus, i, &box);
  399. }
  400. Map->plus.Spidx_built = 1;
  401. G_debug(3, "Spatial index was built");
  402. return 0;
  403. }
  404. /*!
  405. \brief Save spatial index file for vector map
  406. \param Map vector map
  407. \return 1 on success
  408. \return 0 on error
  409. */
  410. int Vect_save_sidx(struct Map_info *Map)
  411. {
  412. struct Plus_head *plus;
  413. char fname[GPATH_MAX], buf[GPATH_MAX];
  414. G_debug(1, "Vect_save_spatial_index()");
  415. plus = &(Map->plus);
  416. if (plus->Spidx_built == 0) {
  417. G_warning("Spatial index not available, can not be saved");
  418. return 0;
  419. }
  420. /* new or update mode ? */
  421. if (plus->Spidx_new == 1) {
  422. /* write out rtrees to sidx file */
  423. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  424. G_file_name(fname, buf, GV_SIDX_ELEMENT, Map->mapset);
  425. G_debug(1, "Open sidx: %s", fname);
  426. dig_file_init(&(plus->spidx_fp));
  427. plus->spidx_fp.file = fopen(fname, "w+");
  428. if (plus->spidx_fp.file == NULL) {
  429. G_warning(_("Unable open spatial index file for write <%s>"),
  430. fname);
  431. return 0;
  432. }
  433. /* set portable info */
  434. dig_init_portable(&(plus->spidx_port), dig__byte_order_out());
  435. if (0 > dig_Wr_spidx(&(plus->spidx_fp), plus)) {
  436. G_warning(_("Error writing out spatial index file"));
  437. return 0;
  438. }
  439. dig_spidx_free(plus);
  440. Map->plus.Spidx_new = 0;
  441. }
  442. fclose(Map->plus.spidx_fp.file);
  443. Map->plus.Spidx_built = 0;
  444. return 1;
  445. }
  446. /*!
  447. \brief Dump spatial index to file
  448. \param Map vector map
  449. \param out file for output (stdout/stderr for example)
  450. \return 1 on success
  451. \return 0 on error
  452. */
  453. int Vect_sidx_dump(struct Map_info *Map, FILE * out)
  454. {
  455. if (!(Map->plus.Spidx_built)) {
  456. Vect_build_sidx_from_topo(Map);
  457. }
  458. fprintf(out, "---------- SPATIAL INDEX DUMP ----------\n");
  459. dig_dump_spidx(out, &(Map->plus));
  460. return 1;
  461. }