build.c 34 KB

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