write_nat.c 26 KB

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