write_nat.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*!
  2. \file lib/vector/Vlib/write_nat.c
  3. \brief Vector library - write/modify vector feature (native format)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. Operations:
  6. - Add feature
  7. - Rewrite feature
  8. - Delete feature
  9. - Restore feature
  10. (C) 2001-2010 by the GRASS Development Team
  11. This program is free software under the GNU General Public License
  12. (>=v2). Read the file COPYING that comes with GRASS for details.
  13. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  14. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  15. \author V*_restore_line() by Martin Landa <landa.martin gmail.com> (2008)
  16. */
  17. #include <grass/config.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include <grass/gis.h>
  22. #include <grass/vector.h>
  23. #include <grass/glocale.h>
  24. static off_t rewrite_line_nat(struct Map_info *, off_t, int,
  25. const struct line_pnts *, const struct line_cats *);
  26. /*!
  27. \brief Writes feature to 'coor' file (level 1)
  28. \param Map pointer to Map_info structure
  29. \param type feature type
  30. \param points feature geometry
  31. \param cats feature categories
  32. \return feature offset into file
  33. \return -1 on error
  34. */
  35. off_t V1_write_line_nat(struct Map_info *Map,
  36. int type, const struct line_pnts *points, const struct line_cats *cats)
  37. {
  38. off_t offset;
  39. if (dig_fseek(&(Map->dig_fp), 0L, SEEK_END) == -1) /* set to end of file */
  40. return -1;
  41. offset = dig_ftell(&(Map->dig_fp));
  42. if (offset == -1)
  43. return -1;
  44. return rewrite_line_nat(Map, offset, type, points, cats);
  45. }
  46. /*!
  47. \brief Writes feature to 'coor' file (topology level)
  48. \param Map pointer to Map_info structure
  49. \param type feature type
  50. \param points feature geometry
  51. \param cats feature categories
  52. \return new feature id
  53. \return -1 on error
  54. */
  55. off_t V2_write_line_nat(struct Map_info *Map,
  56. int type, const struct line_pnts *points, const struct line_cats *cats)
  57. {
  58. return V2__write_line(Map, type, points, cats, V1_write_line_nat);
  59. }
  60. /*!
  61. \brief Rewrites feature at the given offset (level 1)
  62. If the number of points or cats differs from the original one or
  63. the type is changed: GV_POINTS -> GV_LINES or GV_LINES ->
  64. GV_POINTS, the old one is deleted and the new is appended to the
  65. end of the file.
  66. Old feature is deleted (marked as dead), new feature written.
  67. \param Map pointer to Map_info structure
  68. \param offset feature offset
  69. \param type feature type
  70. \param points feature geometry
  71. \param cats feature categories
  72. \return feature offset (rewriten feature)
  73. \return -1 on error
  74. */
  75. off_t V1_rewrite_line_nat(struct Map_info *Map,
  76. off_t offset,
  77. int type,
  78. const struct line_pnts *points, const struct line_cats *cats)
  79. {
  80. int old_type;
  81. struct line_pnts *old_points;
  82. struct line_cats *old_cats;
  83. off_t new_offset;
  84. /* TODO: enable points and cats == NULL */
  85. /* First compare numbers of points and cats with tha old one */
  86. old_points = Vect_new_line_struct();
  87. old_cats = Vect_new_cats_struct();
  88. old_type = V1_read_line_nat(Map, old_points, old_cats, offset);
  89. if (old_type == -1)
  90. return (-1); /* error */
  91. if (old_type != -2 /* EOF -> write new line */
  92. && points->n_points == old_points->n_points
  93. && cats->n_cats == old_cats->n_cats
  94. && (((type & GV_POINTS) && (old_type & GV_POINTS))
  95. || ((type & GV_LINES) && (old_type & GV_LINES)))) {
  96. /* equal -> overwrite the old */
  97. return rewrite_line_nat(Map, offset, type, points, cats);
  98. }
  99. else {
  100. /* differ -> delete the old and append new */
  101. /* delete old */
  102. V1_delete_line_nat(Map, offset);
  103. /* write new */
  104. if (dig_fseek(&(Map->dig_fp), 0L, SEEK_END) == -1) /* end of file */
  105. return -1;
  106. new_offset = dig_ftell(&(Map->dig_fp));
  107. if (new_offset == -1)
  108. return -1;
  109. return rewrite_line_nat(Map, new_offset, type, points, cats);
  110. }
  111. }
  112. /*!
  113. \brief Rewrites feature (topology level)
  114. Old feature is deleted (marked as dead), new feature written.
  115. \param Map pointer to Map_info structure
  116. \param line feature id
  117. \param type feature type
  118. \param points feature geometry
  119. \param cats feature category
  120. \return new feature id
  121. \return -1 on error
  122. */
  123. int V2_rewrite_line_nat(struct Map_info *Map,
  124. int line,
  125. int type,
  126. const struct line_pnts *points, const struct line_cats *cats)
  127. {
  128. /* TODO: this is just quick shortcut because we have already V2_delete_nat()
  129. * and V2_write_nat() this function first deletes old line
  130. * and then writes new one. It is not very effective if number of points
  131. * and cats was not changed or topology is not changed (nodes not moved,
  132. * angles not changed etc.) */
  133. V2_delete_line_nat(Map, line);
  134. return (V2_write_line_nat(Map, type, points, cats));
  135. }
  136. /*!
  137. \brief Rewrites feature at the given offset.
  138. \param Map pointer to Map_info structure
  139. \param offset feature offset
  140. \param type feature type
  141. \param points feature geometry
  142. \param cats feature categories
  143. \return feature offset
  144. \return -1 on error
  145. */
  146. off_t rewrite_line_nat(struct Map_info *Map,
  147. off_t offset,
  148. int type,
  149. const struct line_pnts *points, const struct line_cats *cats)
  150. {
  151. int i, n_points;
  152. char rhead, nc;
  153. short field;
  154. struct gvfile *dig_fp;
  155. dig_set_cur_port(&(Map->head.port));
  156. dig_fp = &(Map->dig_fp);
  157. if (dig_fseek(dig_fp, offset, 0) == -1)
  158. return -1;
  159. /* first byte: 0 bit: 1 - alive, 0 - dead
  160. * 1 bit: 1 - categories, 0 - no category
  161. * 2-3 bit: store type
  162. * 4-5 bit: reserved for store type expansion
  163. * 6-7 bit: not used
  164. */
  165. rhead = (char)dig_type_to_store(type);
  166. rhead <<= 2;
  167. if (cats->n_cats > 0) {
  168. rhead |= 0x02;
  169. }
  170. rhead |= 0x01; /* written/rewritten is always alive */
  171. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp)) {
  172. return -1;
  173. }
  174. if (cats->n_cats > 0) {
  175. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  176. if (0 >= dig__fwrite_port_I(&(cats->n_cats), 1, dig_fp))
  177. return -1;
  178. }
  179. else { /* coor format 5.0 */
  180. nc = (char)cats->n_cats;
  181. if (0 >= dig__fwrite_port_C(&nc, 1, dig_fp))
  182. return -1;
  183. }
  184. if (cats->n_cats > 0) {
  185. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  186. if (0 >=
  187. dig__fwrite_port_I(cats->field, cats->n_cats, dig_fp))
  188. return -1;
  189. }
  190. else { /* coor format 5.0 */
  191. for (i = 0; i < cats->n_cats; i++) {
  192. field = (short)cats->field[i];
  193. if (0 >= dig__fwrite_port_S(&field, 1, dig_fp))
  194. return -1;
  195. }
  196. }
  197. if (0 >= dig__fwrite_port_I(cats->cat, cats->n_cats, dig_fp))
  198. return -1;
  199. }
  200. }
  201. if (type & GV_POINTS) {
  202. n_points = 1;
  203. }
  204. else {
  205. n_points = points->n_points;
  206. if (0 >= dig__fwrite_port_I(&n_points, 1, dig_fp))
  207. return -1;
  208. }
  209. if (0 >= dig__fwrite_port_D(points->x, n_points, dig_fp))
  210. return -1;
  211. if (0 >= dig__fwrite_port_D(points->y, n_points, dig_fp))
  212. return -1;
  213. if (Map->head.with_z) {
  214. if (0 >= dig__fwrite_port_D(points->z, n_points, dig_fp))
  215. return -1;
  216. }
  217. if (0 != dig_fflush(dig_fp))
  218. return -1;
  219. return offset;
  220. }
  221. /*!
  222. \brief Deletes feature at the given offset (level 1)
  223. \param Map pointer Map_info structure
  224. \param offset feature offset
  225. \return 0 on success
  226. \return -1 on error
  227. */
  228. int V1_delete_line_nat(struct Map_info *Map, off_t offset)
  229. {
  230. char rhead;
  231. struct gvfile *dig_fp;
  232. G_debug(3, "V1_delete_line_nat(), offset = %lu", (unsigned long) offset);
  233. dig_set_cur_port(&(Map->head.port));
  234. dig_fp = &(Map->dig_fp);
  235. if (dig_fseek(dig_fp, offset, 0) == -1)
  236. return -1;
  237. /* read old */
  238. if (0 >= dig__fread_port_C(&rhead, 1, dig_fp))
  239. return (-1);
  240. rhead &= 0xFE;
  241. if (dig_fseek(dig_fp, offset, 0) == -1)
  242. return -1;
  243. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp))
  244. return -1;
  245. if (0 != dig_fflush(dig_fp))
  246. return -1;
  247. return 0;
  248. }
  249. /*!
  250. \brief Deletes feature (topology level).
  251. \param pointer to Map_info structure
  252. \param line feature id
  253. \return 0 on success
  254. \return -1 on error
  255. */
  256. int V2_delete_line_nat(struct Map_info *Map, int line)
  257. {
  258. return V2__delete_line(Map, line, V1_delete_line_nat);
  259. }
  260. /*!
  261. \brief Restores feature at the given offset.
  262. \param Map pointer to Map_info structure
  263. \param offset feature offset
  264. \return 0 on success
  265. \return -1 on error
  266. */
  267. int V1_restore_line_nat(struct Map_info *Map, off_t offset)
  268. {
  269. char rhead;
  270. struct gvfile *dig_fp;
  271. G_debug(3, "V1_restore_line_nat(), offset = %lu", (unsigned long) offset);
  272. dig_set_cur_port(&(Map->head.port));
  273. dig_fp = &(Map->dig_fp);
  274. if (dig_fseek(dig_fp, offset, 0) == -1)
  275. return -1;
  276. /* read old */
  277. if (0 >= dig__fread_port_C(&rhead, 1, dig_fp))
  278. return (-1);
  279. /* mark as alive */
  280. rhead |= 1;
  281. /* write new */
  282. if (dig_fseek(dig_fp, offset, 0) == -1)
  283. return -1;
  284. if (0 >= dig__fwrite_port_C(&rhead, 1, dig_fp))
  285. return -1;
  286. if (0 != dig_fflush(dig_fp))
  287. return -1;
  288. return 0;
  289. }
  290. /*!
  291. \brief Restores feature (topology level)
  292. \param Map pointer to Map_info structure
  293. \param line feature id
  294. \param offset feature offset
  295. \return 0 on success
  296. \return -1 on error
  297. */
  298. int V2_restore_line_nat(struct Map_info *Map, int line, off_t offset)
  299. {
  300. int i, ret, type;
  301. struct P_line *Line;
  302. struct Plus_head *plus;
  303. struct bound_box box;
  304. static struct line_pnts *points = NULL;
  305. static struct line_cats *cats = NULL;
  306. Line = NULL;
  307. type = 0;
  308. G_debug(3, "V2_restore_line_nat(), line = %d", line);
  309. plus = &(Map->plus);
  310. if (plus->built >= GV_BUILD_BASE) {
  311. Line = Map->plus.Line[line];
  312. if (Line != NULL)
  313. G_fatal_error(_("Attempt to restore alive feature"));
  314. }
  315. if (!points) {
  316. points = Vect_new_line_struct();
  317. }
  318. if (!cats) {
  319. cats = Vect_new_cats_struct();
  320. }
  321. /* restore the line in coor */
  322. ret = V1_restore_line_nat(Map, offset);
  323. if (ret == -1) {
  324. return ret;
  325. }
  326. /* read feature geometry */
  327. type = V1_read_line_nat(Map, points, cats, offset);
  328. if (type < 0) {
  329. return -1;
  330. }
  331. /* update category index */
  332. if (plus->update_cidx) {
  333. for (i = 0; i < cats->n_cats; i++) {
  334. dig_cidx_add_cat(plus, cats->field[i], cats->cat[i], line, type);
  335. }
  336. }
  337. /* restore the line from topo */
  338. if (plus->built >= GV_BUILD_BASE) {
  339. dig_restore_line(plus, line, type, points, offset);
  340. G_debug(3, " line restored in topo with id = %d", line);
  341. dig_line_box(points, &box);
  342. dig_line_set_box(plus, line, &box);
  343. Vect_box_extend(&(plus->box), &box);
  344. }
  345. Vect__add_line_to_topo(Map,
  346. line, points, cats);
  347. G_debug(3, "updated lines : %d , updated nodes : %d", plus->n_uplines,
  348. plus->n_upnodes);
  349. return ret;
  350. }