write_nat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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-2009 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. */
  17. #include <grass/config.h>
  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. const struct line_pnts *points, const 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. static off_t V1__rewrite_line_nat(struct Map_info *Map, off_t offset, int type,
  254. const struct line_pnts *points, const 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. off_t V1_write_line_nat(struct Map_info *Map,
  265. int type, const struct line_pnts *points, const struct line_cats *cats)
  266. {
  267. off_t 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. off_t V2_write_line_nat(struct Map_info *Map,
  285. int type, const struct line_pnts *points, const struct line_cats *cats)
  286. {
  287. int line;
  288. off_t 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. /* returns int line, but is defined as off_t for compatibility with
  314. * Write_line_array in write.c */
  315. return line;
  316. }
  317. /*!
  318. \brief Rewrites feature at the given offset.
  319. If the number of points or cats differs from the original one or
  320. the type is changed: GV_POINTS -> GV_LINES or GV_LINES ->
  321. GV_POINTS, the old one is deleted and the new is appended to the
  322. end of the file.
  323. Old feature is deleted (marked as dead), new feature written.
  324. \param Map pointer to vector map
  325. \param offset feature offset
  326. \param type feature type
  327. \param points feature geometry
  328. \param cats feature categories
  329. \return feature offset (rewriten feature)
  330. \return -1 on error
  331. */
  332. off_t V1_rewrite_line_nat(struct Map_info *Map,
  333. off_t offset,
  334. int type,
  335. const struct line_pnts *points, const struct line_cats *cats)
  336. {
  337. int old_type;
  338. struct line_pnts *old_points;
  339. struct line_cats *old_cats;
  340. off_t new_offset;
  341. /* TODO: enable points and cats == NULL */
  342. /* First compare numbers of points and cats with tha old one */
  343. old_points = Vect_new_line_struct();
  344. old_cats = Vect_new_cats_struct();
  345. old_type = V1_read_line_nat(Map, old_points, old_cats, offset);
  346. if (old_type == -1)
  347. return (-1); /* error */
  348. if (old_type != -2 /* EOF -> write new line */
  349. && points->n_points == old_points->n_points
  350. && cats->n_cats == old_cats->n_cats
  351. && (((type & GV_POINTS) && (old_type & GV_POINTS))
  352. || ((type & GV_LINES) && (old_type & GV_LINES)))) {
  353. /* equal -> overwrite the old */
  354. return V1__rewrite_line_nat(Map, offset, type, points, cats);
  355. }
  356. else {
  357. /* differ -> delete the old and append new */
  358. /* delete old */
  359. V1_delete_line_nat(Map, offset);
  360. /* write new */
  361. if (dig_fseek(&(Map->dig_fp), 0L, SEEK_END) == -1) /* end of file */
  362. return -1;
  363. new_offset = dig_ftell(&(Map->dig_fp));
  364. if (new_offset == -1)
  365. return -1;
  366. return V1__rewrite_line_nat(Map, new_offset, type, points, cats);
  367. }
  368. }
  369. /*!
  370. \brief Rewrites feature (topology level)
  371. Old feature is deleted (marked as dead), new feature written.
  372. \param Map pointer to vector map
  373. \param line feature id
  374. \param type feature type
  375. \param points feature geometry
  376. \param cats feature category
  377. \return new feature id
  378. \return -1 on error
  379. */
  380. int V2_rewrite_line_nat(struct Map_info *Map,
  381. int line,
  382. int type,
  383. const struct line_pnts *points, const struct line_cats *cats)
  384. {
  385. /* TODO: this is just quick shortcut because we have already V2_delete_nat()
  386. * and V2_write_nat() this function first deletes old line
  387. * and then writes new one. It is not very effective if number of points
  388. * and cats was not changed or topology is not changed (nodes not moved,
  389. * angles not changed etc.) */
  390. V2_delete_line_nat(Map, line);
  391. return (V2_write_line_nat(Map, type, points, cats));
  392. }
  393. /*!
  394. \brief Rewrites feature at the given offset.
  395. \param Map pointer to vector map
  396. \param offset feature offset
  397. \param type feature type
  398. \param points feature geometry
  399. \param cats feature categories
  400. \return feature offset
  401. \return -1 on error
  402. */
  403. off_t V1__rewrite_line_nat(struct Map_info *Map,
  404. off_t offset,
  405. int type,
  406. const struct line_pnts *points, const struct line_cats *cats)
  407. {
  408. int i, n_points;
  409. char rhead, nc;
  410. short field;
  411. GVFILE *dig_fp;
  412. dig_set_cur_port(&(Map->head.port));
  413. dig_fp = &(Map->dig_fp);
  414. if (dig_fseek(dig_fp, offset, 0) == -1)
  415. return -1;
  416. /* first byte: 0 bit: 1 - alive, 0 - dead
  417. * 1 bit: 1 - categories, 0 - no category
  418. * 2-3 bit: store type
  419. * 4-5 bit: reserved for store type expansion
  420. * 6-7 bit: not used
  421. */
  422. rhead = (char)dig_type_to_store(type);
  423. rhead <<= 2;
  424. if (cats->n_cats > 0) {
  425. rhead |= 0x02;
  426. }
  427. rhead |= 0x01; /* written/rewritten is always alive */
  428. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp)) {
  429. return -1;
  430. }
  431. if (cats->n_cats > 0) {
  432. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  433. if (0 >= dig__fwrite_port_I(&(cats->n_cats), 1, dig_fp))
  434. return -1;
  435. }
  436. else { /* coor format 5.0 */
  437. nc = (char)cats->n_cats;
  438. if (0 >= dig__fwrite_port_C(&nc, 1, dig_fp))
  439. return -1;
  440. }
  441. if (cats->n_cats > 0) {
  442. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  443. if (0 >=
  444. dig__fwrite_port_I(cats->field, cats->n_cats, dig_fp))
  445. return -1;
  446. }
  447. else { /* coor format 5.0 */
  448. for (i = 0; i < cats->n_cats; i++) {
  449. field = (short)cats->field[i];
  450. if (0 >= dig__fwrite_port_S(&field, 1, dig_fp))
  451. return -1;
  452. }
  453. }
  454. if (0 >= dig__fwrite_port_I(cats->cat, cats->n_cats, dig_fp))
  455. return -1;
  456. }
  457. }
  458. if (type & GV_POINTS) {
  459. n_points = 1;
  460. }
  461. else {
  462. n_points = points->n_points;
  463. if (0 >= dig__fwrite_port_I(&n_points, 1, dig_fp))
  464. return -1;
  465. }
  466. if (0 >= dig__fwrite_port_D(points->x, n_points, dig_fp))
  467. return -1;
  468. if (0 >= dig__fwrite_port_D(points->y, n_points, dig_fp))
  469. return -1;
  470. if (Map->head.with_z) {
  471. if (0 >= dig__fwrite_port_D(points->z, n_points, dig_fp))
  472. return -1;
  473. }
  474. if (0 != dig_fflush(dig_fp))
  475. return -1;
  476. return offset;
  477. }
  478. /*!
  479. \brief Deletes feature at the given offset.
  480. \param Map pointer to vector map
  481. \param offset feature offset
  482. \return 0 on success
  483. \return -1 on error
  484. */
  485. int V1_delete_line_nat(struct Map_info *Map, off_t offset)
  486. {
  487. char rhead;
  488. GVFILE *dig_fp;
  489. G_debug(3, "V1_delete_line_nat(), offset = %lu", (unsigned long) offset);
  490. dig_set_cur_port(&(Map->head.port));
  491. dig_fp = &(Map->dig_fp);
  492. if (dig_fseek(dig_fp, offset, 0) == -1)
  493. return -1;
  494. /* read old */
  495. if (0 >= dig__fread_port_C(&rhead, 1, dig_fp))
  496. return (-1);
  497. rhead &= 0xFE;
  498. if (dig_fseek(dig_fp, offset, 0) == -1)
  499. return -1;
  500. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp))
  501. return -1;
  502. if (0 != dig_fflush(dig_fp))
  503. return -1;
  504. return 0;
  505. }
  506. /*!
  507. \brief Deletes feature (topology level).
  508. \param pointer to vector map
  509. \param line feature id
  510. \return 0 on success
  511. \return -1 on error
  512. */
  513. int V2_delete_line_nat(struct Map_info *Map, int line)
  514. {
  515. int ret, i, side, type = 0, first = 0, next_line, area;
  516. P_LINE *Line = NULL;
  517. P_AREA *Area;
  518. struct Plus_head *plus;
  519. BOUND_BOX box, abox;
  520. int adjacent[4], n_adjacent = 0;
  521. static struct line_cats *Cats = NULL;
  522. G_debug(3, "V2_delete_line_nat(), line = %d", line);
  523. plus = &(Map->plus);
  524. if (plus->built >= GV_BUILD_BASE) {
  525. Line = Map->plus.Line[line];
  526. if (Line == NULL)
  527. G_fatal_error(_("Attempt to delete dead feature"));
  528. type = Line->type;
  529. }
  530. if (!Cats) {
  531. Cats = Vect_new_cats_struct();
  532. }
  533. /* Update category index */
  534. if (plus->update_cidx) {
  535. type = V2_read_line_nat(Map, NULL, Cats, line);
  536. for (i = 0; i < Cats->n_cats; i++) {
  537. dig_cidx_del_cat(plus, Cats->field[i], Cats->cat[i], line, type);
  538. }
  539. }
  540. /* delete the line from coor */
  541. ret = V1_delete_line_nat(Map, Line->offset);
  542. if (ret == -1) {
  543. return ret;
  544. }
  545. /* Update topology */
  546. if (plus->built >= GV_BUILD_AREAS && type == GV_BOUNDARY) {
  547. /* Store adjacent boundaries at nodes (will be used to rebuild area/isle) */
  548. /* Adjacent are stored: > 0 - we want right side; < 0 - we want left side */
  549. n_adjacent = 0;
  550. next_line = dig_angle_next_line(plus, line, GV_RIGHT, GV_BOUNDARY);
  551. if (next_line != 0 && abs(next_line) != line) {
  552. /* N1, to the right -> we want the right side for > 0 and left for < 0 */
  553. adjacent[n_adjacent] = next_line;
  554. n_adjacent++;
  555. }
  556. next_line = dig_angle_next_line(plus, line, GV_LEFT, GV_BOUNDARY);
  557. if (next_line != 0 && abs(next_line) != line) {
  558. /* N1, to the left -> we want the left side for > 0 and right for < 0 */
  559. adjacent[n_adjacent] = -next_line;
  560. n_adjacent++;
  561. }
  562. next_line = dig_angle_next_line(plus, -line, GV_RIGHT, GV_BOUNDARY);
  563. if (next_line != 0 && abs(next_line) != line) {
  564. /* N2, to the right -> we want the right side for > 0 and left for < 0 */
  565. adjacent[n_adjacent] = next_line;
  566. n_adjacent++;
  567. }
  568. next_line = dig_angle_next_line(plus, -line, GV_LEFT, GV_BOUNDARY);
  569. if (next_line != 0 && abs(next_line) != line) {
  570. /* N2, to the left -> we want the left side for > 0 and right for < 0 */
  571. adjacent[n_adjacent] = -next_line;
  572. n_adjacent++;
  573. }
  574. /* Delete area(s) and islands this line forms */
  575. first = 1;
  576. if (Line->left > 0) { /* delete area */
  577. Vect_get_area_box(Map, Line->left, &box);
  578. if (first) {
  579. Vect_box_copy(&abox, &box);
  580. first = 0;
  581. }
  582. else
  583. Vect_box_extend(&abox, &box);
  584. if (plus->update_cidx) {
  585. delete_area_cats_from_cidx(Map, Line->left);
  586. }
  587. dig_del_area(plus, Line->left);
  588. }
  589. else if (Line->left < 0) { /* delete isle */
  590. dig_del_isle(plus, -Line->left);
  591. }
  592. if (Line->right > 0) { /* delete area */
  593. Vect_get_area_box(Map, Line->right, &box);
  594. if (first) {
  595. Vect_box_copy(&abox, &box);
  596. first = 0;
  597. }
  598. else
  599. Vect_box_extend(&abox, &box);
  600. if (plus->update_cidx) {
  601. delete_area_cats_from_cidx(Map, Line->right);
  602. }
  603. dig_del_area(plus, Line->right);
  604. }
  605. else if (Line->right < 0) { /* delete isle */
  606. dig_del_isle(plus, -Line->right);
  607. }
  608. }
  609. /* Delete reference from area */
  610. if (plus->built >= GV_BUILD_CENTROIDS && type == GV_CENTROID) {
  611. if (Line->left > 0) {
  612. G_debug(3, "Remove centroid %d from area %d", line, Line->left);
  613. if (plus->update_cidx) {
  614. delete_area_cats_from_cidx(Map, Line->left);
  615. }
  616. Area = Map->plus.Area[Line->left];
  617. Area->centroid = 0;
  618. }
  619. }
  620. /* delete the line from topo */
  621. dig_del_line(plus, line);
  622. /* Rebuild areas/isles and attach centroids and isles */
  623. if (plus->built >= GV_BUILD_AREAS && type == GV_BOUNDARY) {
  624. int *new_areas, nnew_areas;
  625. nnew_areas = 0;
  626. new_areas = (int *)G_malloc(2 * n_adjacent * sizeof(int));
  627. /* Rebuild areas/isles */
  628. for (i = 0; i < n_adjacent; i++) {
  629. if (adjacent[i] > 0)
  630. side = GV_RIGHT;
  631. else
  632. side = GV_LEFT;
  633. G_debug(3, "Build area for line = %d, side = %d", adjacent[i],
  634. side);
  635. area = Vect_build_line_area(Map, abs(adjacent[i]), side);
  636. if (area > 0) { /* area */
  637. Vect_get_area_box(Map, area, &box);
  638. if (first) {
  639. Vect_box_copy(&abox, &box);
  640. first = 0;
  641. }
  642. else
  643. Vect_box_extend(&abox, &box);
  644. new_areas[nnew_areas] = area;
  645. nnew_areas++;
  646. }
  647. else if (area < 0) {
  648. /* isle -> must be attached -> add to abox */
  649. Vect_get_isle_box(Map, -area, &box);
  650. if (first) {
  651. Vect_box_copy(&abox, &box);
  652. first = 0;
  653. }
  654. else
  655. Vect_box_extend(&abox, &box);
  656. }
  657. }
  658. /* Reattach all centroids/isles in deleted areas + new area.
  659. * Because isles are selected by box it covers also possible new isle created above */
  660. if (!first) { /* i.e. old area/isle was deleted or new one created */
  661. /* Reattache isles */
  662. if (plus->built >= GV_BUILD_ATTACH_ISLES)
  663. Vect_attach_isles(Map, &abox);
  664. /* Reattach centroids */
  665. if (plus->built >= GV_BUILD_CENTROIDS)
  666. Vect_attach_centroids(Map, &abox);
  667. }
  668. if (plus->update_cidx) {
  669. for (i = 0; i < nnew_areas; i++) {
  670. add_area_cats_to_cidx(Map, new_areas[i]);
  671. }
  672. }
  673. }
  674. G_debug(3, "updated lines : %d , updated nodes : %d", plus->n_uplines,
  675. plus->n_upnodes);
  676. return ret;
  677. }
  678. /*!
  679. \brief Restores feature at the given offset.
  680. \param Map pointer to vector map
  681. \param offset feature offset
  682. \return 0 on success
  683. \return -1 on error
  684. */
  685. int V1_restore_line_nat(struct Map_info *Map, off_t offset)
  686. {
  687. char rhead;
  688. GVFILE *dig_fp;
  689. G_debug(3, "V1_restore_line_nat(), offset = %lu", (unsigned long) offset);
  690. dig_set_cur_port(&(Map->head.port));
  691. dig_fp = &(Map->dig_fp);
  692. if (dig_fseek(dig_fp, offset, 0) == -1)
  693. return -1;
  694. /* read old */
  695. if (0 >= dig__fread_port_C(&rhead, 1, dig_fp))
  696. return (-1);
  697. /* mark as alive */
  698. rhead |= 1;
  699. /* write new */
  700. if (dig_fseek(dig_fp, offset, 0) == -1)
  701. return -1;
  702. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp))
  703. return -1;
  704. if (0 != dig_fflush(dig_fp))
  705. return -1;
  706. return 0;
  707. }
  708. /*!
  709. \brief Restores feature (topology level)
  710. \param Map pointer to vector map
  711. \param line feature id
  712. \param offset feature offset
  713. \return 0 on success
  714. \return -1 on error
  715. */
  716. int V2_restore_line_nat(struct Map_info *Map, int line, off_t offset)
  717. {
  718. int i, ret, type;
  719. P_LINE *Line;
  720. struct Plus_head *plus;
  721. BOUND_BOX box;
  722. static struct line_pnts *points = NULL;
  723. static struct line_cats *cats = NULL;
  724. Line = NULL;
  725. type = 0;
  726. G_debug(3, "V2_restore_line_nat(), line = %d", line);
  727. plus = &(Map->plus);
  728. if (plus->built >= GV_BUILD_BASE) {
  729. Line = Map->plus.Line[line];
  730. if (Line != NULL)
  731. G_fatal_error(_("Attempt to restore alive feature"));
  732. }
  733. if (!points) {
  734. points = Vect_new_line_struct();
  735. }
  736. if (!cats) {
  737. cats = Vect_new_cats_struct();
  738. }
  739. /* restore the line in coor */
  740. ret = V1_restore_line_nat(Map, offset);
  741. if (ret == -1) {
  742. return ret;
  743. }
  744. /* read feature geometry */
  745. type = V1_read_line_nat(Map, points, cats, offset);
  746. if (type < 0) {
  747. return -1;
  748. }
  749. /* update category index */
  750. if (plus->update_cidx) {
  751. for (i = 0; i < cats->n_cats; i++) {
  752. dig_cidx_add_cat(plus, cats->field[i], cats->cat[i], line, type);
  753. }
  754. }
  755. /* restore the line from topo */
  756. if (plus->built >= GV_BUILD_BASE) {
  757. dig_restore_line(plus, line, type, points, offset);
  758. G_debug(3, " line restored in topo with id = %d", line);
  759. dig_line_box(points, &box);
  760. dig_line_set_box(plus, line, &box);
  761. Vect_box_extend(&(plus->box), &box);
  762. }
  763. add_line_to_topo(Map,
  764. line, points, cats);
  765. G_debug(3, "updated lines : %d , updated nodes : %d", plus->n_uplines,
  766. plus->n_upnodes);
  767. return ret;
  768. }