write_nat.c 24 KB

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