build.c 14 KB

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