write_nat.c 26 KB

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