write_nat.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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
  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. - reattache 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. */
  100. static void V2__add_line_to_topo_nat(struct Map_info *Map, int line,
  101. const struct line_pnts *points, const struct line_cats *cats)
  102. {
  103. int first, s, n, i;
  104. int type, node, next_line, area, side, sel_area, new_area[2];
  105. struct Plus_head *plus;
  106. struct P_line *Line, *NLine;
  107. struct P_node *Node;
  108. struct P_area *Area;
  109. struct bound_box box, abox;
  110. G_debug(3, "V2__add_line_to_topo_nat(), line = %d", line);
  111. plus = &(Map->plus);
  112. Line = plus->Line[line];
  113. type = Line->type;
  114. if (plus->built >= GV_BUILD_AREAS) {
  115. if (type == GV_BOUNDARY) {
  116. /* Delete neighbour areas/isles */
  117. first = 1;
  118. for (s = 0; s < 2; s++) { /* for each node */
  119. node = (s == 0 ? Line->N1 : Line->N2);
  120. G_debug(3,
  121. " delete neighbour areas/isles: %s node = %d",
  122. (s == 0 ? "first" : "second"), 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)
  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. V2__delete_area_cats_from_cidx_nat(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.
  170. * It's true that we deleted also adjacent areas/isles, but
  171. * if they form new one our boundary must participate, so
  172. * we need to build areas/isles just for our boundary */
  173. for (s = 0; s < 2; s++) {
  174. side = (s == 0 ? GV_LEFT : GV_RIGHT);
  175. G_debug(3, " build area/isle on side = %d", side);
  176. G_debug(3, "Build area for line = %d, side = %d", line, side);
  177. area = Vect_build_line_area(Map, line, side);
  178. G_debug(3, "Build area for line = %d, side = %d", line, side);
  179. if (area > 0) { /* area */
  180. Vect_get_area_box(Map, area, &box);
  181. if (first) {
  182. Vect_box_copy(&abox, &box);
  183. first = 0;
  184. }
  185. else
  186. Vect_box_extend(&abox, &box);
  187. }
  188. else if (area < 0) {
  189. /* isle -> must be attached -> add to abox */
  190. Vect_get_isle_box(Map, -area, &box);
  191. if (first) {
  192. Vect_box_copy(&abox, &box);
  193. first = 0;
  194. }
  195. else
  196. Vect_box_extend(&abox, &box);
  197. }
  198. new_area[s] = area;
  199. }
  200. /* Reattach all centroids/isles in deleted areas + new area.
  201. * Because isles are selected by box it covers also possible new isle created above */
  202. if (!first) { /* i.e. old area/isle was deleted or new one created */
  203. /* Reattach isles */
  204. if (plus->built >= GV_BUILD_ATTACH_ISLES)
  205. Vect_attach_isles(Map, &abox);
  206. /* Reattach centroids */
  207. if (plus->built >= GV_BUILD_CENTROIDS)
  208. Vect_attach_centroids(Map, &abox);
  209. }
  210. /* Add to category index */
  211. if (plus->update_cidx) {
  212. for (s = 0; s < 2; s++) {
  213. if (new_area[s] > 0) {
  214. V2__add_area_cats_to_cidx_nat(Map, new_area[s]);
  215. }
  216. }
  217. }
  218. }
  219. }
  220. /* Attach centroid */
  221. if (plus->built >= GV_BUILD_CENTROIDS) {
  222. if (type == GV_CENTROID) {
  223. sel_area = Vect_find_area(Map, points->x[0], points->y[0]);
  224. G_debug(3, " new centroid %d is in area %d", line, sel_area);
  225. if (sel_area > 0) {
  226. Area = plus->Area[sel_area];
  227. Line = plus->Line[line];
  228. if (Area->centroid == 0) { /* first centroid */
  229. G_debug(3, " first centroid -> attach to area");
  230. Area->centroid = line;
  231. Line->left = sel_area;
  232. if (plus->update_cidx) {
  233. V2__add_area_cats_to_cidx_nat(Map, sel_area);
  234. }
  235. }
  236. else { /* duplicate centroid */
  237. G_debug(3,
  238. " duplicate centroid -> do not attach to area");
  239. Line->left = -sel_area;
  240. }
  241. }
  242. }
  243. }
  244. /* Add category index */
  245. for (i = 0; i < cats->n_cats; i++) {
  246. dig_cidx_add_cat_sorted(plus, cats->field[i], cats->cat[i], line,
  247. type);
  248. }
  249. return;
  250. }
  251. /*!
  252. \brief Writes feature to 'coor' file (level 1)
  253. \param Map pointer to Map_info structure
  254. \param type feature type
  255. \param points feature geometry
  256. \param cats feature categories
  257. \return feature offset into file
  258. \return -1 on error
  259. */
  260. off_t V1_write_line_nat(struct Map_info *Map,
  261. int type, const struct line_pnts *points, const struct line_cats *cats)
  262. {
  263. off_t offset;
  264. if (dig_fseek(&(Map->dig_fp), 0L, SEEK_END) == -1) /* set to end of file */
  265. return -1;
  266. offset = dig_ftell(&(Map->dig_fp));
  267. if (offset == -1)
  268. return -1;
  269. return V1__rewrite_line_nat(Map, offset, type, points, cats);
  270. }
  271. /*!
  272. \brief Writes feature to 'coor' file (topology level) - internal use only
  273. \param Map pointer to Map_info structure
  274. \param type feature type
  275. \param points feature geometry
  276. \param cats feature categories
  277. \return new feature id
  278. \return -1 on error
  279. */
  280. off_t V2_write_line_nat(struct Map_info *Map, int type,
  281. const struct line_pnts *points, const struct line_cats *cats)
  282. {
  283. int line;
  284. off_t offset;
  285. struct Plus_head *plus;
  286. struct bound_box box;
  287. line = 0;
  288. G_debug(3, "V2_write_line_nat()");
  289. offset = V1_write_line_nat(Map, type, points, cats);
  290. if (offset < 0)
  291. return -1;
  292. /* Update topology */
  293. plus = &(Map->plus);
  294. /* Add line */
  295. if (plus->built >= GV_BUILD_BASE) {
  296. line = dig_add_line(plus, type, points, offset);
  297. G_debug(3, " line added to topo with id = %d", line);
  298. dig_line_box(points, &box);
  299. dig_line_set_box(plus, line, &box);
  300. if (line == 1)
  301. Vect_box_copy(&(plus->box), &box);
  302. else
  303. Vect_box_extend(&(plus->box), &box);
  304. }
  305. V2__add_line_to_topo_nat(Map, line, points, cats);
  306. G_debug(3, "updated lines : %d , updated nodes : %d", plus->n_uplines,
  307. plus->n_upnodes);
  308. /* returns int line, but is defined as off_t for compatibility with
  309. * Vect_write_line_array in write.c */
  310. return line;
  311. }
  312. /*!
  313. \brief Rewrites feature at the given offset (level 1)
  314. If the number of points or cats differs from the original one or
  315. the type is changed: GV_POINTS -> GV_LINES or GV_LINES ->
  316. GV_POINTS, the old one is deleted and the new is appended to the
  317. end of the file.
  318. Old feature is deleted (marked as dead), new feature written.
  319. \param Map pointer to Map_info structure
  320. \param offset feature offset
  321. \param type feature type
  322. \param points feature geometry
  323. \param cats feature categories
  324. \return feature offset (rewriten feature)
  325. \return -1 on error
  326. */
  327. off_t V1_rewrite_line_nat(struct Map_info *Map, int line, int type, off_t offset,
  328. const struct line_pnts *points, const struct line_cats *cats)
  329. {
  330. int old_type;
  331. struct line_pnts *old_points;
  332. struct line_cats *old_cats;
  333. off_t new_offset;
  334. G_debug(3, "V1_rewrite_line_nat(), offset = %lu", (unsigned long) offset);
  335. /* TODO: enable points and cats == NULL */
  336. /* First compare numbers of points and cats with tha old one */
  337. old_points = Vect_new_line_struct();
  338. old_cats = Vect_new_cats_struct();
  339. old_type = V1_read_line_nat(Map, old_points, old_cats, offset);
  340. if (old_type == -1)
  341. return (-1); /* error */
  342. if (old_type != -2 /* EOF -> write new line */
  343. && points->n_points == old_points->n_points
  344. && cats->n_cats == old_cats->n_cats
  345. && (((type & GV_POINTS) && (old_type & GV_POINTS))
  346. || ((type & GV_LINES) && (old_type & GV_LINES)))) {
  347. /* equal -> overwrite the old */
  348. return V1__rewrite_line_nat(Map, offset, type, points, cats);
  349. }
  350. else {
  351. /* differ -> delete the old and append new */
  352. /* delete old */
  353. V1_delete_line_nat(Map, offset);
  354. /* write new */
  355. if (dig_fseek(&(Map->dig_fp), 0L, SEEK_END) == -1) /* end of file */
  356. return -1;
  357. new_offset = dig_ftell(&(Map->dig_fp));
  358. if (new_offset == -1)
  359. return -1;
  360. return V1__rewrite_line_nat(Map, new_offset, type, points, cats);
  361. }
  362. }
  363. /*!
  364. \brief Rewrites feature to 'coor' file (topology level) - internal use only
  365. \param Map pointer to Map_info structure
  366. \param type feature type
  367. \param line feature id
  368. \param points feature geometry
  369. \param cats feature categories
  370. \return offset where line was rewritten
  371. \return -1 on error
  372. */
  373. off_t V2_rewrite_line_nat(struct Map_info *Map, int line, int type, off_t old_offset,
  374. const struct line_pnts *points, const struct line_cats *cats)
  375. {
  376. /* TODO: this is just quick shortcut because we have already V2_delete_nat()
  377. * and V2_write_nat() this function first deletes old line
  378. * and then writes new one. It is not very effective if number of points
  379. * and cats was not changed or topology is not changed (nodes not moved,
  380. * angles not changed etc.) */
  381. off_t offset;
  382. struct Plus_head *plus;
  383. struct bound_box box;
  384. V2_delete_line_nat(Map, line);
  385. G_debug(3, "V2_write_line_nat(), line = %d", line);
  386. offset = V1_rewrite_line_nat(Map, line, type, old_offset, points, cats);
  387. if (offset < 0)
  388. return -1;
  389. /* Update topology */
  390. plus = &(Map->plus);
  391. /* Add line */
  392. if (plus->built >= GV_BUILD_BASE) {
  393. line = dig_add_line(plus, type, points, offset);
  394. G_debug(3, " line added to topo with id = %d", line);
  395. dig_line_box(points, &box);
  396. dig_line_set_box(plus, line, &box);
  397. if (line == 1)
  398. Vect_box_copy(&(plus->box), &box);
  399. else
  400. Vect_box_extend(&(plus->box), &box);
  401. }
  402. V2__add_line_to_topo_nat(Map, line, points, cats);
  403. G_debug(3, "updated lines : %d , updated nodes : %d", plus->n_uplines,
  404. plus->n_upnodes);
  405. /* returns int line, but is defined as off_t for compatibility with
  406. * Vect_rewrite_line_array in write.c */
  407. return line;
  408. }
  409. /*!
  410. \brief Rewrites feature at the given offset.
  411. \param Map pointer to Map_info structure
  412. \param offset feature offset
  413. \param type feature type
  414. \param points feature geometry
  415. \param cats feature categories
  416. \return feature offset
  417. \return -1 on error
  418. */
  419. off_t V1__rewrite_line_nat(struct Map_info *Map,
  420. off_t offset,
  421. int type,
  422. const struct line_pnts *points, const struct line_cats *cats)
  423. {
  424. int i, n_points;
  425. char rhead, nc;
  426. short field;
  427. struct gvfile *dig_fp;
  428. dig_set_cur_port(&(Map->head.port));
  429. dig_fp = &(Map->dig_fp);
  430. if (dig_fseek(dig_fp, offset, 0) == -1)
  431. return -1;
  432. /* first byte: 0 bit: 1 - alive, 0 - dead
  433. * 1 bit: 1 - categories, 0 - no category
  434. * 2-3 bit: store type
  435. * 4-5 bit: reserved for store type expansion
  436. * 6-7 bit: not used
  437. */
  438. rhead = (char)dig_type_to_store(type);
  439. rhead <<= 2;
  440. if (cats->n_cats > 0) {
  441. rhead |= 0x02;
  442. }
  443. rhead |= 0x01; /* written/rewritten is always alive */
  444. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp)) {
  445. return -1;
  446. }
  447. if (cats->n_cats > 0) {
  448. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  449. if (0 >= dig__fwrite_port_I(&(cats->n_cats), 1, dig_fp))
  450. return -1;
  451. }
  452. else { /* coor format 5.0 */
  453. nc = (char)cats->n_cats;
  454. if (0 >= dig__fwrite_port_C(&nc, 1, dig_fp))
  455. return -1;
  456. }
  457. if (cats->n_cats > 0) {
  458. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  459. if (0 >=
  460. dig__fwrite_port_I(cats->field, cats->n_cats, dig_fp))
  461. return -1;
  462. }
  463. else { /* coor format 5.0 */
  464. for (i = 0; i < cats->n_cats; i++) {
  465. field = (short)cats->field[i];
  466. if (0 >= dig__fwrite_port_S(&field, 1, dig_fp))
  467. return -1;
  468. }
  469. }
  470. if (0 >= dig__fwrite_port_I(cats->cat, cats->n_cats, dig_fp))
  471. return -1;
  472. }
  473. }
  474. if (type & GV_POINTS) {
  475. n_points = 1;
  476. }
  477. else {
  478. n_points = points->n_points;
  479. if (0 >= dig__fwrite_port_I(&n_points, 1, dig_fp))
  480. return -1;
  481. }
  482. if (0 >= dig__fwrite_port_D(points->x, n_points, dig_fp))
  483. return -1;
  484. if (0 >= dig__fwrite_port_D(points->y, n_points, dig_fp))
  485. return -1;
  486. if (Map->head.with_z) {
  487. if (0 >= dig__fwrite_port_D(points->z, n_points, dig_fp))
  488. return -1;
  489. }
  490. if (0 != dig_fflush(dig_fp))
  491. return -1;
  492. return offset;
  493. }
  494. /*!
  495. \brief Deletes feature at the given offset (level 1)
  496. \param Map pointer Map_info structure
  497. \param offset feature offset
  498. \return 0 on success
  499. \return -1 on error
  500. */
  501. int V1_delete_line_nat(struct Map_info *Map, off_t offset)
  502. {
  503. char rhead;
  504. struct gvfile *dig_fp;
  505. G_debug(3, "V1_delete_line_nat(), offset = %lu", (unsigned long) offset);
  506. dig_set_cur_port(&(Map->head.port));
  507. dig_fp = &(Map->dig_fp);
  508. if (dig_fseek(dig_fp, offset, 0) == -1)
  509. return -1;
  510. /* read old */
  511. if (0 >= dig__fread_port_C(&rhead, 1, dig_fp))
  512. return (-1);
  513. rhead &= 0xFE;
  514. if (dig_fseek(dig_fp, offset, 0) == -1)
  515. return -1;
  516. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp))
  517. return -1;
  518. if (0 != dig_fflush(dig_fp))
  519. return -1;
  520. return 0;
  521. }
  522. /*!
  523. \brief Deletes feature (topology level) -- internal use only
  524. \param pointer to Map_info structure
  525. \param line feature id
  526. \return 0 on success
  527. \return -1 on error
  528. */
  529. int V2_delete_line_nat(struct Map_info *Map, off_t line)
  530. {
  531. int ret, i, side, type, first, next_line, area;
  532. struct P_line *Line;
  533. struct P_area *Area;
  534. struct Plus_head *plus;
  535. struct bound_box box, abox;
  536. int adjacent[4], n_adjacent;
  537. static struct line_cats *Cats = NULL;
  538. G_debug(3, "V2_delete_line_nat(), line = %lu", (unsigned long) line);
  539. type = first = n_adjacent = 0;
  540. Line = NULL;
  541. plus = &(Map->plus);
  542. if (plus->built >= GV_BUILD_BASE) {
  543. Line = Map->plus.Line[line];
  544. if (Line == NULL)
  545. G_fatal_error(_("Attempt to delete dead feature"));
  546. type = Line->type;
  547. }
  548. if (!Cats) {
  549. Cats = Vect_new_cats_struct();
  550. }
  551. /* Update category index */
  552. if (plus->update_cidx) {
  553. type = V2_read_line_nat(Map, NULL, Cats, line);
  554. for (i = 0; i < Cats->n_cats; i++) {
  555. dig_cidx_del_cat(plus, Cats->field[i], Cats->cat[i], line, type);
  556. }
  557. }
  558. /* delete the line from coor */
  559. ret = V1_delete_line_nat(Map, Line->offset);
  560. if (ret == -1) {
  561. return ret;
  562. }
  563. /* Update topology */
  564. if (plus->built >= GV_BUILD_AREAS && type == GV_BOUNDARY) {
  565. /* Store adjacent boundaries at nodes (will be used to rebuild area/isle) */
  566. /* Adjacent are stored: > 0 - we want right side; < 0 - we want left side */
  567. n_adjacent = 0;
  568. next_line = dig_angle_next_line(plus, line, GV_RIGHT, GV_BOUNDARY);
  569. if (next_line != 0 && abs(next_line) != line) {
  570. /* N1, to the right -> we want the right side for > 0 and left for < 0 */
  571. adjacent[n_adjacent] = next_line;
  572. n_adjacent++;
  573. }
  574. next_line = dig_angle_next_line(plus, line, GV_LEFT, GV_BOUNDARY);
  575. if (next_line != 0 && abs(next_line) != line) {
  576. /* N1, to the left -> we want the left side for > 0 and right for < 0 */
  577. adjacent[n_adjacent] = -next_line;
  578. n_adjacent++;
  579. }
  580. next_line = dig_angle_next_line(plus, -line, GV_RIGHT, GV_BOUNDARY);
  581. if (next_line != 0 && abs(next_line) != line) {
  582. /* N2, to the right -> we want the right side for > 0 and left for < 0 */
  583. adjacent[n_adjacent] = next_line;
  584. n_adjacent++;
  585. }
  586. next_line = dig_angle_next_line(plus, -line, GV_LEFT, GV_BOUNDARY);
  587. if (next_line != 0 && abs(next_line) != line) {
  588. /* N2, to the left -> we want the left side for > 0 and right for < 0 */
  589. adjacent[n_adjacent] = -next_line;
  590. n_adjacent++;
  591. }
  592. /* Delete area(s) and islands this line forms */
  593. first = 1;
  594. if (Line->left > 0) { /* delete area */
  595. Vect_get_area_box(Map, Line->left, &box);
  596. if (first) {
  597. Vect_box_copy(&abox, &box);
  598. first = 0;
  599. }
  600. else
  601. Vect_box_extend(&abox, &box);
  602. if (plus->update_cidx) {
  603. V2__delete_area_cats_from_cidx_nat(Map, Line->left);
  604. }
  605. dig_del_area(plus, Line->left);
  606. }
  607. else if (Line->left < 0) { /* delete isle */
  608. dig_del_isle(plus, -Line->left);
  609. }
  610. if (Line->right > 0) { /* delete area */
  611. Vect_get_area_box(Map, Line->right, &box);
  612. if (first) {
  613. Vect_box_copy(&abox, &box);
  614. first = 0;
  615. }
  616. else
  617. Vect_box_extend(&abox, &box);
  618. if (plus->update_cidx) {
  619. V2__delete_area_cats_from_cidx_nat(Map, Line->right);
  620. }
  621. dig_del_area(plus, Line->right);
  622. }
  623. else if (Line->right < 0) { /* delete isle */
  624. dig_del_isle(plus, -Line->right);
  625. }
  626. }
  627. /* Delete reference from area */
  628. if (plus->built >= GV_BUILD_CENTROIDS && type == GV_CENTROID) {
  629. if (Line->left > 0) {
  630. G_debug(3, "Remove centroid %d from area %d", (int) line, Line->left);
  631. if (plus->update_cidx) {
  632. V2__delete_area_cats_from_cidx_nat(Map, Line->left);
  633. }
  634. Area = Map->plus.Area[Line->left];
  635. Area->centroid = 0;
  636. }
  637. }
  638. /* delete the line from topo */
  639. dig_del_line(plus, line);
  640. /* Rebuild areas/isles and attach centroids and isles */
  641. if (plus->built >= GV_BUILD_AREAS && type == GV_BOUNDARY) {
  642. int new_areas[4], nnew_areas = 0;
  643. /* Rebuild areas/isles */
  644. for (i = 0; i < n_adjacent; i++) {
  645. side = (adjacent[i] > 0 ? GV_RIGHT : GV_LEFT);
  646. G_debug(3, "Build area for line = %d, side = %d", adjacent[i],
  647. side);
  648. area = Vect_build_line_area(Map, abs(adjacent[i]), side);
  649. if (area > 0) { /* area */
  650. Vect_get_area_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. new_areas[nnew_areas] = area;
  658. nnew_areas++;
  659. }
  660. else if (area < 0) {
  661. /* isle -> must be attached -> add to abox */
  662. Vect_get_isle_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. }
  670. }
  671. /* Reattach all centroids/isles in deleted areas + new area.
  672. * Because isles are selected by box it covers also possible new isle created above */
  673. if (!first) { /* i.e. old area/isle was deleted or new one created */
  674. /* Reattach isles */
  675. if (plus->built >= GV_BUILD_ATTACH_ISLES)
  676. Vect_attach_isles(Map, &abox);
  677. /* Reattach centroids */
  678. if (plus->built >= GV_BUILD_CENTROIDS)
  679. Vect_attach_centroids(Map, &abox);
  680. }
  681. if (plus->update_cidx) {
  682. for (i = 0; i < nnew_areas; i++) {
  683. V2__add_area_cats_to_cidx_nat(Map, new_areas[i]);
  684. }
  685. }
  686. }
  687. G_debug(3, "updated lines : %d , updated nodes : %d", plus->n_uplines,
  688. plus->n_upnodes);
  689. return ret;
  690. }
  691. /*!
  692. \brief Restores feature at the given offset.
  693. \param Map pointer to Map_info structure
  694. \param offset feature offset
  695. \return 0 on success
  696. \return -1 on error
  697. */
  698. int V1_restore_line_nat(struct Map_info *Map, off_t offset)
  699. {
  700. char rhead;
  701. struct gvfile *dig_fp;
  702. G_debug(3, "V1_restore_line_nat(), offset = %lu", (unsigned long) offset);
  703. dig_set_cur_port(&(Map->head.port));
  704. dig_fp = &(Map->dig_fp);
  705. if (dig_fseek(dig_fp, offset, 0) == -1)
  706. return -1;
  707. /* read old */
  708. if (0 >= dig__fread_port_C(&rhead, 1, dig_fp))
  709. return (-1);
  710. /* mark as alive */
  711. rhead |= 1;
  712. /* write new */
  713. if (dig_fseek(dig_fp, offset, 0) == -1)
  714. return -1;
  715. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp))
  716. return -1;
  717. if (0 != dig_fflush(dig_fp))
  718. return -1;
  719. return 0;
  720. }
  721. /*!
  722. \brief Restores feature (topology level)
  723. \param Map pointer to Map_info structure
  724. \param line feature id
  725. \param offset feature offset
  726. \return 0 on success
  727. \return -1 on error
  728. */
  729. int V2_restore_line_nat(struct Map_info *Map, int line, off_t offset)
  730. {
  731. int i, ret, type;
  732. struct P_line *Line;
  733. struct Plus_head *plus;
  734. struct bound_box box;
  735. static struct line_pnts *points = NULL;
  736. static struct line_cats *cats = NULL;
  737. Line = NULL;
  738. type = 0;
  739. G_debug(3, "V2_restore_line_nat(), line = %d", line);
  740. plus = &(Map->plus);
  741. if (plus->built >= GV_BUILD_BASE) {
  742. Line = Map->plus.Line[line];
  743. if (Line != NULL)
  744. G_fatal_error(_("Attempt to restore alive feature"));
  745. }
  746. if (!points) {
  747. points = Vect_new_line_struct();
  748. }
  749. if (!cats) {
  750. cats = Vect_new_cats_struct();
  751. }
  752. /* restore the line in coor */
  753. ret = V1_restore_line_nat(Map, offset);
  754. if (ret == -1) {
  755. return ret;
  756. }
  757. /* read feature geometry */
  758. type = V1_read_line_nat(Map, points, cats, offset);
  759. if (type < 0) {
  760. return -1;
  761. }
  762. /* update category index */
  763. if (plus->update_cidx) {
  764. for (i = 0; i < cats->n_cats; i++) {
  765. dig_cidx_add_cat(plus, cats->field[i], cats->cat[i], line, type);
  766. }
  767. }
  768. /* restore the line from topo */
  769. if (plus->built >= GV_BUILD_BASE) {
  770. dig_restore_line(plus, line, type, points, offset);
  771. G_debug(3, " line restored in topo with id = %d", line);
  772. dig_line_box(points, &box);
  773. dig_line_set_box(plus, line, &box);
  774. Vect_box_extend(&(plus->box), &box);
  775. }
  776. V2__add_line_to_topo_nat(Map,
  777. line, points, cats);
  778. G_debug(3, "updated lines : %d , updated nodes : %d", plus->n_uplines,
  779. plus->n_upnodes);
  780. return ret;
  781. }