write_nat.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*!
  2. \file lib/vector/Vlib/write_nat.c
  3. \brief Vector library - write/modify vector feature (native format)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. Operations:
  6. - Add feature
  7. - Rewrite feature
  8. - Delete feature
  9. - Restore feature
  10. (C) 2001-2010 by the GRASS Development Team
  11. This program is free software under the GNU General Public License
  12. (>=v2). Read the file COPYING that comes with GRASS for details.
  13. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  14. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  15. \author V*_restore_line() by Martin Landa <landa.martin gmail.com> (2008)
  16. */
  17. #include <grass/config.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include <grass/gis.h>
  22. #include <grass/vector.h>
  23. #include <grass/glocale.h>
  24. /*!
  25. \brief Add line to topo file
  26. Update areas. Areas are modified if:
  27. 1) first or/and last point are existing nodes ->
  28. - drop areas/islands whose boundaries are neighbour to this boundary at these nodes
  29. - try build areas and islands for this boundary and neighbour boundaries going through these nodes
  30. Question: may be by adding line created new area/isle which doesn't go through nodes of this line
  31. <pre>
  32. old new line
  33. +----+----+ +----+----+ +----+----+
  34. | A1 | A2 | + / -> | A1 | /| or + \ -> | A1 | A2 | \
  35. | | | | | | | | |
  36. +----+----+ +----+----+ +----+----+
  37. I1 I1 I1 I1
  38. </pre>
  39. - reattache all centroids/isles inside new area(s)
  40. - attach new isle to area outside
  41. 2) line is closed ring (node at the end is new, so it is not case above)
  42. - build new area/isle
  43. - check if it is island or contains island(s)
  44. - re-attach all centroids/isles inside new area(s)
  45. - attach new isle to area outside
  46. Note that 1) and 2) is done by the same code.
  47. */
  48. static void add_line_to_topo(struct Map_info *Map, int line,
  49. const struct line_pnts *points, const struct line_cats *cats)
  50. {
  51. int first, s, n, i;
  52. int type, node, next_line, area, side, sel_area, new_area[2];
  53. struct Plus_head *plus;
  54. struct P_line *Line, *NLine;
  55. struct P_node *Node;
  56. struct P_area *Area;
  57. struct bound_box box, abox;
  58. plus = &(Map->plus);
  59. Line = plus->Line[line];
  60. type = Line->type;
  61. if (plus->built >= GV_BUILD_AREAS) {
  62. if (type == GV_BOUNDARY) {
  63. /* Delete neighbour areas/isles */
  64. first = 1;
  65. for (s = 1; s < 3; s++) { /* for each node */
  66. if (s == 1)
  67. node = Line->N1; /* Node 1 */
  68. else
  69. node = Line->N2;
  70. G_debug(3,
  71. " delete neighbour areas/iseles: side = %d node = %d",
  72. s, node);
  73. Node = plus->Node[node];
  74. n = 0;
  75. for (i = 0; i < Node->n_lines; i++) {
  76. NLine = plus->Line[abs(Node->lines[i])];
  77. if (NLine->type == GV_BOUNDARY)
  78. n++;
  79. }
  80. G_debug(3, " number of boundaries at node = %d", n);
  81. if (n > 2) { /* more than 2 boundaries at node ( >= 2 old + 1 new ) */
  82. /* Line above (to the right), it is enough to check to the right, because if area/isle
  83. * exists it is the same to the left */
  84. if (s == 1)
  85. next_line =
  86. dig_angle_next_line(plus, line, GV_RIGHT,
  87. GV_BOUNDARY);
  88. else
  89. next_line =
  90. dig_angle_next_line(plus, -line, GV_RIGHT,
  91. GV_BOUNDARY);
  92. if (next_line != 0) { /* there is a boundary to the right */
  93. NLine = plus->Line[abs(next_line)];
  94. if (next_line > 0) /* the boundary is connected by 1. node */
  95. area = NLine->right; /* we are interested just in this side (close to our line) */
  96. else if (next_line < 0) /* the boundary is connected by 2. node */
  97. area = NLine->left;
  98. G_debug(3, " next_line = %d area = %d", next_line,
  99. area);
  100. if (area > 0) { /* is area */
  101. Vect_get_area_box(Map, area, &box);
  102. if (first) {
  103. Vect_box_copy(&abox, &box);
  104. first = 0;
  105. }
  106. else
  107. Vect_box_extend(&abox, &box);
  108. if (plus->update_cidx) {
  109. Vect__delete_area_cats_from_cidx(Map, area);
  110. }
  111. dig_del_area(plus, area);
  112. }
  113. else if (area < 0) { /* is isle */
  114. dig_del_isle(plus, -area);
  115. }
  116. }
  117. }
  118. }
  119. /* Build new areas/isles. Thas true that we deleted also adjacent areas/isles, but if
  120. * they form new one our boundary must participate, so we need to build areas/isles
  121. * just for our boundary */
  122. for (s = 1; s < 3; s++) {
  123. if (s == 1)
  124. side = GV_LEFT;
  125. else
  126. side = GV_RIGHT;
  127. G_debug(3, " build area/isle on side = %d", side);
  128. G_debug(3, "Build area for line = %d, side = %d", line, side);
  129. area = Vect_build_line_area(Map, line, side);
  130. G_debug(3, "Build area for line = %d, side = %d", line, side);
  131. if (area > 0) { /* area */
  132. Vect_get_area_box(Map, area, &box);
  133. if (first) {
  134. Vect_box_copy(&abox, &box);
  135. first = 0;
  136. }
  137. else
  138. Vect_box_extend(&abox, &box);
  139. }
  140. else if (area < 0) {
  141. /* isle -> must be attached -> add to abox */
  142. Vect_get_isle_box(Map, -area, &box);
  143. if (first) {
  144. Vect_box_copy(&abox, &box);
  145. first = 0;
  146. }
  147. else
  148. Vect_box_extend(&abox, &box);
  149. }
  150. new_area[s - 1] = area;
  151. }
  152. /* Reattach all centroids/isles in deleted areas + new area.
  153. * Because isles are selected by box it covers also possible new isle created above */
  154. if (!first) { /* i.e. old area/isle was deleted or new one created */
  155. /* Reattache isles */
  156. if (plus->built >= GV_BUILD_ATTACH_ISLES)
  157. Vect_attach_isles(Map, &abox);
  158. /* Reattach centroids */
  159. if (plus->built >= GV_BUILD_CENTROIDS)
  160. Vect_attach_centroids(Map, &abox);
  161. }
  162. /* Add to category index */
  163. if (plus->update_cidx) {
  164. for (s = 1; s < 3; s++) {
  165. if (new_area[s - 1] > 0) {
  166. Vect__add_area_cats_to_cidx(Map, new_area[s - 1]);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. /* Attach centroid */
  173. if (plus->built >= GV_BUILD_CENTROIDS) {
  174. if (type == GV_CENTROID) {
  175. sel_area = Vect_find_area(Map, points->x[0], points->y[0]);
  176. G_debug(3, " new centroid %d is in area %d", line, sel_area);
  177. if (sel_area > 0) {
  178. Area = plus->Area[sel_area];
  179. Line = plus->Line[line];
  180. if (Area->centroid == 0) { /* first centroid */
  181. G_debug(3, " first centroid -> attach to area");
  182. Area->centroid = line;
  183. Line->left = sel_area;
  184. if (plus->update_cidx) {
  185. Vect__add_area_cats_to_cidx(Map, sel_area);
  186. }
  187. }
  188. else { /* duplicate centroid */
  189. G_debug(3,
  190. " duplicate centroid -> do not attach to area");
  191. Line->left = -sel_area;
  192. }
  193. }
  194. }
  195. }
  196. /* Add cetegory index */
  197. for (i = 0; i < cats->n_cats; i++) {
  198. dig_cidx_add_cat_sorted(plus, cats->field[i], cats->cat[i], line,
  199. type);
  200. }
  201. return;
  202. }
  203. static off_t V1__rewrite_line_nat(struct Map_info *Map, off_t offset, int type,
  204. const struct line_pnts *points, const struct line_cats *cats);
  205. /*!
  206. \brief Writes feature to 'coor' file
  207. \param Map pointer to Map_info structure
  208. \param type feature type
  209. \param points feature geometry
  210. \param cats feature categories
  211. \return feature offset into file
  212. \return -1 on error
  213. */
  214. off_t V1_write_line_nat(struct Map_info *Map,
  215. int type, const struct line_pnts *points, const struct line_cats *cats)
  216. {
  217. off_t offset;
  218. if (dig_fseek(&(Map->dig_fp), 0L, SEEK_END) == -1) /* set to end of file */
  219. return -1;
  220. offset = dig_ftell(&(Map->dig_fp));
  221. if (offset == -1)
  222. return -1;
  223. return V1__rewrite_line_nat(Map, offset, type, points, cats);
  224. }
  225. /*!
  226. \brief Writes feature to 'coor' file (topology level)
  227. \param Map pointer to Map_info structure
  228. \param type feature type
  229. \param points feature geometry
  230. \param cats feature categories
  231. \return new feature id
  232. \return -1 on error
  233. */
  234. off_t V2_write_line_nat(struct Map_info *Map,
  235. int type, const struct line_pnts *points, const struct line_cats *cats)
  236. {
  237. int line;
  238. off_t offset;
  239. struct Plus_head *plus;
  240. struct bound_box box;
  241. line = 0;
  242. G_debug(3, "V2_write_line_nat()");
  243. offset = V1_write_line_nat(Map, type, points, cats);
  244. if (offset < 0)
  245. return -1;
  246. /* Update topology */
  247. plus = &(Map->plus);
  248. /* Add line */
  249. if (plus->built >= GV_BUILD_BASE) {
  250. line = dig_add_line(plus, type, points, offset);
  251. G_debug(3, " line added to topo with id = %d", line);
  252. dig_line_box(points, &box);
  253. dig_line_set_box(plus, line, &box);
  254. if (line == 1)
  255. Vect_box_copy(&(plus->box), &box);
  256. else
  257. Vect_box_extend(&(plus->box), &box);
  258. }
  259. add_line_to_topo(Map,
  260. line, points, cats);
  261. G_debug(3, "updated lines : %d , updated nodes : %d", plus->n_uplines,
  262. plus->n_upnodes);
  263. /* returns int line, but is defined as off_t for compatibility with
  264. * Write_line_array in write.c */
  265. return line;
  266. }
  267. /*!
  268. \brief Rewrites feature at the given offset.
  269. If the number of points or cats differs from the original one or
  270. the type is changed: GV_POINTS -> GV_LINES or GV_LINES ->
  271. GV_POINTS, the old one is deleted and the new is appended to the
  272. end of the file.
  273. Old feature is deleted (marked as dead), new feature written.
  274. \param Map pointer to Map_info structure
  275. \param offset feature offset
  276. \param type feature type
  277. \param points feature geometry
  278. \param cats feature categories
  279. \return feature offset (rewriten feature)
  280. \return -1 on error
  281. */
  282. off_t V1_rewrite_line_nat(struct Map_info *Map,
  283. off_t offset,
  284. int type,
  285. const struct line_pnts *points, const struct line_cats *cats)
  286. {
  287. int old_type;
  288. struct line_pnts *old_points;
  289. struct line_cats *old_cats;
  290. off_t new_offset;
  291. /* TODO: enable points and cats == NULL */
  292. /* First compare numbers of points and cats with tha old one */
  293. old_points = Vect_new_line_struct();
  294. old_cats = Vect_new_cats_struct();
  295. old_type = V1_read_line_nat(Map, old_points, old_cats, offset);
  296. if (old_type == -1)
  297. return (-1); /* error */
  298. if (old_type != -2 /* EOF -> write new line */
  299. && points->n_points == old_points->n_points
  300. && cats->n_cats == old_cats->n_cats
  301. && (((type & GV_POINTS) && (old_type & GV_POINTS))
  302. || ((type & GV_LINES) && (old_type & GV_LINES)))) {
  303. /* equal -> overwrite the old */
  304. return V1__rewrite_line_nat(Map, offset, type, points, cats);
  305. }
  306. else {
  307. /* differ -> delete the old and append new */
  308. /* delete old */
  309. V1_delete_line_nat(Map, offset);
  310. /* write new */
  311. if (dig_fseek(&(Map->dig_fp), 0L, SEEK_END) == -1) /* end of file */
  312. return -1;
  313. new_offset = dig_ftell(&(Map->dig_fp));
  314. if (new_offset == -1)
  315. return -1;
  316. return V1__rewrite_line_nat(Map, new_offset, type, points, cats);
  317. }
  318. }
  319. /*!
  320. \brief Rewrites feature (topology level)
  321. Old feature is deleted (marked as dead), new feature written.
  322. \param Map pointer to Map_info structure
  323. \param line feature id
  324. \param type feature type
  325. \param points feature geometry
  326. \param cats feature category
  327. \return new feature id
  328. \return -1 on error
  329. */
  330. int V2_rewrite_line_nat(struct Map_info *Map,
  331. int line,
  332. int type,
  333. const struct line_pnts *points, const struct line_cats *cats)
  334. {
  335. /* TODO: this is just quick shortcut because we have already V2_delete_nat()
  336. * and V2_write_nat() this function first deletes old line
  337. * and then writes new one. It is not very effective if number of points
  338. * and cats was not changed or topology is not changed (nodes not moved,
  339. * angles not changed etc.) */
  340. V2_delete_line_nat(Map, line);
  341. return (V2_write_line_nat(Map, type, points, cats));
  342. }
  343. /*!
  344. \brief Rewrites feature at the given offset.
  345. \param Map pointer to Map_info structure
  346. \param offset feature offset
  347. \param type feature type
  348. \param points feature geometry
  349. \param cats feature categories
  350. \return feature offset
  351. \return -1 on error
  352. */
  353. off_t V1__rewrite_line_nat(struct Map_info *Map,
  354. off_t offset,
  355. int type,
  356. const struct line_pnts *points, const struct line_cats *cats)
  357. {
  358. int i, n_points;
  359. char rhead, nc;
  360. short field;
  361. struct gvfile *dig_fp;
  362. dig_set_cur_port(&(Map->head.port));
  363. dig_fp = &(Map->dig_fp);
  364. if (dig_fseek(dig_fp, offset, 0) == -1)
  365. return -1;
  366. /* first byte: 0 bit: 1 - alive, 0 - dead
  367. * 1 bit: 1 - categories, 0 - no category
  368. * 2-3 bit: store type
  369. * 4-5 bit: reserved for store type expansion
  370. * 6-7 bit: not used
  371. */
  372. rhead = (char)dig_type_to_store(type);
  373. rhead <<= 2;
  374. if (cats->n_cats > 0) {
  375. rhead |= 0x02;
  376. }
  377. rhead |= 0x01; /* written/rewritten is always alive */
  378. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp)) {
  379. return -1;
  380. }
  381. if (cats->n_cats > 0) {
  382. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  383. if (0 >= dig__fwrite_port_I(&(cats->n_cats), 1, dig_fp))
  384. return -1;
  385. }
  386. else { /* coor format 5.0 */
  387. nc = (char)cats->n_cats;
  388. if (0 >= dig__fwrite_port_C(&nc, 1, dig_fp))
  389. return -1;
  390. }
  391. if (cats->n_cats > 0) {
  392. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  393. if (0 >=
  394. dig__fwrite_port_I(cats->field, cats->n_cats, dig_fp))
  395. return -1;
  396. }
  397. else { /* coor format 5.0 */
  398. for (i = 0; i < cats->n_cats; i++) {
  399. field = (short)cats->field[i];
  400. if (0 >= dig__fwrite_port_S(&field, 1, dig_fp))
  401. return -1;
  402. }
  403. }
  404. if (0 >= dig__fwrite_port_I(cats->cat, cats->n_cats, dig_fp))
  405. return -1;
  406. }
  407. }
  408. if (type & GV_POINTS) {
  409. n_points = 1;
  410. }
  411. else {
  412. n_points = points->n_points;
  413. if (0 >= dig__fwrite_port_I(&n_points, 1, dig_fp))
  414. return -1;
  415. }
  416. if (0 >= dig__fwrite_port_D(points->x, n_points, dig_fp))
  417. return -1;
  418. if (0 >= dig__fwrite_port_D(points->y, n_points, dig_fp))
  419. return -1;
  420. if (Map->head.with_z) {
  421. if (0 >= dig__fwrite_port_D(points->z, n_points, dig_fp))
  422. return -1;
  423. }
  424. if (0 != dig_fflush(dig_fp))
  425. return -1;
  426. return offset;
  427. }
  428. /*!
  429. \brief Deletes feature at the given offset (level 1)
  430. \param Map pointer Map_info structure
  431. \param offset feature offset
  432. \return 0 on success
  433. \return -1 on error
  434. */
  435. int V1_delete_line_nat(struct Map_info *Map, off_t offset)
  436. {
  437. char rhead;
  438. struct gvfile *dig_fp;
  439. G_debug(3, "V1_delete_line_nat(), offset = %lu", (unsigned long) offset);
  440. dig_set_cur_port(&(Map->head.port));
  441. dig_fp = &(Map->dig_fp);
  442. if (dig_fseek(dig_fp, offset, 0) == -1)
  443. return -1;
  444. /* read old */
  445. if (0 >= dig__fread_port_C(&rhead, 1, dig_fp))
  446. return (-1);
  447. rhead &= 0xFE;
  448. if (dig_fseek(dig_fp, offset, 0) == -1)
  449. return -1;
  450. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp))
  451. return -1;
  452. if (0 != dig_fflush(dig_fp))
  453. return -1;
  454. return 0;
  455. }
  456. /*!
  457. \brief Deletes feature (topology level).
  458. \param pointer to Map_info structure
  459. \param line feature id
  460. \return 0 on success
  461. \return -1 on error
  462. */
  463. int V2_delete_line_nat(struct Map_info *Map, int line)
  464. {
  465. return V2__delete_line(Map, line, V1_delete_line_nat);
  466. }
  467. /*!
  468. \brief Restores feature at the given offset.
  469. \param Map pointer to Map_info structure
  470. \param offset feature offset
  471. \return 0 on success
  472. \return -1 on error
  473. */
  474. int V1_restore_line_nat(struct Map_info *Map, off_t offset)
  475. {
  476. char rhead;
  477. struct gvfile *dig_fp;
  478. G_debug(3, "V1_restore_line_nat(), offset = %lu", (unsigned long) offset);
  479. dig_set_cur_port(&(Map->head.port));
  480. dig_fp = &(Map->dig_fp);
  481. if (dig_fseek(dig_fp, offset, 0) == -1)
  482. return -1;
  483. /* read old */
  484. if (0 >= dig__fread_port_C(&rhead, 1, dig_fp))
  485. return (-1);
  486. /* mark as alive */
  487. rhead |= 1;
  488. /* write new */
  489. if (dig_fseek(dig_fp, offset, 0) == -1)
  490. return -1;
  491. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp))
  492. return -1;
  493. if (0 != dig_fflush(dig_fp))
  494. return -1;
  495. return 0;
  496. }
  497. /*!
  498. \brief Restores feature (topology level)
  499. \param Map pointer to Map_info structure
  500. \param line feature id
  501. \param offset feature offset
  502. \return 0 on success
  503. \return -1 on error
  504. */
  505. int V2_restore_line_nat(struct Map_info *Map, int line, off_t offset)
  506. {
  507. int i, ret, type;
  508. struct P_line *Line;
  509. struct Plus_head *plus;
  510. struct bound_box box;
  511. static struct line_pnts *points = NULL;
  512. static struct line_cats *cats = NULL;
  513. Line = NULL;
  514. type = 0;
  515. G_debug(3, "V2_restore_line_nat(), line = %d", line);
  516. plus = &(Map->plus);
  517. if (plus->built >= GV_BUILD_BASE) {
  518. Line = Map->plus.Line[line];
  519. if (Line != NULL)
  520. G_fatal_error(_("Attempt to restore alive feature"));
  521. }
  522. if (!points) {
  523. points = Vect_new_line_struct();
  524. }
  525. if (!cats) {
  526. cats = Vect_new_cats_struct();
  527. }
  528. /* restore the line in coor */
  529. ret = V1_restore_line_nat(Map, offset);
  530. if (ret == -1) {
  531. return ret;
  532. }
  533. /* read feature geometry */
  534. type = V1_read_line_nat(Map, points, cats, offset);
  535. if (type < 0) {
  536. return -1;
  537. }
  538. /* update category index */
  539. if (plus->update_cidx) {
  540. for (i = 0; i < cats->n_cats; i++) {
  541. dig_cidx_add_cat(plus, cats->field[i], cats->cat[i], line, type);
  542. }
  543. }
  544. /* restore the line from topo */
  545. if (plus->built >= GV_BUILD_BASE) {
  546. dig_restore_line(plus, line, type, points, offset);
  547. G_debug(3, " line restored in topo with id = %d", line);
  548. dig_line_box(points, &box);
  549. dig_line_set_box(plus, line, &box);
  550. Vect_box_extend(&(plus->box), &box);
  551. }
  552. add_line_to_topo(Map,
  553. line, points, cats);
  554. G_debug(3, "updated lines : %d , updated nodes : %d", plus->n_uplines,
  555. plus->n_upnodes);
  556. return ret;
  557. }