write_nat.c 26 KB

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