write.c 6.7 KB

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