write.c 6.7 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-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)
  10. */
  11. #include <grass/config.h>
  12. #include <sys/types.h>
  13. #include <grass/gis.h>
  14. #include <grass/glocale.h>
  15. #include <grass/Vect.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, 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 new feature id (level 2) or offset into file where the feature starts (level 1)
  104. */
  105. off_t
  106. Vect_write_line(struct Map_info *Map,
  107. int type, const struct line_pnts *points, const struct line_cats *cats)
  108. {
  109. off_t 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. /* NOTE: returns new line id on level 2 and file offset on level 1 */
  125. return offset;
  126. }
  127. /*!
  128. \brief Rewrites feature info at the given offset.
  129. The number of points or cats or type may change. If necessary, the
  130. old feature is deleted and new is written.
  131. This function calls G_fatal_error() on error.
  132. \param Map pointer to vector map
  133. \param line feature id
  134. \param type feature type
  135. \param points feature geometry
  136. \param cats feature categories
  137. \return new feature id
  138. \return -1 on error
  139. */
  140. int
  141. Vect_rewrite_line(struct Map_info *Map,
  142. int line,
  143. int type, const struct line_pnts *points, const struct line_cats *cats)
  144. {
  145. long ret;
  146. G_debug(3, "Vect_rewrite_line(): name = %s, line = %d", Map->name, line);
  147. if (!VECT_OPEN(Map))
  148. G_fatal_error(_("Unable to rewrite feature, vector map is not opened"));
  149. dig_line_reset_updated(&(Map->plus));
  150. dig_node_reset_updated(&(Map->plus));
  151. if (!(Map->plus.update_cidx)) {
  152. Map->plus.cidx_up_to_date = 0;
  153. }
  154. ret =
  155. (*Vect_rewrite_line_array[Map->format][Map->level]) (Map, line, type,
  156. points, cats);
  157. if (ret == -1)
  158. G_fatal_error(_("Unable to rewrite feature %d"), line);
  159. return ret;
  160. }
  161. /*!
  162. \brief Delete feature
  163. Vector map must be opened on topo level 2.
  164. This function calls G_fatal_error() on error.
  165. \param Map pointer to vector map
  166. \param line feature id
  167. \return 0 on success
  168. \return -1 on error
  169. */
  170. int Vect_delete_line(struct Map_info *Map, int line)
  171. {
  172. int ret;
  173. G_debug(3, "Vect_delete_line(): name = %s, line = %d", Map->name, line);
  174. if (Map->level < 2) {
  175. G_fatal_error(_("Unable to delete feature %d, "
  176. "vector map <%s> is not opened on topology level"),
  177. line, Map->name);
  178. }
  179. if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
  180. G_fatal_error(_("Unable to delete feature %d, "
  181. "vector map <%s> is not opened in 'write' mode"),
  182. line, Map->name);
  183. }
  184. dig_line_reset_updated(&(Map->plus));
  185. dig_node_reset_updated(&(Map->plus));
  186. if (!(Map->plus.update_cidx)) {
  187. Map->plus.cidx_up_to_date = 0;
  188. }
  189. ret = (*Vect_delete_line_array[Map->format][Map->level]) (Map, line);
  190. if (ret == -1)
  191. G_fatal_error(_("Unable to feature %d from vector map <%s>"),
  192. line, Map->name);
  193. return ret;
  194. }
  195. /*!
  196. \brief Restore previously deleted feature
  197. Vector map must be opened on topo level 2.
  198. This function calls G_fatal_error() on error.
  199. \param Map pointer to vector map
  200. \param line feature id to be deleted
  201. \return 0 on success
  202. \return -1 on error
  203. */
  204. int Vect_restore_line(struct Map_info *Map, int line, off_t offset)
  205. {
  206. int ret;
  207. G_debug(3, "Vect_restore_line(): name = %s, line = %d", Map->name, line);
  208. if (Map->level < 2) {
  209. G_fatal_error(_("Unable to restore feature %d, "
  210. "vector map <%s> is not opened on topology level"),
  211. line, Map->name);
  212. }
  213. if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
  214. G_fatal_error(_("Unable to restore feature %d, "
  215. "vector map <%s> is not opened in 'write' mode"),
  216. line, Map->name);
  217. }
  218. dig_line_reset_updated(&(Map->plus));
  219. dig_node_reset_updated(&(Map->plus));
  220. if (!(Map->plus.update_cidx)) {
  221. Map->plus.cidx_up_to_date = 0;
  222. }
  223. ret = (*Vect_restore_line_array[Map->format][Map->level]) (Map, line, offset);
  224. if (ret == -1)
  225. G_fatal_error(_("Unable to restore feature %d from vector map <%s>"),
  226. line, Map->name);
  227. return ret;
  228. }