build_nat.c 18 KB

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