write_nat.c 25 KB

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