write_nat.c 24 KB

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