build.c 34 KB

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