write.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*!
  2. \file write.c
  3. \brief Vector library - write vector features
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Radim Blazek
  11. \date 2001
  12. */
  13. #include <grass/gis.h>
  14. #include <grass/glocale.h>
  15. #include <grass/Vect.h>
  16. static long 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 long format_l()
  47. {
  48. G_fatal_error(_("Requested format is not compiled in this version"));
  49. return 0;
  50. }
  51. #endif
  52. static long (*Write_line_array[][3]) () = {
  53. {
  54. write_dummy, V1_write_line_nat, V2_write_line_nat}
  55. #ifdef HAVE_OGR
  56. , {
  57. write_dummy, write_dummy, 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 (table)
  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 offset into file where the feature starts
  104. */
  105. long
  106. Vect_write_line(struct Map_info *Map,
  107. int type, struct line_pnts *points, struct line_cats *cats)
  108. {
  109. long offset;
  110. G_debug(3, "Vect_write_line(): name = %s, format = %d, level = %d",
  111. Map->name, Map->format, Map->level);
  112. if (!VECT_OPEN(Map))
  113. G_fatal_error(_("Unable to write feature, vector map is not opened"));
  114. dig_line_reset_updated(&(Map->plus));
  115. dig_node_reset_updated(&(Map->plus));
  116. if (!(Map->plus.update_cidx)) {
  117. Map->plus.cidx_up_to_date = 0;
  118. }
  119. offset =
  120. (*Write_line_array[Map->format][Map->level]) (Map, type, points,
  121. cats);
  122. if (offset == -1)
  123. G_fatal_error(_("Unable to write feature (negative offset)"));
  124. return offset;
  125. }
  126. /*!
  127. \brief Rewrites feature info at the given offset.
  128. The number of points or cats or type may change. If necessary, the
  129. old feature is deleted and new is written.
  130. This function calls G_fatal_error() on error.
  131. \param Map pointer to vector map
  132. \param line feature id
  133. \param type feature type
  134. \param points feature geometry
  135. \param cats feature categories
  136. \return new feature id
  137. \return -1 on error
  138. */
  139. int
  140. Vect_rewrite_line(struct Map_info *Map,
  141. int line,
  142. int type, struct line_pnts *points, struct line_cats *cats)
  143. {
  144. long ret;
  145. G_debug(3, "Vect_rewrite_line(): name = %s, line = %d", Map->name, line);
  146. if (!VECT_OPEN(Map))
  147. G_fatal_error(_("Unable to rewrite feature, vector map is not opened"));
  148. dig_line_reset_updated(&(Map->plus));
  149. dig_node_reset_updated(&(Map->plus));
  150. if (!(Map->plus.update_cidx)) {
  151. Map->plus.cidx_up_to_date = 0;
  152. }
  153. ret =
  154. (*Vect_rewrite_line_array[Map->format][Map->level]) (Map, line, type,
  155. points, cats);
  156. if (ret == -1)
  157. G_fatal_error(_("Unable to rewrite feature %d"), line);
  158. return ret;
  159. }
  160. /*!
  161. \brief Delete feature
  162. Vector map must be opened on topo level 2.
  163. This function calls G_fatal_error() on error.
  164. \param Map pointer to vector map
  165. \param line feature id
  166. \return 0 on success
  167. \return -1 on error
  168. */
  169. int Vect_delete_line(struct Map_info *Map, int line)
  170. {
  171. int ret;
  172. G_debug(3, "Vect_delete_line(): name = %s, line = %d", Map->name, line);
  173. if (Map->level < 2) {
  174. G_fatal_error(_("Unable to delete feature %d, "
  175. "vector map <%s> is not opened on topology level"),
  176. line, Map->name);
  177. }
  178. if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
  179. G_fatal_error(_("Unable to delete feature %d, "
  180. "vector map <%s> is not opened in 'write' mode"),
  181. line, Map->name);
  182. }
  183. dig_line_reset_updated(&(Map->plus));
  184. dig_node_reset_updated(&(Map->plus));
  185. if (!(Map->plus.update_cidx)) {
  186. Map->plus.cidx_up_to_date = 0;
  187. }
  188. ret = (*Vect_delete_line_array[Map->format][Map->level]) (Map, line);
  189. if (ret == -1)
  190. G_fatal_error(_("Unable to feature %d from vector map <%s>"),
  191. line, Map->name);
  192. return ret;
  193. }
  194. /*!
  195. \brief Restore previously deleted feature
  196. Vector map must be opened on topo level 2.
  197. This function calls G_fatal_error() on error.
  198. \param Map pointer to vector map
  199. \param line feature id to be deleted
  200. \return 0 on success
  201. \return -1 on error
  202. */
  203. int Vect_restore_line(struct Map_info *Map, int line, long offset)
  204. {
  205. int ret;
  206. G_debug(3, "Vect_restore_line(): name = %s, line = %d", Map->name, line);
  207. if (Map->level < 2) {
  208. G_fatal_error(_("Unable to restore feature %d, "
  209. "vector map <%s> is not opened on topology level"),
  210. line, Map->name);
  211. }
  212. if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
  213. G_fatal_error(_("Unable to restore feature %d, "
  214. "vector map <%s> is not opened in 'write' mode"),
  215. line, Map->name);
  216. }
  217. dig_line_reset_updated(&(Map->plus));
  218. dig_node_reset_updated(&(Map->plus));
  219. if (!(Map->plus.update_cidx)) {
  220. Map->plus.cidx_up_to_date = 0;
  221. }
  222. ret = (*Vect_restore_line_array[Map->format][Map->level]) (Map, line, offset);
  223. if (ret == -1)
  224. G_fatal_error(_("Unable to restore feature %d from vector map <%s>"),
  225. line, Map->name);
  226. return ret;
  227. }