write.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*!
  2. \file write.c
  3. \brief Vector library - write data
  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. #ifndef HAVE_OGR
  35. static int format()
  36. {
  37. G_fatal_error(_("Requested format is not compiled in this version"));
  38. return 0;
  39. }
  40. static long format_l()
  41. {
  42. G_fatal_error(_("Requested format is not compiled in this version"));
  43. return 0;
  44. }
  45. #endif
  46. static long (*Write_line_array[][3]) () = {
  47. {
  48. write_dummy, V1_write_line_nat, V2_write_line_nat}
  49. #ifdef HAVE_OGR
  50. , {
  51. write_dummy, write_dummy, write_dummy}
  52. #else
  53. , {
  54. write_dummy, format_l, format_l}
  55. #endif
  56. };
  57. static int (*Vect_rewrite_line_array[][3]) () = {
  58. {
  59. rewrite_dummy, rewrite_dummy, V2_rewrite_line_nat}
  60. #ifdef HAVE_OGR
  61. , {
  62. rewrite_dummy, rewrite_dummy, rewrite_dummy}
  63. #else
  64. , {
  65. rewrite_dummy, format, format}
  66. #endif
  67. };
  68. static int (*Vect_delete_line_array[][3]) () = {
  69. {
  70. delete_dummy, delete_dummy, V2_delete_line_nat}
  71. #ifdef HAVE_OGR
  72. , {
  73. delete_dummy, delete_dummy, delete_dummy}
  74. #else
  75. , {
  76. delete_dummy, format, format}
  77. #endif
  78. };
  79. /*!
  80. \brief Writes new line to the end of file (table)
  81. The function calls G_fatal_error() on error.
  82. \param Map pointer to vector map
  83. \param type vector type
  84. \param points line geometry
  85. \param cats line categories
  86. \return offset into file where the line starts
  87. */
  88. long
  89. Vect_write_line(struct Map_info *Map,
  90. int type, struct line_pnts *points, struct line_cats *cats)
  91. {
  92. long offset;
  93. G_debug(3, "Vect_write_line(): name = %s, format = %d, level = %d",
  94. Map->name, Map->format, Map->level);
  95. if (!VECT_OPEN(Map))
  96. G_fatal_error(_("Unable to write line, vector map is not opened"));
  97. dig_line_reset_updated(&(Map->plus));
  98. dig_node_reset_updated(&(Map->plus));
  99. if (!(Map->plus.update_cidx)) {
  100. Map->plus.cidx_up_to_date = 0;
  101. }
  102. offset =
  103. (*Write_line_array[Map->format][Map->level]) (Map, type, points,
  104. cats);
  105. if (offset == -1)
  106. G_fatal_error(_("Unable to write line (negative offset)"));
  107. return offset;
  108. }
  109. /*!
  110. \brief Rewrites line info at the given offset.
  111. The number of points or cats or type may change. If necessary, the
  112. old line is deleted and new is written.
  113. This function calls G_fatal_error() on error.
  114. \param Map pointer to vector map
  115. \param line line id
  116. \param type vector type
  117. \param points line geometry
  118. \param cats line categories
  119. \return number of new line
  120. \return -1 on error
  121. */
  122. int
  123. Vect_rewrite_line(struct Map_info *Map,
  124. int line,
  125. int type, struct line_pnts *points, struct line_cats *cats)
  126. {
  127. long ret;
  128. G_debug(3, "Vect_rewrite_line(): name = %s, line = %d", Map->name, line);
  129. if (!VECT_OPEN(Map))
  130. G_fatal_error(_("Unable to rewrite line, vector map is not opened"));
  131. dig_line_reset_updated(&(Map->plus));
  132. dig_node_reset_updated(&(Map->plus));
  133. if (!(Map->plus.update_cidx)) {
  134. Map->plus.cidx_up_to_date = 0;
  135. }
  136. ret =
  137. (*Vect_rewrite_line_array[Map->format][Map->level]) (Map, line, type,
  138. points, cats);
  139. if (ret == -1)
  140. G_fatal_error(_("Unable to rewrite line %d"), line);
  141. return ret;
  142. }
  143. /*
  144. * Deletes line at the given offset. Map must be opened on level 2.
  145. *
  146. * Returns: 0 ok
  147. * -1 on error
  148. */
  149. /*
  150. int
  151. V1_delete_line (
  152. struct Map_info *Map,
  153. long offset)
  154. {
  155. #ifdef GDEBUG
  156. G_debug (3, "V1_delete_line(): name = %s", Map->name);
  157. #endif
  158. return (*V1_delete_line_array[Map->format][Map->level]) (Map, offset);
  159. }
  160. */
  161. /*!
  162. \brief Deletes line of given number.
  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 line 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 line %d, "
  176. "vector map <%s> is not opened on topo level 2"),
  177. line, Map->name);
  178. }
  179. if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
  180. G_fatal_error(_("Unable to delete line %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 delete line %d from vector map <%s>"),
  192. line, Map->name);
  193. return ret;
  194. }