write_nat.c 26 KB

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