build.c 34 KB

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