build_nat.c 18 KB

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