build.c 34 KB

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