build.c 14 KB

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