write_nat.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*!
  2. \file write_nat.c
  3. \brief Vector library - write data
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  11. Update to GRASS 5.7 Radim Blazek and David D. Gray.
  12. \date 2001
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <math.h>
  17. #include <grass/gis.h>
  18. #include <grass/Vect.h>
  19. #include <grass/glocale.h>
  20. static void delete_area_cats_from_cidx(struct Map_info *Map, int area)
  21. {
  22. int i;
  23. P_AREA *Area;
  24. static struct line_cats *Cats = NULL;
  25. G_debug(3, "delete_area_cats_from_cidx() area = %d", area);
  26. Area = Map->plus.Area[area];
  27. if (!Area)
  28. G_fatal_error(_("BUG (delete_area_cats_from_cidx): Area %d does not exist"),
  29. area);
  30. if (Area->centroid == 0)
  31. return;
  32. if (!Cats)
  33. Cats = Vect_new_cats_struct();
  34. V2_read_line_nat(Map, NULL, Cats, Area->centroid);
  35. for (i = 0; i < Cats->n_cats; i++) {
  36. dig_cidx_del_cat(&(Map->plus), Cats->field[i], Cats->cat[i], area,
  37. GV_AREA);
  38. }
  39. }
  40. static void add_area_cats_to_cidx(struct Map_info *Map, int area)
  41. {
  42. int i;
  43. P_AREA *Area;
  44. static struct line_cats *Cats = NULL;
  45. G_debug(3, "add_area_cats_to_cidx() area = %d", area);
  46. Area = Map->plus.Area[area];
  47. if (!Area)
  48. G_fatal_error(_("BUG (add_area_cats_to_cidx): Area %d does not exist"),
  49. area);
  50. if (Area->centroid == 0)
  51. return;
  52. if (!Cats)
  53. Cats = Vect_new_cats_struct();
  54. V2_read_line_nat(Map, NULL, Cats, Area->centroid);
  55. for (i = 0; i < Cats->n_cats; i++) {
  56. dig_cidx_add_cat_sorted(&(Map->plus), Cats->field[i], Cats->cat[i],
  57. area, GV_AREA);
  58. }
  59. }
  60. long V1__rewrite_line_nat(struct Map_info *Map, long offset, int type,
  61. struct line_pnts *points, struct line_cats *cats);
  62. /**
  63. \brief Writes line to 'coor' file
  64. \param Map pointer to vector map
  65. \param type feature type
  66. \param points line geometry
  67. \param cats line cats
  68. \return offset into file
  69. \return -1 on error
  70. */
  71. long
  72. V1_write_line_nat(struct Map_info *Map,
  73. int type, struct line_pnts *points, struct line_cats *cats)
  74. {
  75. long offset;
  76. if (dig_fseek(&(Map->dig_fp), 0L, SEEK_END) == -1) /* set to end of file */
  77. return -1;
  78. offset = dig_ftell(&(Map->dig_fp));
  79. if (offset == -1)
  80. return -1;
  81. return V1__rewrite_line_nat(Map, offset, type, points, cats);
  82. }
  83. /**
  84. \brief Writes line to 'coor' file.
  85. \param Map pointer to vector map
  86. \param type feature type
  87. \param points line geometry
  88. \param cats line cats
  89. \return number of new line
  90. \return -1 on error
  91. */
  92. long
  93. V2_write_line_nat(struct Map_info *Map,
  94. int type, struct line_pnts *points, struct line_cats *cats)
  95. {
  96. int i, s, n, line = 0, next_line, area = 0, node, side, first, sel_area;
  97. int new_area[2];
  98. long offset;
  99. struct Plus_head *plus;
  100. BOUND_BOX box, abox;
  101. P_LINE *Line, *NLine;
  102. P_AREA *Area;
  103. P_NODE *Node;
  104. G_debug(3, "V2_write_line_nat()");
  105. offset = V1_write_line_nat(Map, type, points, cats);
  106. if (offset < 0)
  107. return -1;
  108. /* Update topology */
  109. plus = &(Map->plus);
  110. /* Add line */
  111. if (plus->built >= GV_BUILD_BASE) {
  112. line = dig_add_line(plus, type, points, offset);
  113. G_debug(3, " line added to topo with id = %d", line);
  114. dig_line_box(points, &box);
  115. dig_line_set_box(plus, line, &box);
  116. if (line == 1)
  117. Vect_box_copy(&(plus->box), &box);
  118. else
  119. Vect_box_extend(&(plus->box), &box);
  120. }
  121. /* Update areas. Areas are modified if:
  122. * 1) first or/and last point are existing nodes ->
  123. * - drop areas/islands whose boundaries are neighbour to this boundary at these nodes
  124. * - try build areas and islands for this boundary and neighbour boundaries going through these nodes
  125. * Question: may be by adding line created new area/isle which doesn't go through nodes of this line
  126. * old new line
  127. * +----+----+ +----+----+ +----+----+
  128. * | A1 | A2 | + / -> | A1 | /| or + \ -> | A1 | A2 |\
  129. * | | | | | | | | |
  130. * +----+----+ +----+----+ +----+----+
  131. * I1 I1 I1 I1
  132. *
  133. * - reattache all centroids/isles inside new area(s)
  134. * - attach new isle to area outside
  135. * 2) line is closed ring (node at the end is new, so it is not case above)
  136. * - build new area/isle
  137. * - check if it is island or contains island(s)
  138. * - reattache all centroids/isles inside new area(s)
  139. * - attach new isle to area outside
  140. *
  141. * Note that 1) and 2) is done by the same code.
  142. */
  143. if (plus->built >= GV_BUILD_AREAS) {
  144. if (type == GV_BOUNDARY) {
  145. Line = plus->Line[line];
  146. /* Delete neighbour areas/isles */
  147. first = 1;
  148. for (s = 1; s < 3; s++) { /* for each node */
  149. if (s == 1)
  150. node = Line->N1; /* Node 1 */
  151. else
  152. node = Line->N2;
  153. G_debug(3,
  154. " delete neighbour areas/iseles: side = %d node = %d",
  155. s, node);
  156. Node = plus->Node[node];
  157. n = 0;
  158. for (i = 0; i < Node->n_lines; i++) {
  159. NLine = plus->Line[abs(Node->lines[i])];
  160. if (NLine->type == GV_BOUNDARY)
  161. n++;
  162. }
  163. G_debug(3, " number of boundaries at node = %d", n);
  164. if (n > 2) { /* more than 2 boundaries at node ( >= 2 old + 1 new ) */
  165. /* Line above (to the right), it is enough to check to the right, because if area/isle
  166. * exists it is the same to the left */
  167. if (s == 1)
  168. next_line =
  169. dig_angle_next_line(plus, line, GV_RIGHT,
  170. GV_BOUNDARY);
  171. else
  172. next_line =
  173. dig_angle_next_line(plus, -line, GV_RIGHT,
  174. GV_BOUNDARY);
  175. if (next_line != 0) { /* there is a boundary to the right */
  176. NLine = plus->Line[abs(next_line)];
  177. if (next_line > 0) /* the boundary is connected by 1. node */
  178. area = NLine->right; /* we are interested just in this side (close to our line) */
  179. else if (next_line < 0) /* the boundary is connected by 2. node */
  180. area = NLine->left;
  181. G_debug(3, " next_line = %d area = %d", next_line,
  182. area);
  183. if (area > 0) { /* is area */
  184. Vect_get_area_box(Map, area, &box);
  185. if (first) {
  186. Vect_box_copy(&abox, &box);
  187. first = 0;
  188. }
  189. else
  190. Vect_box_extend(&abox, &box);
  191. if (plus->update_cidx) {
  192. delete_area_cats_from_cidx(Map, area);
  193. }
  194. dig_del_area(plus, area);
  195. }
  196. else if (area < 0) { /* is isle */
  197. dig_del_isle(plus, -area);
  198. }
  199. }
  200. }
  201. }
  202. /* Build new areas/isles. Thas true that we deleted also adjacent areas/isles, but if
  203. * they form new one our boundary must participate, so we need to build areas/isles
  204. * just for our boundary */
  205. for (s = 1; s < 3; s++) {
  206. if (s == 1)
  207. side = GV_LEFT;
  208. else
  209. side = GV_RIGHT;
  210. G_debug(3, " build area/isle on side = %d", side);
  211. G_debug(3, "Build area for line = %d, side = %d", line, side);
  212. area = Vect_build_line_area(Map, line, side);
  213. G_debug(3, "Build area for line = %d, side = %d", line, side);
  214. if (area > 0) { /* area */
  215. Vect_get_area_box(Map, area, &box);
  216. if (first) {
  217. Vect_box_copy(&abox, &box);
  218. first = 0;
  219. }
  220. else
  221. Vect_box_extend(&abox, &box);
  222. }
  223. else if (area < 0) {
  224. /* isle -> must be attached -> add to abox */
  225. Vect_get_isle_box(Map, -area, &box);
  226. if (first) {
  227. Vect_box_copy(&abox, &box);
  228. first = 0;
  229. }
  230. else
  231. Vect_box_extend(&abox, &box);
  232. }
  233. new_area[s - 1] = area;
  234. }
  235. /* Reattach all centroids/isles in deleted areas + new area.
  236. * Because isles are selected by box it covers also possible new isle created above */
  237. if (!first) { /* i.e. old area/isle was deleted or new one created */
  238. /* Reattache isles */
  239. if (plus->built >= GV_BUILD_ATTACH_ISLES)
  240. Vect_attach_isles(Map, &abox);
  241. /* Reattach centroids */
  242. if (plus->built >= GV_BUILD_CENTROIDS)
  243. Vect_attach_centroids(Map, &abox);
  244. }
  245. /* Add to category index */
  246. if (plus->update_cidx) {
  247. for (s = 1; s < 3; s++) {
  248. if (new_area[s - 1] > 0) {
  249. add_area_cats_to_cidx(Map, new_area[s - 1]);
  250. }
  251. }
  252. }
  253. }
  254. }
  255. /* Attach centroid */
  256. if (plus->built >= GV_BUILD_CENTROIDS) {
  257. if (type == GV_CENTROID) {
  258. sel_area = Vect_find_area(Map, points->x[0], points->y[0]);
  259. G_debug(3, " new centroid %d is in area %d", line, sel_area);
  260. if (sel_area > 0) {
  261. Area = plus->Area[sel_area];
  262. Line = plus->Line[line];
  263. if (Area->centroid == 0) { /* first centroid */
  264. G_debug(3, " first centroid -> attach to area");
  265. Area->centroid = line;
  266. Line->left = sel_area;
  267. if (plus->update_cidx) {
  268. add_area_cats_to_cidx(Map, sel_area);
  269. }
  270. }
  271. else { /* duplicate centroid */
  272. G_debug(3,
  273. " duplicate centroid -> do not attach to area");
  274. Line->left = -sel_area;
  275. }
  276. }
  277. }
  278. }
  279. /* Add cetegory index */
  280. for (i = 0; i < cats->n_cats; i++) {
  281. dig_cidx_add_cat_sorted(plus, cats->field[i], cats->cat[i], line,
  282. type);
  283. }
  284. G_debug(3, "updated lines : %d , updated nodes : %d", plus->n_uplines,
  285. plus->n_upnodes);
  286. return line;
  287. }
  288. /**
  289. \brief Rewrites line at the given offset.
  290. If the number of points or cats differs from
  291. the original one or the type is changed:
  292. GV_POINTS -> GV_LINES or GV_LINES -> GV_POINTS,
  293. the old one is deleted and the
  294. new is appended to the end of the file.
  295. Old line is deleted (marked as dead), new line written.
  296. \param Map pointer to vector map
  297. \param offset line offset
  298. \param type feature type
  299. \param points line geometry
  300. \param cats line cats
  301. \return line offset (rewriten line)
  302. \return -1 on error
  303. */
  304. long
  305. V1_rewrite_line_nat(struct Map_info *Map,
  306. long offset,
  307. int type,
  308. struct line_pnts *points, struct line_cats *cats)
  309. {
  310. int old_type;
  311. struct line_pnts *old_points;
  312. struct line_cats *old_cats;
  313. long new_offset;
  314. /* TODO: enable points and cats == NULL */
  315. /* First compare numbers of points and cats with tha old one */
  316. old_points = Vect_new_line_struct();
  317. old_cats = Vect_new_cats_struct();
  318. old_type = V1_read_line_nat(Map, old_points, old_cats, offset);
  319. if (old_type == -1)
  320. return (-1); /* error */
  321. if (old_type != -2 /* EOF -> write new line */
  322. && points->n_points == old_points->n_points
  323. && cats->n_cats == old_cats->n_cats
  324. && (((type & GV_POINTS) && (old_type & GV_POINTS))
  325. || ((type & GV_LINES) && (old_type & GV_LINES)))) {
  326. /* equal -> overwrite the old */
  327. return V1__rewrite_line_nat(Map, offset, type, points, cats);
  328. }
  329. else {
  330. /* differ -> delete the old and append new */
  331. /* delete old */
  332. V1_delete_line_nat(Map, offset);
  333. /* write new */
  334. if (dig_fseek(&(Map->dig_fp), 0L, SEEK_END) == -1) /* end of file */
  335. return -1;
  336. new_offset = dig_ftell(&(Map->dig_fp));
  337. if (new_offset == -1)
  338. return -1;
  339. return V1__rewrite_line_nat(Map, new_offset, type, points, cats);
  340. }
  341. }
  342. /**
  343. \brief Rewrites line of given number.
  344. Old line is deleted (marked as dead), new line written.
  345. \param Map pointer to vector map
  346. \param line line id
  347. \param type feature type
  348. \param points line geometry
  349. \param cats line cats
  350. \return number of new line
  351. \return -1 on error
  352. */
  353. int
  354. V2_rewrite_line_nat(struct Map_info *Map,
  355. int line,
  356. int type,
  357. struct line_pnts *points, struct line_cats *cats)
  358. {
  359. /* TODO: this is just quick shortcut because we have already V2_delete_nat()
  360. * and V2_write_nat() this function first deletes old line
  361. * and then writes new one. It is not very effective if number of points
  362. * and cats was not changed or topology is not changed (nodes not moved,
  363. * angles not changed etc.) */
  364. V2_delete_line_nat(Map, line);
  365. return (V2_write_line_nat(Map, type, points, cats));
  366. }
  367. /**
  368. \brief Rewrites line at the given offset.
  369. \param Map pointer to vector map
  370. \param offset line offset
  371. \param type feature type
  372. \param points line geometry
  373. \param cats line cats
  374. \return line offset
  375. \return -1 on error
  376. */
  377. long
  378. V1__rewrite_line_nat(struct Map_info *Map,
  379. long offset,
  380. int type,
  381. struct line_pnts *points, struct line_cats *cats)
  382. {
  383. int i, n_points;
  384. char rhead, nc;
  385. short field;
  386. GVFILE *dig_fp;
  387. dig_set_cur_port(&(Map->head.port));
  388. dig_fp = &(Map->dig_fp);
  389. if (dig_fseek(dig_fp, offset, 0) == -1)
  390. return -1;
  391. /* first byte: 0 bit: 1 - alive, 0 - dead
  392. * 1 bit: 1 - categories, 0 - no category
  393. * 2-3 bit: store type
  394. * 4-5 bit: reserved for store type expansion
  395. * 6-7 bit: not used
  396. */
  397. rhead = (char)dig_type_to_store(type);
  398. rhead <<= 2;
  399. if (cats->n_cats > 0) {
  400. rhead |= 0x02;
  401. }
  402. rhead |= 0x01; /* written/rewritten is always alive */
  403. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp)) {
  404. return -1;
  405. }
  406. if (cats->n_cats > 0) {
  407. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  408. if (0 >= dig__fwrite_port_I(&(cats->n_cats), 1, dig_fp))
  409. return -1;
  410. }
  411. else { /* coor format 5.0 */
  412. nc = (char)cats->n_cats;
  413. if (0 >= dig__fwrite_port_C(&nc, 1, dig_fp))
  414. return -1;
  415. }
  416. if (cats->n_cats > 0) {
  417. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  418. if (0 >=
  419. dig__fwrite_port_I(cats->field, cats->n_cats, dig_fp))
  420. return -1;
  421. }
  422. else { /* coor format 5.0 */
  423. for (i = 0; i < cats->n_cats; i++) {
  424. field = (short)cats->field[i];
  425. if (0 >= dig__fwrite_port_S(&field, 1, dig_fp))
  426. return -1;
  427. }
  428. }
  429. if (0 >= dig__fwrite_port_I(cats->cat, cats->n_cats, dig_fp))
  430. return -1;
  431. }
  432. }
  433. if (type & GV_POINTS) {
  434. n_points = 1;
  435. }
  436. else {
  437. n_points = points->n_points;
  438. if (0 >= dig__fwrite_port_I(&n_points, 1, dig_fp))
  439. return -1;
  440. }
  441. if (0 >= dig__fwrite_port_D(points->x, n_points, dig_fp))
  442. return -1;
  443. if (0 >= dig__fwrite_port_D(points->y, n_points, dig_fp))
  444. return -1;
  445. if (Map->head.with_z) {
  446. if (0 >= dig__fwrite_port_D(points->z, n_points, dig_fp))
  447. return -1;
  448. }
  449. if (0 != dig_fflush(dig_fp))
  450. return -1;
  451. return offset;
  452. }
  453. /**
  454. \brief Deletes line at the given offset.
  455. \param Map pointer to vector map
  456. \param offset line offset
  457. \return 0 ok
  458. \return -1 on error
  459. */
  460. int V1_delete_line_nat(struct Map_info *Map, long offset)
  461. {
  462. char rhead;
  463. GVFILE *dig_fp;
  464. G_debug(3, "V1_delete_line_nat(), offset = %ld", offset);
  465. dig_set_cur_port(&(Map->head.port));
  466. dig_fp = &(Map->dig_fp);
  467. if (dig_fseek(dig_fp, offset, 0) == -1)
  468. return -1;
  469. /* read old */
  470. if (0 >= dig__fread_port_C(&rhead, 1, dig_fp))
  471. return (-1);
  472. rhead &= 0xFE;
  473. if (dig_fseek(dig_fp, offset, 0) == -1)
  474. return -1;
  475. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp))
  476. return -1;
  477. if (0 != dig_fflush(dig_fp))
  478. return -1;
  479. return 0;
  480. }
  481. /**
  482. \brief Deletes line of given number.
  483. \param pointer to vector map
  484. \param line line id
  485. \return 0 ok
  486. \return -1 on error
  487. */
  488. int V2_delete_line_nat(struct Map_info *Map, int line)
  489. {
  490. int ret, i, side, type = 0, first = 0, next_line, area;
  491. P_LINE *Line = NULL;
  492. P_AREA *Area;
  493. struct Plus_head *plus;
  494. BOUND_BOX box, abox;
  495. int adjacent[4], n_adjacent = 0;
  496. static struct line_cats *Cats = NULL;
  497. G_debug(3, "V2_delete_line_nat(), line = %d", line);
  498. plus = &(Map->plus);
  499. if (plus->built >= GV_BUILD_BASE) {
  500. Line = Map->plus.Line[line];
  501. if (Line == NULL)
  502. G_fatal_error(_("Attempt to delete dead line"));
  503. type = Line->type;
  504. }
  505. if (!Cats) {
  506. Cats = Vect_new_cats_struct();
  507. }
  508. /* Update category index */
  509. if (plus->update_cidx) {
  510. type = V2_read_line_nat(Map, NULL, Cats, line);
  511. for (i = 0; i < Cats->n_cats; i++) {
  512. dig_cidx_del_cat(plus, Cats->field[i], Cats->cat[i], line, type);
  513. }
  514. }
  515. /* delete the line from coor */
  516. ret = V1_delete_line_nat(Map, Line->offset);
  517. if (ret == -1) {
  518. return ret;
  519. }
  520. /* Update topology */
  521. if (plus->built >= GV_BUILD_AREAS && type == GV_BOUNDARY) {
  522. /* Store adjacent boundaries at nodes (will be used to rebuild area/isle) */
  523. /* Adjacent are stored: > 0 - we want right side; < 0 - we want left side */
  524. n_adjacent = 0;
  525. next_line = dig_angle_next_line(plus, line, GV_RIGHT, GV_BOUNDARY);
  526. if (next_line != 0 && abs(next_line) != line) {
  527. /* N1, to the right -> we want the right side for > 0 and left for < 0 */
  528. adjacent[n_adjacent] = next_line;
  529. n_adjacent++;
  530. }
  531. next_line = dig_angle_next_line(plus, line, GV_LEFT, GV_BOUNDARY);
  532. if (next_line != 0 && abs(next_line) != line) {
  533. /* N1, to the left -> we want the left side for > 0 and right for < 0 */
  534. adjacent[n_adjacent] = -next_line;
  535. n_adjacent++;
  536. }
  537. next_line = dig_angle_next_line(plus, -line, GV_RIGHT, GV_BOUNDARY);
  538. if (next_line != 0 && abs(next_line) != line) {
  539. /* N2, to the right -> we want the right side for > 0 and left for < 0 */
  540. adjacent[n_adjacent] = next_line;
  541. n_adjacent++;
  542. }
  543. next_line = dig_angle_next_line(plus, -line, GV_LEFT, GV_BOUNDARY);
  544. if (next_line != 0 && abs(next_line) != line) {
  545. /* N2, to the left -> we want the left side for > 0 and right for < 0 */
  546. adjacent[n_adjacent] = -next_line;
  547. n_adjacent++;
  548. }
  549. /* Delete area(s) and islands this line forms */
  550. first = 1;
  551. if (Line->left > 0) { /* delete area */
  552. Vect_get_area_box(Map, Line->left, &box);
  553. if (first) {
  554. Vect_box_copy(&abox, &box);
  555. first = 0;
  556. }
  557. else
  558. Vect_box_extend(&abox, &box);
  559. if (plus->update_cidx) {
  560. delete_area_cats_from_cidx(Map, Line->left);
  561. }
  562. dig_del_area(plus, Line->left);
  563. }
  564. else if (Line->left < 0) { /* delete isle */
  565. dig_del_isle(plus, -Line->left);
  566. }
  567. if (Line->right > 0) { /* delete area */
  568. Vect_get_area_box(Map, Line->right, &box);
  569. if (first) {
  570. Vect_box_copy(&abox, &box);
  571. first = 0;
  572. }
  573. else
  574. Vect_box_extend(&abox, &box);
  575. if (plus->update_cidx) {
  576. delete_area_cats_from_cidx(Map, Line->right);
  577. }
  578. dig_del_area(plus, Line->right);
  579. }
  580. else if (Line->right < 0) { /* delete isle */
  581. dig_del_isle(plus, -Line->right);
  582. }
  583. }
  584. /* Delete reference from area */
  585. if (plus->built >= GV_BUILD_CENTROIDS && type == GV_CENTROID) {
  586. if (Line->left > 0) {
  587. G_debug(3, "Remove centroid %d from area %d", line, Line->left);
  588. if (plus->update_cidx) {
  589. delete_area_cats_from_cidx(Map, Line->left);
  590. }
  591. Area = Map->plus.Area[Line->left];
  592. Area->centroid = 0;
  593. }
  594. }
  595. /* delete the line from topo */
  596. dig_del_line(plus, line);
  597. /* Rebuild areas/isles and attach centroids and isles */
  598. if (plus->built >= GV_BUILD_AREAS && type == GV_BOUNDARY) {
  599. int *new_areas, nnew_areas;
  600. nnew_areas = 0;
  601. new_areas = (int *)G_malloc(2 * n_adjacent * sizeof(int));
  602. /* Rebuild areas/isles */
  603. for (i = 0; i < n_adjacent; i++) {
  604. if (adjacent[i] > 0)
  605. side = GV_RIGHT;
  606. else
  607. side = GV_LEFT;
  608. G_debug(3, "Build area for line = %d, side = %d", adjacent[i],
  609. side);
  610. area = Vect_build_line_area(Map, abs(adjacent[i]), side);
  611. if (area > 0) { /* area */
  612. Vect_get_area_box(Map, area, &box);
  613. if (first) {
  614. Vect_box_copy(&abox, &box);
  615. first = 0;
  616. }
  617. else
  618. Vect_box_extend(&abox, &box);
  619. new_areas[nnew_areas] = area;
  620. nnew_areas++;
  621. }
  622. else if (area < 0) {
  623. /* isle -> must be attached -> add to abox */
  624. Vect_get_isle_box(Map, -area, &box);
  625. if (first) {
  626. Vect_box_copy(&abox, &box);
  627. first = 0;
  628. }
  629. else
  630. Vect_box_extend(&abox, &box);
  631. }
  632. }
  633. /* Reattach all centroids/isles in deleted areas + new area.
  634. * Because isles are selected by box it covers also possible new isle created above */
  635. if (!first) { /* i.e. old area/isle was deleted or new one created */
  636. /* Reattache isles */
  637. if (plus->built >= GV_BUILD_ATTACH_ISLES)
  638. Vect_attach_isles(Map, &abox);
  639. /* Reattach centroids */
  640. if (plus->built >= GV_BUILD_CENTROIDS)
  641. Vect_attach_centroids(Map, &abox);
  642. }
  643. if (plus->update_cidx) {
  644. for (i = 0; i < nnew_areas; i++) {
  645. add_area_cats_to_cidx(Map, new_areas[i]);
  646. }
  647. }
  648. }
  649. G_debug(3, "updated lines : %d , updated nodes : %d", plus->n_uplines,
  650. plus->n_upnodes);
  651. return ret;
  652. }