build_nat.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*!
  2. \file build_nat.c
  3. \brief Vector library - Building topology for native format
  4. (C) 2001-2008 by the GRASS Development Team
  5. This program is free software under the
  6. GNU General Public License (>=v2).
  7. Read the file COPYING that comes with GRASS
  8. for details.
  9. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  10. Update to GRASS 5.7 Radim Blazek and David D. Gray.
  11. \date 2001-2008
  12. */
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <grass/glocale.h>
  17. #include <grass/gis.h>
  18. #include <grass/Vect.h>
  19. /*!
  20. \brief Build area on given side of line (GV_LEFT or GV_RIGHT)
  21. \param Map_info vector map
  22. \param iline line id
  23. \param side side (GV_LEFT or GV_RIGHT)
  24. \return > 0 number of area
  25. \return < 0 number of isle
  26. \return 0 not created (may also already exist)
  27. */
  28. int Vect_build_line_area(struct Map_info *Map, int iline, int side)
  29. {
  30. int j, area, isle, n_lines, line, type, direction;
  31. static int first = 1;
  32. long offset;
  33. struct Plus_head *plus;
  34. P_LINE *BLine;
  35. static struct line_pnts *Points, *APoints;
  36. plus_t *lines;
  37. double area_size;
  38. plus = &(Map->plus);
  39. G_debug(3, "Vect_build_line_area() line = %d, side = %d", iline, side);
  40. if (first) {
  41. Points = Vect_new_line_struct();
  42. APoints = Vect_new_line_struct();
  43. first = 0;
  44. }
  45. area = dig_line_get_area(plus, iline, side);
  46. if (area != 0) {
  47. G_debug(3, " area/isle = %d -> skip", area);
  48. return 0;
  49. }
  50. n_lines = dig_build_area_with_line(plus, iline, side, &lines);
  51. G_debug(3, " n_lines = %d", n_lines);
  52. if (n_lines < 1) {
  53. return 0;
  54. } /* area was not built */
  55. /* Area or island ? */
  56. Vect_reset_line(APoints);
  57. for (j = 0; j < n_lines; j++) {
  58. line = abs(lines[j]);
  59. BLine = plus->Line[line];
  60. offset = BLine->offset;
  61. G_debug(3, " line[%d] = %d, offset = %ld", j, line, offset);
  62. type = Vect_read_line(Map, Points, NULL, line);
  63. if (lines[j] > 0)
  64. direction = GV_FORWARD;
  65. else
  66. direction = GV_BACKWARD;
  67. Vect_append_points(APoints, Points, direction);
  68. }
  69. dig_find_area_poly(APoints, &area_size);
  70. G_debug(3, " area/isle size = %f", area_size);
  71. if (area_size > 0) { /* area */
  72. /* add area structure to plus */
  73. area = dig_add_area(plus, n_lines, lines);
  74. if (area == -1) { /* error */
  75. Vect_close(Map);
  76. G_fatal_error(_("Unable to add area (map closed, topo saved)"));
  77. }
  78. G_debug(3, " -> area %d", area);
  79. return area;
  80. }
  81. else if (area_size < 0) { /* island */
  82. isle = dig_add_isle(plus, n_lines, lines);
  83. if (isle == -1) { /* error */
  84. Vect_close(Map);
  85. G_fatal_error(_("Unable to add isle (map closed, topo saved)"));
  86. }
  87. G_debug(3, " -> isle %d", isle);
  88. return -isle;
  89. }
  90. else {
  91. /* TODO: What to do with such areas? Should be areas/isles of size 0 stored,
  92. * so that may be found and cleaned by some utility
  93. * Note: it would be useful for vertical closed polygons, but such would be added twice
  94. * as area */
  95. G_warning(_("Area of size = 0.0 ignored"));
  96. }
  97. return 0;
  98. }
  99. /*!
  100. \brief Find area outside island
  101. \param Map_info vector map
  102. \param isle isle id
  103. \return number of area(s)
  104. \return 0 if not found
  105. */
  106. int Vect_isle_find_area(struct Map_info *Map, int isle)
  107. {
  108. int j, line, node, sel_area, first, area, poly;
  109. static int first_call = 1;
  110. struct Plus_head *plus;
  111. P_LINE *Line;
  112. P_NODE *Node;
  113. P_ISLE *Isle;
  114. P_AREA *Area;
  115. double size, cur_size;
  116. BOUND_BOX box, abox;
  117. static struct ilist *List;
  118. static struct line_pnts *APoints;
  119. /* Note: We should check all isle points (at least) because if topology is not clean
  120. * and two areas overlap, isle which is not completely within area may be attached,
  121. * but it would take long time */
  122. G_debug(3, "Vect_isle_find_area () island = %d", isle);
  123. plus = &(Map->plus);
  124. if (plus->Isle[isle] == NULL) {
  125. G_warning(_("Request to find area outside nonexistent isle"));
  126. return 0;
  127. }
  128. if (first_call) {
  129. List = Vect_new_list();
  130. APoints = Vect_new_line_struct();
  131. first_call = 0;
  132. }
  133. Isle = plus->Isle[isle];
  134. line = abs(Isle->lines[0]);
  135. Line = plus->Line[line];
  136. node = Line->N1;
  137. Node = plus->Node[node];
  138. /* select areas by box */
  139. box.E = Node->x;
  140. box.W = Node->x;
  141. box.N = Node->y;
  142. box.S = Node->y;
  143. box.T = PORT_DOUBLE_MAX;
  144. box.B = -PORT_DOUBLE_MAX;
  145. Vect_select_areas_by_box(Map, &box, List);
  146. G_debug(3, "%d areas overlap island boundary point", List->n_values);
  147. sel_area = 0;
  148. cur_size = -1;
  149. first = 1;
  150. Vect_get_isle_box(Map, isle, &box);
  151. for (j = 0; j < List->n_values; j++) {
  152. area = List->value[j];
  153. G_debug(3, "area = %d", area);
  154. Area = plus->Area[area];
  155. /* Before other tests, simply exclude those areas inside isolated isles formed by one boundary */
  156. if (abs(Isle->lines[0]) == abs(Area->lines[0])) {
  157. G_debug(3, " area inside isolated isle");
  158. continue;
  159. }
  160. /* Check box */
  161. /* Note: If build is run on large files of areas imported from nontopo format (shapefile)
  162. * attaching of isles takes very long time because each area is also isle and select by
  163. * box all overlapping areas selects all areas with box overlapping first node.
  164. * Then reading coordinates for all those areas would take a long time -> check first
  165. * if isle's box is completely within area box */
  166. Vect_get_area_box(Map, area, &abox);
  167. if (box.E > abox.E || box.W < abox.W || box.N > abox.N ||
  168. box.S < abox.S) {
  169. G_debug(3, " isle not completely inside area box");
  170. continue;
  171. }
  172. poly = Vect_point_in_area_outer_ring(Node->x, Node->y, Map, area);
  173. G_debug(3, " poly = %d", poly);
  174. if (poly == 1) { /* pint in area, but node is not part of area inside isle (would be poly == 2) */
  175. /* In rare case island is inside more areas in that case we have to calculate area
  176. * of outer ring and take the smaller */
  177. if (sel_area == 0) { /* first */
  178. sel_area = area;
  179. }
  180. else { /* is not first */
  181. if (cur_size < 0) { /* second area */
  182. /* This is slow, but should not be called often */
  183. Vect_get_area_points(Map, sel_area, APoints);
  184. G_begin_polygon_area_calculations();
  185. cur_size =
  186. G_area_of_polygon(APoints->x, APoints->y,
  187. APoints->n_points);
  188. G_debug(3, " first area size = %f (n points = %d)",
  189. cur_size, APoints->n_points);
  190. }
  191. Vect_get_area_points(Map, area, APoints);
  192. size =
  193. G_area_of_polygon(APoints->x, APoints->y,
  194. APoints->n_points);
  195. G_debug(3, " area size = %f (n points = %d)", cur_size,
  196. APoints->n_points);
  197. if (size < cur_size) {
  198. sel_area = area;
  199. cur_size = size;
  200. }
  201. }
  202. G_debug(3, "sel_area = %d cur_size = %f", sel_area, cur_size);
  203. }
  204. }
  205. if (sel_area > 0) {
  206. G_debug(3, "Island %d in area %d", isle, sel_area);
  207. }
  208. else {
  209. G_debug(3, "Island %d is not in area", isle);
  210. }
  211. return sel_area;
  212. }
  213. /*!
  214. \brief (Re)Attach isle to area
  215. \param Map_info vector map
  216. \param isle isle id
  217. \return 0
  218. */
  219. int Vect_attach_isle(struct Map_info *Map, int isle)
  220. {
  221. int sel_area;
  222. P_ISLE *Isle;
  223. struct Plus_head *plus;
  224. /* Note!: If topology is not clean and areas overlap, one island may fall to more areas
  225. * (partially or fully). Before isle is attached to area it must be check if it is not attached yet */
  226. G_debug(3, "Vect_attach_isle (): isle = %d", isle);
  227. plus = &(Map->plus);
  228. sel_area = Vect_isle_find_area(Map, isle);
  229. G_debug(3, " isle = %d -> area outside = %d", isle, sel_area);
  230. if (sel_area > 0) {
  231. Isle = plus->Isle[isle];
  232. if (Isle->area > 0) {
  233. G_debug(3,
  234. "Attempt to attach isle %d to more areas (=>topology is not clean)",
  235. isle);
  236. }
  237. else {
  238. Isle->area = sel_area;
  239. dig_area_add_isle(plus, sel_area, isle);
  240. }
  241. }
  242. return 0;
  243. }
  244. /*!
  245. \brief (Re)Attach isles to areas in given box
  246. \param Map_info vector map
  247. \param box bounding box
  248. \return 0
  249. */
  250. int Vect_attach_isles(struct Map_info *Map, BOUND_BOX * box)
  251. {
  252. int i, isle;
  253. static int first = 1;
  254. static struct ilist *List;
  255. struct Plus_head *plus;
  256. G_debug(3, "Vect_attach_isles ()");
  257. plus = &(Map->plus);
  258. if (first) {
  259. List = Vect_new_list();
  260. first = 0;
  261. }
  262. Vect_select_isles_by_box(Map, box, List);
  263. G_debug(3, " number of isles to attach = %d", List->n_values);
  264. for (i = 0; i < List->n_values; i++) {
  265. isle = List->value[i];
  266. Vect_attach_isle(Map, isle);
  267. }
  268. return 0;
  269. }
  270. /*!
  271. \brief (Re)Attach centroids to areas in given box
  272. \param Map_info vector map
  273. \param box bounding box
  274. \return 0
  275. */
  276. int Vect_attach_centroids(struct Map_info *Map, BOUND_BOX * box)
  277. {
  278. int i, sel_area, centr;
  279. static int first = 1;
  280. static struct ilist *List;
  281. P_AREA *Area;
  282. P_NODE *Node;
  283. P_LINE *Line;
  284. struct Plus_head *plus;
  285. G_debug(3, "Vect_attach_centroids ()");
  286. plus = &(Map->plus);
  287. if (first) {
  288. List = Vect_new_list();
  289. first = 0;
  290. }
  291. /* Warning: If map is updated on level2, it may happen that previously correct island
  292. * becomes incorrect. In that case, centroid of area forming the island is reattached
  293. * to outer area, because island polygon is not excluded.
  294. *
  295. * +-----------+ +-----------+
  296. * | 1 | | 1 |
  297. * | +---+---+ | | +---+---+ |
  298. * | | 2 | 3 | | | | 2 | |
  299. * | | x | | | -> | | x | |
  300. * | | | | | | | | |
  301. * | +---+---+ | | +---+---+ |
  302. * | | | |
  303. * +-----------+ +-----------+
  304. * centroid is centroid is
  305. * attached to 2 reattached to 1
  306. *
  307. * Because of this, when the centroid is reattached to another area, it is always necessary
  308. * to check if original area exist, unregister centroid from previous area.
  309. * To simplify code, this is implemented so that centroid is always firs unregistered
  310. * and if new area is found, it is registered again.
  311. */
  312. Vect_select_lines_by_box(Map, box, GV_CENTROID, List);
  313. G_debug(3, " number of centroids to reattach = %d", List->n_values);
  314. for (i = 0; i < List->n_values; i++) {
  315. int orig_area;
  316. centr = List->value[i];
  317. Line = plus->Line[centr];
  318. Node = plus->Node[Line->N1];
  319. /* Unregister centroid */
  320. orig_area = Line->left;
  321. if (orig_area > 0) {
  322. if (plus->Area[orig_area] != NULL) {
  323. plus->Area[orig_area]->centroid = 0;
  324. }
  325. }
  326. Line->left = 0;
  327. sel_area = Vect_find_area(Map, Node->x, Node->y);
  328. G_debug(3, " centroid %d is in area %d", centr, sel_area);
  329. if (sel_area > 0) {
  330. Area = plus->Area[sel_area];
  331. if (Area->centroid == 0) { /* first centroid */
  332. G_debug(3, " first centroid -> attach to area");
  333. Area->centroid = centr;
  334. Line->left = sel_area;
  335. }
  336. else if (Area->centroid != centr) { /* duplicate centroid */
  337. /* Note: it cannot happen that Area->centroid == centr, because the centroid
  338. * was previously unregistered */
  339. G_debug(3, " duplicate centroid -> do not attach to area");
  340. Line->left = -sel_area;
  341. }
  342. }
  343. if (sel_area != orig_area && plus->do_uplist)
  344. dig_line_add_updated(plus, centr);
  345. }
  346. return 0;
  347. }
  348. /*!
  349. \brief Build topology
  350. \param Map_info vector map
  351. \param build build level
  352. \return 1 on success
  353. \return 0 on error
  354. */
  355. int Vect_build_nat(struct Map_info *Map, int build)
  356. {
  357. struct Plus_head *plus;
  358. int i, s, type, lineid;
  359. long offset;
  360. int side, line, area;
  361. struct line_pnts *Points, *APoints;
  362. struct line_cats *Cats;
  363. P_LINE *Line;
  364. P_NODE *Node;
  365. P_AREA *Area;
  366. BOUND_BOX box;
  367. struct ilist *List;
  368. G_debug(3, "Vect_build_nat() build = %d", build);
  369. plus = &(Map->plus);
  370. if (build == plus->built)
  371. return 1; /* Do nothing */
  372. /* Check if upgrade or downgrade */
  373. if (build < plus->built) { /* lower level request */
  374. /* release old sources (this also initializes structures and numbers of elements) */
  375. if (plus->built >= GV_BUILD_CENTROIDS && build < GV_BUILD_CENTROIDS) {
  376. /* reset info about areas stored for centroids */
  377. int nlines = Vect_get_num_lines(Map);
  378. for (line = 1; line <= nlines; line++) {
  379. Line = plus->Line[line];
  380. if (Line && Line->type == GV_CENTROID)
  381. Line->left = 0;
  382. }
  383. dig_free_plus_areas(plus);
  384. dig_spidx_free_areas(plus);
  385. dig_free_plus_isles(plus);
  386. dig_spidx_free_isles(plus);
  387. }
  388. if (plus->built >= GV_BUILD_AREAS && build < GV_BUILD_AREAS) {
  389. /* reset info about areas stored for lines */
  390. int nlines = Vect_get_num_lines(Map);
  391. for (line = 1; line <= nlines; line++) {
  392. Line = plus->Line[line];
  393. if (Line && Line->type == GV_BOUNDARY) {
  394. Line->left = 0;
  395. Line->right = 0;
  396. }
  397. }
  398. dig_free_plus_areas(plus);
  399. dig_spidx_free_areas(plus);
  400. dig_free_plus_isles(plus);
  401. dig_spidx_free_isles(plus);
  402. }
  403. if (plus->built >= GV_BUILD_BASE && build < GV_BUILD_BASE) {
  404. dig_free_plus_nodes(plus);
  405. dig_spidx_free_nodes(plus);
  406. dig_free_plus_lines(plus);
  407. dig_spidx_free_lines(plus);
  408. }
  409. plus->built = build;
  410. return 1;
  411. }
  412. Points = Vect_new_line_struct();
  413. APoints = Vect_new_line_struct();
  414. Cats = Vect_new_cats_struct();
  415. List = Vect_new_list();
  416. if (plus->built < GV_BUILD_BASE) {
  417. int npoints;
  418. /*
  419. * We shall go through all primitives in coor file and
  420. * add new node for each end point to nodes structure
  421. * if the node with the same coordinates doesn't exist yet.
  422. */
  423. /* register lines, create nodes */
  424. Vect_rewind(Map);
  425. G_message(_("Registering primitives..."));
  426. i = 1;
  427. npoints = 0;
  428. while (1) {
  429. /* register line */
  430. type = Vect_read_next_line(Map, Points, Cats);
  431. /* Note: check for dead lines is not needed, because they are skipped by V1_read_next_line_nat() */
  432. if (type == -1) {
  433. G_warning(_("Unable to read vector map"));
  434. return 0;
  435. }
  436. else if (type == -2) {
  437. break;
  438. }
  439. npoints += Points->n_points;
  440. offset = Map->head.last_offset;
  441. G_debug(3, "Register line: offset = %ld", offset);
  442. lineid = dig_add_line(plus, type, Points, offset);
  443. dig_line_box(Points, &box);
  444. if (lineid == 1)
  445. Vect_box_copy(&(plus->box), &box);
  446. else
  447. Vect_box_extend(&(plus->box), &box);
  448. /* Add all categories to category index */
  449. if (build == GV_BUILD_ALL) {
  450. int c;
  451. for (c = 0; c < Cats->n_cats; c++) {
  452. dig_cidx_add_cat(plus, Cats->field[c], Cats->cat[c],
  453. lineid, type);
  454. }
  455. if (Cats->n_cats == 0) /* add field 0, cat 0 */
  456. dig_cidx_add_cat(plus, 0, 0, lineid, type);
  457. }
  458. if (G_verbose() > G_verbose_min() && i % 1000 == 0) {
  459. fprintf(stderr, "%7d\b\b\b\b\b\b\b", i);
  460. }
  461. i++;
  462. }
  463. if (G_verbose() > G_verbose_min())
  464. fprintf(stderr, "\r");
  465. G_message(_("%d primitives registered"), plus->n_lines);
  466. G_message(_("%d vertices registered"), npoints);
  467. plus->built = GV_BUILD_BASE;
  468. }
  469. if (build < GV_BUILD_AREAS)
  470. return 1;
  471. if (plus->built < GV_BUILD_AREAS) {
  472. /* Build areas */
  473. /* Go through all bundaries and try to build area for both sides */
  474. G_message(_("Building areas..."));
  475. for (i = 1; i <= plus->n_lines; i++) {
  476. G_percent(i, plus->n_lines, 1);
  477. /* build */
  478. if (plus->Line[i] == NULL) {
  479. continue;
  480. } /* dead line */
  481. Line = plus->Line[i];
  482. if (Line->type != GV_BOUNDARY) {
  483. continue;
  484. }
  485. for (s = 0; s < 2; s++) {
  486. if (s == 0)
  487. side = GV_LEFT;
  488. else
  489. side = GV_RIGHT;
  490. G_debug(3, "Build area for line = %d, side = %d", i, side);
  491. Vect_build_line_area(Map, i, side);
  492. }
  493. }
  494. G_message(_("%d areas built"), plus->n_areas);
  495. G_message(_("%d isles built"), plus->n_isles);
  496. plus->built = GV_BUILD_AREAS;
  497. }
  498. if (build < GV_BUILD_ATTACH_ISLES)
  499. return 1;
  500. /* Attach isles to areas */
  501. if (plus->built < GV_BUILD_ATTACH_ISLES) {
  502. G_message(_("Attaching islands..."));
  503. for (i = 1; i <= plus->n_isles; i++) {
  504. G_percent(i, plus->n_isles, 1);
  505. Vect_attach_isle(Map, i);
  506. }
  507. plus->built = GV_BUILD_ATTACH_ISLES;
  508. }
  509. if (build < GV_BUILD_CENTROIDS)
  510. return 1;
  511. /* Attach centroids to areas */
  512. if (plus->built < GV_BUILD_CENTROIDS) {
  513. int nlines;
  514. G_message(_("Attaching centroids..."));
  515. nlines = Vect_get_num_lines(Map);
  516. for (line = 1; line <= nlines; line++) {
  517. G_percent(line, nlines, 1);
  518. Line = plus->Line[line];
  519. if (!Line)
  520. continue; /* Dead */
  521. if (Line->type != GV_CENTROID)
  522. continue;
  523. Node = plus->Node[Line->N1];
  524. area = Vect_find_area(Map, Node->x, Node->y);
  525. if (area > 0) {
  526. G_debug(3, "Centroid (line=%d) in area %d", line, area);
  527. Area = plus->Area[area];
  528. if (Area->centroid == 0) { /* first */
  529. Area->centroid = line;
  530. Line->left = area;
  531. }
  532. else { /* duplicate */
  533. Line->left = -area;
  534. }
  535. }
  536. }
  537. plus->built = GV_BUILD_CENTROIDS;
  538. }
  539. /* Add areas to category index */
  540. for (area = 1; area <= plus->n_areas; area++) {
  541. int c;
  542. if (plus->Area[area] == NULL)
  543. continue;
  544. if (plus->Area[area]->centroid > 0) {
  545. Vect_read_line(Map, NULL, Cats, plus->Area[area]->centroid);
  546. for (c = 0; c < Cats->n_cats; c++) {
  547. dig_cidx_add_cat(plus, Cats->field[c], Cats->cat[c], area,
  548. GV_AREA);
  549. }
  550. }
  551. if (plus->Area[area]->centroid == 0 || Cats->n_cats == 0) /* no centroid or no cats */
  552. dig_cidx_add_cat(plus, 0, 0, area, GV_AREA);
  553. }
  554. return 1;
  555. }