build.c 30 KB

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