write_nat.c 25 KB

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