build.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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, 2012 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 <math.h>
  16. #include <grass/vector.h>
  17. #include <grass/glocale.h>
  18. #define SEP "-----------------------------------\n"
  19. #if !defined HAVE_OGR || !defined HAVE_POSTGRES
  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. #ifdef HAVE_POSTGRES
  36. , Vect_build_pg
  37. #else
  38. , format
  39. #endif
  40. };
  41. /*!
  42. \brief Build area on given side of line (GV_LEFT or GV_RIGHT)
  43. \param Map pointer to Map_info structure
  44. \param iline line id
  45. \param side side (GV_LEFT or GV_RIGHT)
  46. \return > 0 number of area
  47. \return < 0 number of isle
  48. \return 0 not created (may also already exist)
  49. */
  50. int Vect_build_line_area(struct Map_info *Map, int iline, int side)
  51. {
  52. int j, area, isle, n_lines, line, direction;
  53. static int first = 1;
  54. off_t offset;
  55. struct Plus_head *plus;
  56. struct P_line *BLine;
  57. static struct line_pnts *Points, *APoints;
  58. struct bound_box box;
  59. plus_t *lines;
  60. double area_size;
  61. plus = &(Map->plus);
  62. G_debug(3, "Vect_build_line_area() line = %d, side = %d", iline, side);
  63. if (first) {
  64. Points = Vect_new_line_struct();
  65. APoints = Vect_new_line_struct();
  66. first = 0;
  67. }
  68. area = dig_line_get_area(plus, iline, side);
  69. if (area != 0) {
  70. G_debug(3, " area/isle = %d -> skip", area);
  71. return 0;
  72. }
  73. n_lines = dig_build_area_with_line(plus, iline, side, &lines);
  74. G_debug(3, " n_lines = %d", n_lines);
  75. if (n_lines < 1) {
  76. return 0;
  77. } /* area was not built */
  78. /* Area or island ? */
  79. Vect_reset_line(APoints);
  80. for (j = 0; j < n_lines; j++) {
  81. line = abs(lines[j]);
  82. BLine = plus->Line[line];
  83. offset = BLine->offset;
  84. G_debug(3, " line[%d] = %d, offset = %lu", j, line,
  85. (unsigned long)offset);
  86. Vect_read_line(Map, Points, NULL, line);
  87. if (lines[j] > 0)
  88. direction = GV_FORWARD;
  89. else
  90. direction = GV_BACKWARD;
  91. Vect_append_points(APoints, Points, direction);
  92. APoints->n_points--; /* skip last point, avoids duplicates */
  93. }
  94. dig_line_box(APoints, &box);
  95. APoints->n_points++; /* close polygon */
  96. dig_find_area_poly(APoints, &area_size);
  97. /* area_size = dig_find_poly_orientation(APoints); */
  98. /* area_size is not real area size, we are only interested in the sign */
  99. G_debug(3, " area/isle size = %f", area_size);
  100. if (area_size > 0) { /* CW: area */
  101. /* add area structure to plus */
  102. area = dig_add_area(plus, n_lines, lines, &box);
  103. if (area == -1) { /* error */
  104. Vect_close(Map);
  105. G_fatal_error(_("Unable to add area (map closed, topo saved)"));
  106. }
  107. G_debug(3, " -> area %d", area);
  108. return area;
  109. }
  110. else if (area_size < 0) { /* CCW: island */
  111. isle = dig_add_isle(plus, n_lines, lines, &box);
  112. if (isle == -1) { /* error */
  113. Vect_close(Map);
  114. G_fatal_error(_("Unable to add isle (map closed, topo saved)"));
  115. }
  116. G_debug(3, " -> isle %d", isle);
  117. return -isle;
  118. }
  119. else {
  120. /* TODO: What to do with such areas? Should be areas/isles of size 0 stored,
  121. * so that may be found and cleaned by some utility
  122. * Note: it would be useful for vertical closed polygons, but such would be added twice
  123. * as area */
  124. G_warning(_("Area of size = 0.0 ignored"));
  125. }
  126. return 0;
  127. }
  128. /*!
  129. \brief Find area outside island
  130. \param Map_info vector map
  131. \param isle isle id
  132. \return area id
  133. \return 0 if not found
  134. */
  135. int Vect_isle_find_area(struct Map_info *Map, int isle)
  136. {
  137. int j, line, sel_area, area, poly;
  138. static int first_call = 1;
  139. const struct Plus_head *plus;
  140. struct P_line *Line;
  141. struct P_node *Node;
  142. struct P_isle *Isle;
  143. struct P_area *Area;
  144. struct P_topo_b *topo;
  145. double size, cur_size;
  146. struct bound_box box, abox;
  147. static struct boxlist *List;
  148. static struct line_pnts *APoints;
  149. /* Note: We should check all isle points (at least) because if topology is not clean
  150. * and two areas overlap, isle which is not completely within area may be attached,
  151. * but it would take long time */
  152. G_debug(3, "Vect_isle_find_area () island = %d", isle);
  153. plus = &(Map->plus);
  154. if (plus->Isle[isle] == NULL) {
  155. G_warning(_("Request to find area outside nonexistent isle"));
  156. return 0;
  157. }
  158. if (first_call) {
  159. List = Vect_new_boxlist(1);
  160. APoints = Vect_new_line_struct();
  161. first_call = 0;
  162. }
  163. Isle = plus->Isle[isle];
  164. line = abs(Isle->lines[0]);
  165. Line = plus->Line[line];
  166. topo = (struct P_topo_b *)Line->topo;
  167. Node = plus->Node[topo->N1];
  168. /* select areas by box */
  169. box.E = Node->x;
  170. box.W = Node->x;
  171. box.N = Node->y;
  172. box.S = Node->y;
  173. box.T = PORT_DOUBLE_MAX;
  174. box.B = -PORT_DOUBLE_MAX;
  175. Vect_select_areas_by_box(Map, &box, List);
  176. G_debug(3, "%d areas overlap island boundary point", List->n_values);
  177. sel_area = 0;
  178. cur_size = -1;
  179. Vect_get_isle_box(Map, isle, &box);
  180. for (j = 0; j < List->n_values; j++) {
  181. area = List->id[j];
  182. G_debug(3, "area = %d", area);
  183. Area = plus->Area[area];
  184. /* Before other tests, simply exclude those areas inside isolated isles formed by one boundary */
  185. if (abs(Isle->lines[0]) == abs(Area->lines[0])) {
  186. G_debug(3, " area inside isolated isle");
  187. continue;
  188. }
  189. /* Check box */
  190. /* Note: If build is run on large files of areas imported from nontopo format (shapefile)
  191. * attaching of isles takes very long time because each area is also isle and select by
  192. * box all overlapping areas selects all areas with box overlapping first node.
  193. * Then reading coordinates for all those areas would take a long time -> check first
  194. * if isle's box is completely within area box */
  195. abox = List->box[j];
  196. if (box.E > abox.E || box.W < abox.W || box.N > abox.N ||
  197. box.S < abox.S) {
  198. G_debug(3, " isle not completely inside area box");
  199. continue;
  200. }
  201. poly = Vect_point_in_area_outer_ring(Node->x, Node->y, Map, area, abox);
  202. G_debug(3, " poly = %d", poly);
  203. if (poly == 1) { /* point in area, but node is not part of area inside isle (would be poly == 2) */
  204. /* In rare case island is inside more areas in that case we have to calculate area
  205. * of outer ring and take the smaller */
  206. if (sel_area == 0) { /* first */
  207. sel_area = area;
  208. }
  209. else { /* is not first */
  210. if (cur_size < 0) { /* second area */
  211. /* This is slow, but should not be called often */
  212. Vect_get_area_points(Map, sel_area, APoints);
  213. /* G_begin_polygon_area_calculations();
  214. cur_size =
  215. G_area_of_polygon(APoints->x, APoints->y,
  216. APoints->n_points); */
  217. /* this is faster, but there may be latlon problems: the poles */
  218. dig_find_area_poly(APoints, &cur_size);
  219. G_debug(3, " first area size = %f (n points = %d)",
  220. cur_size, APoints->n_points);
  221. }
  222. Vect_get_area_points(Map, area, APoints);
  223. /* size =
  224. G_area_of_polygon(APoints->x, APoints->y,
  225. APoints->n_points); */
  226. /* this is faster, but there may be latlon problems: the poles */
  227. dig_find_area_poly(APoints, &size);
  228. G_debug(3, " area size = %f (n points = %d)", size,
  229. APoints->n_points);
  230. if (size > 0 && size < cur_size) {
  231. sel_area = area;
  232. cur_size = size;
  233. }
  234. }
  235. G_debug(3, "sel_area = %d cur_size = %f", sel_area, cur_size);
  236. }
  237. }
  238. if (sel_area > 0) {
  239. G_debug(3, "Island %d in area %d", isle, sel_area);
  240. }
  241. else {
  242. G_debug(3, "Island %d is not in area", isle);
  243. }
  244. return sel_area;
  245. }
  246. /*!
  247. \brief (Re)Attach isle to area
  248. \param Map_info vector map
  249. \param isle isle id
  250. \return 0
  251. */
  252. int Vect_attach_isle(struct Map_info *Map, int isle)
  253. {
  254. int sel_area;
  255. struct P_isle *Isle;
  256. struct Plus_head *plus;
  257. /* Note!: If topology is not clean and areas overlap, one island
  258. may fall to more areas (partially or fully). Before isle is
  259. attached to area it must be check if it is not attached yet */
  260. G_debug(3, "Vect_attach_isle(): isle = %d", isle);
  261. plus = &(Map->plus);
  262. sel_area = Vect_isle_find_area(Map, isle);
  263. G_debug(3, "\tisle = %d -> area outside = %d", isle, sel_area);
  264. if (sel_area > 0) {
  265. Isle = plus->Isle[isle];
  266. if (Isle->area > 0) {
  267. G_debug(3, "Attempt to attach isle %d to more areas "
  268. "(=>topology is not clean)", isle);
  269. }
  270. else {
  271. Isle->area = sel_area;
  272. dig_area_add_isle(plus, sel_area, isle);
  273. }
  274. }
  275. return 0;
  276. }
  277. /*!
  278. \brief (Re)Attach isles to areas in given bounding box
  279. \param Map_info vector map
  280. \param box bounding box
  281. \return 0
  282. */
  283. int Vect_attach_isles(struct Map_info *Map, const struct bound_box *box)
  284. {
  285. int i, isle;
  286. static int first = TRUE;
  287. static struct boxlist *List;
  288. struct Plus_head *plus;
  289. G_debug(3, "Vect_attach_isles()");
  290. plus = &(Map->plus);
  291. if (first) {
  292. List = Vect_new_boxlist(FALSE);
  293. first = FALSE;
  294. }
  295. Vect_select_isles_by_box(Map, box, List);
  296. G_debug(3, " number of isles to attach = %d", List->n_values);
  297. for (i = 0; i < List->n_values; i++) {
  298. isle = List->id[i];
  299. /* only attach isles that are not yet attached, see Vect_attach_isle() */
  300. if (plus->Isle[isle]->area == 0)
  301. Vect_attach_isle(Map, isle);
  302. }
  303. return 0;
  304. }
  305. /*!
  306. \brief (Re)Attach centroids to areas in given bounding box
  307. Warning: If map is updated on level2, it may happen that
  308. previously correct island becomes incorrect. In that case,
  309. centroid of area forming the island is reattached to outer area,
  310. because island polygon is not excluded.
  311. <pre>
  312. +-----------+ +-----------+
  313. | 1 | | 1 |
  314. | +---+---+ | | +---+---+ |
  315. | | 2 | 3 | | | | 2 | |
  316. | | x | | | -> | | x | |
  317. | | | | | | | | |
  318. | +---+---+ | | +---+---+ |
  319. | | | |
  320. +-----------+ +-----------+
  321. centroid is centroid is
  322. attached to 2 reattached to 1
  323. </pre>
  324. Because of this, when the centroid is reattached to another area,
  325. it is always necessary to check if original area exist, unregister
  326. centroid from previous area. To simplify code, this is
  327. implemented so that centroid is always firs unregistered and if
  328. new area is found, it is registered again.
  329. This problem can be avoided altogether if properly attached
  330. centroids are skipped MM 2009
  331. \param Map_info vector map
  332. \param box bounding box
  333. \return 0
  334. */
  335. int Vect_attach_centroids(struct Map_info *Map, const struct bound_box * box)
  336. {
  337. int i, sel_area, centr;
  338. static int first = 1;
  339. static struct boxlist *List;
  340. static struct line_pnts *Points;
  341. struct P_area *Area;
  342. struct P_line *Line;
  343. struct P_topo_c *topo;
  344. struct Plus_head *plus;
  345. G_debug(3, "Vect_attach_centroids()");
  346. plus = &(Map->plus);
  347. if (first) {
  348. List = Vect_new_boxlist(0);
  349. Points = Vect_new_line_struct();
  350. first = 0;
  351. }
  352. Vect_select_lines_by_box(Map, box, GV_CENTROID, List);
  353. G_debug(3, "\tnumber of centroids to reattach = %d", List->n_values);
  354. for (i = 0; i < List->n_values; i++) {
  355. int orig_area;
  356. centr = List->id[i];
  357. Line = plus->Line[centr];
  358. topo = (struct P_topo_c *)Line->topo;
  359. /* only attach unregistered and duplicate centroids because
  360. * 1) all properly attached centroids are properly attached, really! Don't touch.
  361. * 2) Vect_find_area() below does not always return the correct area
  362. * 3) it's faster
  363. */
  364. if (topo->area > 0)
  365. continue;
  366. orig_area = topo->area;
  367. Vect_read_line(Map, Points, NULL, centr);
  368. if (Points->n_points < 1) {
  369. /* try to get centroid from spatial index (OGR layers) */
  370. int found;
  371. struct boxlist list;
  372. dig_init_boxlist(&list, TRUE);
  373. Vect_select_lines_by_box(Map, box, GV_CENTROID, &list);
  374. found = 0;
  375. for (i = 0; i < list.n_values; i++) {
  376. if (list.id[i] == centr) {
  377. found = i;
  378. break;
  379. }
  380. }
  381. Vect_append_point(Points, list.box[found].E, list.box[found].N, 0.0);
  382. }
  383. sel_area = Vect_find_area(Map, Points->x[0], Points->y[0]);
  384. G_debug(3, "\tcentroid %d is in area %d", centr, sel_area);
  385. if (sel_area > 0) {
  386. Area = plus->Area[sel_area];
  387. if (Area->centroid == 0) { /* first centroid */
  388. G_debug(3, "\tfirst centroid -> attach to area");
  389. Area->centroid = centr;
  390. topo->area = sel_area;
  391. if (sel_area != orig_area && plus->uplist.do_uplist)
  392. dig_line_add_updated(plus, centr);
  393. }
  394. else if (Area->centroid != centr) { /* duplicate centroid */
  395. /* Note: it cannot happen that Area->centroid == centr, because the centroid
  396. * was not registered or a duplicate */
  397. G_debug(3, "\tduplicate centroid -> do not attach to area");
  398. topo->area = -sel_area;
  399. if (-sel_area != orig_area && plus->uplist.do_uplist)
  400. dig_line_add_updated(plus, centr);
  401. }
  402. }
  403. }
  404. return 0;
  405. }
  406. /*!
  407. \brief Build topology for vector map
  408. \param Map vector map
  409. \return 1 on success
  410. \return 0 on error
  411. */
  412. int Vect_build(struct Map_info *Map)
  413. {
  414. return Vect_build_partial(Map, GV_BUILD_ALL);
  415. }
  416. /*!
  417. \brief Extensive tests for correct topology
  418. \param Map vector map
  419. \param[out] Err vector map where errors will be written
  420. \return 1 on success
  421. \return 0 on error
  422. */
  423. int Vect_topo_check(struct Map_info *Map, struct Map_info *Err)
  424. {
  425. int line, nlines;
  426. int nerrors, n_zero_lines, n_zero_boundaries;
  427. struct line_pnts *Points;
  428. struct line_cats *Cats;
  429. if (Vect_get_built(Map) != GV_BUILD_ALL) {
  430. Vect_build_partial(Map, GV_BUILD_NONE);
  431. Vect_build(Map);
  432. }
  433. G_message(_("Checking for topological errors..."));
  434. Points = Vect_new_line_struct();
  435. Cats = Vect_new_cats_struct();
  436. /* boundaries of zero length */
  437. n_zero_lines = n_zero_boundaries = 0;
  438. nlines = Vect_get_num_lines(Map);
  439. for (line = 1; line <= nlines; line++) {
  440. int type;
  441. if (!Vect_line_alive(Map, line))
  442. continue;
  443. type = Vect_get_line_type(Map, line);
  444. if (type & GV_LINES) {
  445. double len;
  446. Vect_read_line(Map, Points, Cats, line);
  447. len = Vect_line_length(Points);
  448. if (len == 0) {
  449. if (type & GV_LINE)
  450. n_zero_lines++;
  451. else if (type & GV_BOUNDARY)
  452. n_zero_boundaries++;
  453. if (Err)
  454. Vect_write_line(Err, type, Points, Cats);
  455. }
  456. }
  457. }
  458. if (n_zero_lines)
  459. G_warning(_("Number of lines of length zero: %d"), n_zero_lines);
  460. if (n_zero_boundaries)
  461. G_warning(_("Number of boundaries of length zero: %d"), n_zero_boundaries);
  462. /* remaining checks are for areas only */
  463. if (Vect_get_num_primitives(Map, GV_BOUNDARY) == 0)
  464. return 1;
  465. /* intersecting boundaries -> overlapping areas */
  466. nerrors = Vect_check_line_breaks(Map, GV_BOUNDARY, Err);
  467. if (nerrors)
  468. G_warning(_("Number of boundary intersections: %d"), nerrors);
  469. /* areas without centroids that are not holes
  470. * only makes sense if all boundaries are correct */
  471. nerrors = 0;
  472. for (line = 1; line <= nlines; line++) {
  473. int type;
  474. if (!Vect_line_alive(Map, line))
  475. continue;
  476. type = Vect_get_line_type(Map, line);
  477. if (type == GV_BOUNDARY) {
  478. struct P_topo_b *topo = (struct P_topo_b *)Map->plus.Line[line]->topo;
  479. if (topo->left == 0 || topo->right == 0) {
  480. G_debug(3, "line = %d left = %d right = %d", line,
  481. topo->left, topo->right);
  482. nerrors++;
  483. }
  484. }
  485. }
  486. if (nerrors)
  487. G_warning(_("Skipping further checks because of incorrect boundaries"));
  488. else {
  489. int i, area, left, right, neighbour;
  490. int nareas = Vect_get_num_areas(Map);
  491. struct ilist *List = Vect_new_list();
  492. nerrors = 0;
  493. for (area = 1; area <= nareas; area++) {
  494. if (!Vect_area_alive(Map, area))
  495. continue;
  496. line = Vect_get_area_centroid(Map, area);
  497. if (line != 0)
  498. continue; /* has centroid */
  499. Vect_get_area_boundaries(Map, area, List);
  500. for (i = 0; i < List->n_values; i++) {
  501. line = List->value[i];
  502. Vect_get_line_areas(Map, abs(line), &left, &right);
  503. if (line > 0)
  504. neighbour = left;
  505. else
  506. neighbour = right;
  507. if (neighbour < 0) {
  508. neighbour = Vect_get_isle_area(Map, abs(neighbour));
  509. if (!neighbour) {
  510. /* borders outer void */
  511. nerrors++;
  512. if (Err) {
  513. Vect_read_line(Map, Points, Cats, abs(line));
  514. Vect_write_line(Err, GV_BOUNDARY, Points, Cats);
  515. }
  516. }
  517. /* else neighbour is > 0, check below */
  518. }
  519. if (neighbour > 0) {
  520. if (Vect_get_area_centroid(Map, neighbour) == 0) {
  521. /* neighbouring area does not have a centroid either */
  522. nerrors++;
  523. if (Err) {
  524. Vect_read_line(Map, Points, Cats, abs(line));
  525. Vect_write_line(Err, GV_BOUNDARY, Points, Cats);
  526. }
  527. }
  528. }
  529. }
  530. }
  531. Vect_destroy_list(List);
  532. if (nerrors)
  533. G_warning(_("Number of redundant holes: %d"),
  534. nerrors);
  535. }
  536. /* what else ? */
  537. Vect_destroy_line_struct(Points);
  538. Vect_destroy_cats_struct(Cats);
  539. return 1;
  540. }
  541. /*!
  542. \brief Return current highest built level (part)
  543. \param Map vector map
  544. \return current highest built level
  545. */
  546. int Vect_get_built(const struct Map_info *Map)
  547. {
  548. return (Map->plus.built);
  549. }
  550. /*!
  551. \brief Build partial topology for vector map.
  552. Should only be used in special cases of vector processing.
  553. This functions optionally builds only some parts of
  554. topology. Highest level is specified by build parameter which may
  555. be:
  556. - GV_BUILD_NONE - nothing is build
  557. - GV_BUILD_BASE - basic topology, nodes, lines, spatial index;
  558. - GV_BUILD_AREAS - build areas and islands, but islands are not attached to areas;
  559. - GV_BUILD_ATTACH_ISLES - attach islands to areas;
  560. - GV_BUILD_CENTROIDS - assign centroids to areas;
  561. - GV_BUILD_ALL - top level, the same as GV_BUILD_CENTROIDS.
  562. If functions is called with build lower than current value of the
  563. Map, the level is downgraded to requested value.
  564. All calls to Vect_write_line(), Vect_rewrite_line(),
  565. Vect_delete_line() respect the last value of build used in this
  566. function.
  567. Note that the functions has effect only if requested level is
  568. higher than current level, to rebuild part of topology, call first
  569. downgrade and then upgrade, for example:
  570. - Vect_build()
  571. - Vect_build_partial(,GV_BUILD_BASE,)
  572. - Vect_build_partial(,GV_BUILD_AREAS,)
  573. \param Map vector map
  574. \param build highest level of build
  575. \return 1 on success
  576. \return 0 on error
  577. */
  578. int Vect_build_partial(struct Map_info *Map, int build)
  579. {
  580. struct Plus_head *plus;
  581. int ret;
  582. G_debug(3, "Vect_build(): build = %d", build);
  583. /* If topology is already build (map on level2), set level to 1 so
  584. * that lines will be read by V1_read_ (all lines) */
  585. Map->level = 1; /* may be not needed, because V1_read is used
  586. directly by Vect_build_ */
  587. if (Map->format != GV_FORMAT_OGR_DIRECT)
  588. Map->support_updated = TRUE;
  589. if (Map->plus.Spidx_built == FALSE)
  590. Vect_open_sidx(Map, 2);
  591. plus = &(Map->plus);
  592. if (build > GV_BUILD_NONE) {
  593. G_message(_("Building topology for vector map <%s>..."),
  594. Vect_get_full_name(Map));
  595. }
  596. plus->with_z = Map->head.with_z;
  597. plus->spidx_with_z = Map->head.with_z;
  598. if (build == GV_BUILD_ALL) {
  599. dig_cidx_free(plus); /* free old (if any) category index */
  600. dig_cidx_init(plus);
  601. }
  602. ret = ((*Build_array[Map->format]) (Map, build));
  603. if (ret == 0) {
  604. return 0;
  605. }
  606. if (build > GV_BUILD_NONE) {
  607. G_verbose_message(_("Topology was built"));
  608. }
  609. Map->level = LEVEL_2;
  610. plus->mode = GV_MODE_WRITE;
  611. if (build == GV_BUILD_ALL) {
  612. plus->cidx_up_to_date = 1; /* category index was build */
  613. dig_cidx_sort(plus);
  614. }
  615. if (build > GV_BUILD_NONE) {
  616. G_message(_("Number of nodes: %d"), plus->n_nodes);
  617. G_message(_("Number of primitives: %d"), plus->n_lines);
  618. G_message(_("Number of points: %d"), plus->n_plines);
  619. G_message(_("Number of lines: %d"), plus->n_llines);
  620. G_message(_("Number of boundaries: %d"), plus->n_blines);
  621. G_message(_("Number of centroids: %d"), plus->n_clines);
  622. if (plus->n_flines > 0)
  623. G_message(_("Number of faces: %d"), plus->n_flines);
  624. if (plus->n_klines > 0)
  625. G_message(_("Number of kernels: %d"), plus->n_klines);
  626. }
  627. if (plus->built >= GV_BUILD_AREAS) {
  628. int line, nlines, area, nareas, err_boundaries, err_centr_out,
  629. err_centr_dupl, err_nocentr;
  630. struct P_line *Line;
  631. struct Plus_head *Plus;
  632. /* Count errors (it does not take much time comparing to build process) */
  633. Plus = &(Map->plus);
  634. nlines = Vect_get_num_lines(Map);
  635. err_boundaries = err_centr_out = err_centr_dupl = 0;
  636. for (line = 1; line <= nlines; line++) {
  637. Line = Plus->Line[line];
  638. if (!Line)
  639. continue;
  640. if (Line->type == GV_BOUNDARY) {
  641. struct P_topo_b *topo = (struct P_topo_b *)Line->topo;
  642. if (topo->left == 0 || topo->right == 0) {
  643. G_debug(3, "line = %d left = %d right = %d", line,
  644. topo->left, topo->right);
  645. err_boundaries++;
  646. }
  647. }
  648. if (Line->type == GV_CENTROID) {
  649. struct P_topo_c *topo = (struct P_topo_c *)Line->topo;
  650. if (topo->area == 0)
  651. err_centr_out++;
  652. else if (topo->area < 0)
  653. err_centr_dupl++;
  654. }
  655. }
  656. err_nocentr = 0;
  657. nareas = Vect_get_num_areas(Map);
  658. for (area = 1; area <= nareas; area++) {
  659. if (!Vect_area_alive(Map, area))
  660. continue;
  661. line = Vect_get_area_centroid(Map, area);
  662. if (line == 0)
  663. err_nocentr++;
  664. }
  665. G_message(_("Number of areas: %d"), plus->n_areas);
  666. G_message(_("Number of isles: %d"), plus->n_isles);
  667. #if 0
  668. /* not an error, message disabled to avoid confusion */
  669. if (err_nocentr)
  670. G_message(_("Number of areas without centroid: %d"),
  671. err_nocentr);
  672. #endif
  673. if (plus->n_clines > plus->n_areas)
  674. G_warning(_("Number of centroids exceeds number of areas: %d > %d"),
  675. plus->n_clines, plus->n_areas);
  676. if (err_boundaries)
  677. G_warning(_("Number of incorrect boundaries: %d"),
  678. err_boundaries);
  679. if (err_centr_out)
  680. G_warning(_("Number of centroids outside area: %d"),
  681. err_centr_out);
  682. if (err_centr_dupl)
  683. G_warning(_("Number of duplicate centroids: %d"), err_centr_dupl);
  684. }
  685. else if (build > GV_BUILD_NONE) {
  686. G_message(_("Number of areas: -"));
  687. G_message(_("Number of isles: -"));
  688. }
  689. return 1;
  690. }
  691. /*!
  692. \brief Save topology file for vector map
  693. \param Map vector map
  694. \return 1 on success
  695. \return 0 on error
  696. */
  697. int Vect_save_topo(struct Map_info *Map)
  698. {
  699. struct Plus_head *plus;
  700. char fname[GPATH_MAX], buf[GPATH_MAX];
  701. struct gvfile fp;
  702. G_debug(1, "Vect_save_topo()");
  703. plus = &(Map->plus);
  704. /* write out all the accumulated info to the plus file */
  705. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  706. G_file_name(fname, buf, GV_TOPO_ELEMENT, Map->mapset);
  707. G_debug(1, "Open topo: %s", fname);
  708. dig_file_init(&fp);
  709. fp.file = fopen(fname, "w");
  710. if (fp.file == NULL) {
  711. G_warning(_("Unable to open topo file for write <%s>"), fname);
  712. return 0;
  713. }
  714. /* set portable info */
  715. dig_init_portable(&(plus->port), dig__byte_order_out());
  716. if (0 > dig_write_plus_file(&fp, plus)) {
  717. G_warning(_("Error writing out topo file"));
  718. return 0;
  719. }
  720. fclose(fp.file);
  721. return 1;
  722. }
  723. /*!
  724. \brief Dump topology to file
  725. \param Map vector map
  726. \param out file for output (stdout/stderr for example)
  727. \return 1 on success
  728. \return 0 on error
  729. */
  730. int Vect_topo_dump(const struct Map_info *Map, FILE *out)
  731. {
  732. int i, j, line, isle;
  733. float angle_deg;
  734. struct P_node *Node;
  735. struct P_line *Line;
  736. struct P_area *Area;
  737. struct P_isle *Isle;
  738. struct bound_box box;
  739. const struct Plus_head *plus;
  740. plus = &(Map->plus);
  741. fprintf(out, "---------- TOPOLOGY DUMP ----------\n");
  742. /* box */
  743. Vect_box_copy(&box, &(plus->box));
  744. fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", box.N, box.S,
  745. box.E, box.W, box.T, box.B);
  746. fprintf(out, SEP);
  747. /* nodes */
  748. fprintf(out, "Nodes (%d nodes, alive + dead):\n", plus->n_nodes);
  749. for (i = 1; i <= plus->n_nodes; i++) {
  750. if (plus->Node[i] == NULL) {
  751. continue;
  752. }
  753. Node = plus->Node[i];
  754. fprintf(out, "node = %d, n_lines = %d, xyz = %f, %f, %f\n", i,
  755. Node->n_lines, Node->x, Node->y, Node->z);
  756. for (j = 0; j < Node->n_lines; j++) {
  757. line = Node->lines[j];
  758. Line = plus->Line[abs(line)];
  759. angle_deg = (Node->angles[j] * 180) / M_PI;
  760. if (angle_deg < 0)
  761. angle_deg += 360;
  762. fprintf(out, " line = %3d, type = %d, angle = %f (%.4f)\n", line,
  763. Line->type, Node->angles[j], angle_deg);
  764. }
  765. }
  766. fprintf(out, SEP);
  767. /* lines */
  768. fprintf(out, "Lines (%d lines, alive + dead):\n", plus->n_lines);
  769. for (i = 1; i <= plus->n_lines; i++) {
  770. if (plus->Line[i] == NULL) {
  771. continue;
  772. }
  773. Line = plus->Line[i];
  774. if (Line->type == GV_POINT) {
  775. fprintf(out, "line = %d, type = %d, offset = %lu\n",
  776. i, Line->type, (unsigned long)Line->offset);
  777. }
  778. else if (Line->type == GV_CENTROID) {
  779. struct P_topo_c *topo = (struct P_topo_c *)Line->topo;
  780. fprintf(out, "line = %d, type = %d, offset = %lu, area = %d\n",
  781. i, Line->type, (unsigned long)Line->offset, topo->area);
  782. }
  783. else if (Line->type == GV_LINE) {
  784. struct P_topo_l *topo = (struct P_topo_l *)Line->topo;
  785. fprintf(out, "line = %d, type = %d, offset = %lu, n1 = %d, n2 = %d\n",
  786. i, Line->type, (unsigned long)Line->offset, topo->N1,
  787. topo->N2);
  788. }
  789. else if (Line->type == GV_BOUNDARY) {
  790. struct P_topo_b *topo = (struct P_topo_b *)Line->topo;
  791. fprintf(out, "line = %d, type = %d, offset = %lu, n1 = %d, n2 = %d, "
  792. "left = %d, right = %d\n",
  793. i, Line->type, (unsigned long)Line->offset, topo->N1,
  794. topo->N2, topo->left, topo->right);
  795. }
  796. else if (Line->type == GV_FACE) {
  797. struct P_topo_f *topo = (struct P_topo_f *)Line->topo;
  798. fprintf(out, "line = %d, type = %d, offset = %lu, e1 = %d, e2 = %d, "
  799. "e3 = %d, left = %d, right = %d\n",
  800. i, Line->type, (unsigned long)Line->offset, topo->E[0],
  801. topo->E[1], topo->E[2], topo->left, topo->right);
  802. }
  803. else if (Line->type == GV_KERNEL) {
  804. struct P_topo_k *topo = (struct P_topo_k *)Line->topo;
  805. fprintf(out, "line = %d, type = %d, offset = %lu, volume = %d",
  806. i, Line->type, (unsigned long)Line->offset, topo->volume);
  807. }
  808. }
  809. fprintf(out, SEP);
  810. /* areas */
  811. fprintf(out, "Areas (%d areas, alive + dead):\n", plus->n_areas);
  812. for (i = 1; i <= plus->n_areas; i++) {
  813. if (plus->Area[i] == NULL) {
  814. continue;
  815. }
  816. Area = plus->Area[i];
  817. fprintf(out, "area = %d, n_lines = %d, n_isles = %d centroid = %d\n",
  818. i, Area->n_lines, Area->n_isles, Area->centroid);
  819. for (j = 0; j < Area->n_lines; j++) {
  820. line = Area->lines[j];
  821. Line = plus->Line[abs(line)];
  822. fprintf(out, " line = %3d\n", line);
  823. }
  824. for (j = 0; j < Area->n_isles; j++) {
  825. isle = Area->isles[j];
  826. fprintf(out, " isle = %3d\n", isle);
  827. }
  828. }
  829. fprintf(out, SEP);
  830. /* isles */
  831. fprintf(out, "Islands (%d islands, alive + dead):\n", plus->n_isles);
  832. for (i = 1; i <= plus->n_isles; i++) {
  833. if (plus->Isle[i] == NULL) {
  834. continue;
  835. }
  836. Isle = plus->Isle[i];
  837. fprintf(out, "isle = %d, n_lines = %d area = %d\n", i, Isle->n_lines,
  838. Isle->area);
  839. for (j = 0; j < Isle->n_lines; j++) {
  840. line = Isle->lines[j];
  841. Line = plus->Line[abs(line)];
  842. fprintf(out, " line = %3d\n", line);
  843. }
  844. }
  845. return 1;
  846. }
  847. /*!
  848. \brief Create spatial index if necessary.
  849. To be used in modules.
  850. Map must be opened on level 2.
  851. \param[in,out] Map pointer to vector map
  852. \return 0 OK
  853. \return 1 error
  854. */
  855. int Vect_build_sidx(struct Map_info *Map)
  856. {
  857. if (Map->level < 2) {
  858. G_fatal_error(_("Unable to build spatial index from topology, "
  859. "vector map is not opened at topology level 2"));
  860. }
  861. if (!(Map->plus.Spidx_built)) {
  862. return (Vect_build_sidx_from_topo(Map));
  863. }
  864. return 0;
  865. }
  866. /*!
  867. \brief Create spatial index from topology if necessary (not longer
  868. supported)
  869. \param Map pointer to vector map
  870. \return 1
  871. */
  872. int Vect_build_sidx_from_topo(const struct Map_info *Map)
  873. {
  874. G_debug(3, "Vect_build_sidx_from_topo(): name=%s",
  875. Vect_get_full_name(Map));
  876. G_warning(_("%s is no longer supported"), "Vect_build_sidx_from_topo()");
  877. return 1;
  878. }
  879. /*!
  880. \brief Save spatial index file for vector map
  881. \param Map vector map
  882. \return 1 on success
  883. \return 0 on error
  884. */
  885. int Vect_save_sidx(struct Map_info *Map)
  886. {
  887. struct Plus_head *plus;
  888. char fname[GPATH_MAX], buf[GPATH_MAX];
  889. G_debug(1, "Vect_save_spatial_index()");
  890. plus = &(Map->plus);
  891. if (plus->Spidx_built == FALSE) {
  892. G_warning("Spatial index not available, can not be saved");
  893. return 0;
  894. }
  895. /* new or update mode ? */
  896. if (plus->Spidx_new == TRUE) {
  897. /* write out rtrees to sidx file */
  898. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  899. G_file_name(fname, buf, GV_SIDX_ELEMENT, Map->mapset);
  900. G_debug(1, "Open sidx: %s", fname);
  901. dig_file_init(&(plus->spidx_fp));
  902. plus->spidx_fp.file = fopen(fname, "w+");
  903. if (plus->spidx_fp.file == NULL) {
  904. G_warning(_("Unable open spatial index file for write <%s>"),
  905. fname);
  906. return 0;
  907. }
  908. /* set portable info */
  909. dig_init_portable(&(plus->spidx_port), dig__byte_order_out());
  910. if (0 > dig_Wr_spidx(&(plus->spidx_fp), plus)) {
  911. G_warning(_("Error writing out spatial index file"));
  912. return 0;
  913. }
  914. dig_spidx_free(plus);
  915. Map->plus.Spidx_new = FALSE;
  916. }
  917. fclose(Map->plus.spidx_fp.file);
  918. Map->plus.Spidx_built = FALSE;
  919. return 1;
  920. }
  921. /*!
  922. \brief Dump spatial index to file
  923. \param Map vector map
  924. \param out file for output (stdout/stderr for example)
  925. \return 1 on success
  926. \return 0 on error
  927. */
  928. int Vect_sidx_dump(const struct Map_info *Map, FILE * out)
  929. {
  930. if (!(Map->plus.Spidx_built)) {
  931. Vect_build_sidx_from_topo(Map);
  932. }
  933. fprintf(out, "---------- SPATIAL INDEX DUMP ----------\n");
  934. dig_dump_spidx(out, &(Map->plus));
  935. return 1;
  936. }