write.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*!
  2. \file lib/vector/Vlib/write.c
  3. \brief Vector library - write vector features
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2010 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 Radim Blazek
  9. \author Updated by Martin Landa <landa.martin gmail.com> (restore lines, OGR support)
  10. */
  11. #include <sys/types.h>
  12. #include <grass/glocale.h>
  13. #include <grass/vector.h>
  14. static off_t write_dummy()
  15. {
  16. G_warning("Vect_write_line() %s",
  17. _("for this format/level not supported"));
  18. return -1;
  19. }
  20. static off_t rewrite_dummy()
  21. {
  22. G_warning("Vect_rewrite_line() %s",
  23. _("for this format/level not supported"));
  24. return -1;
  25. }
  26. static int delete_dummy()
  27. {
  28. G_warning("Vect_delete_line() %s",
  29. _("for this format/level not supported"));
  30. return -1;
  31. }
  32. static int restore_dummy()
  33. {
  34. G_warning("Vect_restore_line() %s",
  35. _("for this format/level not supported"));
  36. return -1;
  37. }
  38. #ifndef HAVE_OGR
  39. static int format()
  40. {
  41. G_fatal_error(_("Requested format is not compiled in this version"));
  42. return 0;
  43. }
  44. static off_t format_l()
  45. {
  46. G_fatal_error(_("Requested format is not compiled in this version"));
  47. return 0;
  48. }
  49. #endif
  50. static off_t (*Vect_write_line_array[][3]) () = {
  51. {
  52. write_dummy, V1_write_line_nat, V2_write_line_nat}
  53. #ifdef HAVE_OGR
  54. , {
  55. write_dummy, V1_write_line_ogr, V2_write_line_ogr}
  56. , {
  57. write_dummy, V1_write_line_ogr, V2_write_line_ogr}
  58. #else
  59. , {
  60. write_dummy, format_l, format_l}
  61. , {
  62. write_dummy, format_l, format_l}
  63. #endif
  64. };
  65. static off_t (*Vect_rewrite_line_array[][3]) () = {
  66. {
  67. rewrite_dummy, V1_rewrite_line_nat, V2_rewrite_line_nat}
  68. #ifdef HAVE_OGR
  69. , {
  70. rewrite_dummy, V1_rewrite_line_ogr, V2_rewrite_line_ogr}
  71. , {
  72. rewrite_dummy, V1_rewrite_line_ogr, V2_rewrite_line_ogr}
  73. #else
  74. , {
  75. rewrite_dummy, format, format}
  76. , {
  77. rewrite_dummy, format, format}
  78. #endif
  79. };
  80. static int (*Vect_delete_line_array[][3]) () = {
  81. {
  82. delete_dummy, V1_delete_line_nat, V2_delete_line_nat}
  83. #ifdef HAVE_OGR
  84. , {
  85. delete_dummy, V1_delete_line_ogr, V2_delete_line_nat}
  86. , {
  87. delete_dummy, V1_delete_line_ogr, V2_delete_line_nat}
  88. #else
  89. , {
  90. delete_dummy, format, format}
  91. , {
  92. delete_dummy, format, format}
  93. #endif
  94. };
  95. static int (*Vect_restore_line_array[][3]) () = {
  96. {
  97. restore_dummy, restore_dummy, V2_restore_line_nat}
  98. #ifdef HAVE_OGR
  99. , {
  100. restore_dummy, restore_dummy, restore_dummy}
  101. , {
  102. restore_dummy, restore_dummy, restore_dummy}
  103. #else
  104. , {
  105. restore_dummy, format, format}
  106. , {
  107. restore_dummy, format, format}
  108. #endif
  109. };
  110. /*!
  111. \brief Writes new feature to the end of file
  112. Vector map topology is not required.
  113. The function calls G_fatal_error() on error.
  114. \param Map pointer to vector map
  115. \param type feature type
  116. \param points feature geometry
  117. \param cats feature categories
  118. \return new feature id (level 2)
  119. \return offset into file where the feature starts (level 1)
  120. */
  121. off_t
  122. Vect_write_line(struct Map_info *Map, int type,
  123. const struct line_pnts *points, const struct line_cats *cats)
  124. {
  125. off_t offset;
  126. G_debug(3, "Vect_write_line(): name = %s, format = %d, level = %d",
  127. Map->name, Map->format, Map->level);
  128. if (!VECT_OPEN(Map))
  129. G_fatal_error(_("Unable to write feature, vector map is not opened"));
  130. dig_line_reset_updated(&(Map->plus));
  131. dig_node_reset_updated(&(Map->plus));
  132. if (!(Map->plus.update_cidx)) {
  133. Map->plus.cidx_up_to_date = 0;
  134. }
  135. offset =
  136. (*Vect_write_line_array[Map->format][Map->level]) (Map, type, points,
  137. cats);
  138. if (offset == -1)
  139. G_fatal_error(_("Unable to write feature (negative offset)"));
  140. /* NOTE: returns new line id on level 2 and file offset on level 1 */
  141. return offset;
  142. }
  143. /*!
  144. \brief Rewrites feature info at the given offset.
  145. Vector map must be opened with full topology (level 2).
  146. The number of points or cats or type may change. If necessary, the
  147. old feature is deleted and new is written.
  148. This function calls G_fatal_error() on error.
  149. \param Map pointer to vector map
  150. \param line feature id
  151. \param type feature type
  152. \param points feature geometry
  153. \param cats feature categories
  154. \return feature offset
  155. \return -1 on error
  156. */
  157. off_t
  158. Vect_rewrite_line(struct Map_info *Map, int line, int type,
  159. const struct line_pnts *points, const struct line_cats *cats)
  160. {
  161. off_t ret, offset;
  162. G_debug(3, "Vect_rewrite_line(): name = %s, line = %d", Map->name, line);
  163. if (!VECT_OPEN(Map))
  164. G_fatal_error(_("Unable to rewrite feature, vector map is not opened"));
  165. dig_line_reset_updated(&(Map->plus));
  166. dig_node_reset_updated(&(Map->plus));
  167. if (!(Map->plus.update_cidx)) {
  168. Map->plus.cidx_up_to_date = 0;
  169. }
  170. offset = Map->plus.Line[line]->offset;
  171. ret =
  172. (*Vect_rewrite_line_array[Map->format][Map->level]) (Map, line, type, offset,
  173. points, cats);
  174. if (ret == -1)
  175. G_fatal_error(_("Unable to rewrite feature %d"), line);
  176. return ret;
  177. }
  178. /*!
  179. \brief Delete feature
  180. Vector map must be opened with full topology (level 2).
  181. This function calls G_fatal_error() on error.
  182. \param Map pointer to vector map
  183. \param line feature id
  184. \return 0 on success
  185. \return -1 on error
  186. */
  187. int Vect_delete_line(struct Map_info *Map, int line)
  188. {
  189. int ret;
  190. G_debug(3, "Vect_delete_line(): name = %s, line = %d", Map->name, line);
  191. if (Map->level < 2) {
  192. G_fatal_error(_("Unable to delete feature %d, "
  193. "vector map <%s> is not opened on topology level"),
  194. line, Map->name);
  195. }
  196. if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
  197. G_fatal_error(_("Unable to delete feature %d, "
  198. "vector map <%s> is not opened in 'write' mode"),
  199. line, Map->name);
  200. }
  201. dig_line_reset_updated(&(Map->plus));
  202. dig_node_reset_updated(&(Map->plus));
  203. if (!(Map->plus.update_cidx)) {
  204. Map->plus.cidx_up_to_date = 0;
  205. }
  206. ret = (*Vect_delete_line_array[Map->format][Map->level]) (Map, line);
  207. if (ret == -1)
  208. G_fatal_error(_("Unable to delete feature id %d from vector map <%s>"),
  209. line, Vect_get_full_name(Map));
  210. return ret;
  211. }
  212. /*!
  213. \brief Restore previously deleted feature
  214. Vector map must be opened with full topology (level 2).
  215. This function calls G_fatal_error() on error.
  216. \param Map pointer to vector map
  217. \param line feature id to be restored
  218. \return 0 on success
  219. \return -1 on error
  220. */
  221. int Vect_restore_line(struct Map_info *Map, int line, off_t offset)
  222. {
  223. int ret;
  224. G_debug(3, "Vect_restore_line(): name = %s, line = %d", Map->name, line);
  225. if (Map->level < 2) {
  226. G_fatal_error(_("Unable to restore feature %d, "
  227. "vector map <%s> is not opened on topology level"),
  228. line, Map->name);
  229. }
  230. if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
  231. G_fatal_error(_("Unable to restore feature %d, "
  232. "vector map <%s> is not opened in 'write' mode"),
  233. line, Map->name);
  234. }
  235. dig_line_reset_updated(&(Map->plus));
  236. dig_node_reset_updated(&(Map->plus));
  237. if (!(Map->plus.update_cidx)) {
  238. Map->plus.cidx_up_to_date = 0;
  239. }
  240. ret = (*Vect_restore_line_array[Map->format][Map->level]) (Map, line, offset);
  241. if (ret == -1)
  242. G_fatal_error(_("Unable to restore feature %d from vector map <%s>"),
  243. line, Map->name);
  244. return ret;
  245. }