build.c 34 KB

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