write.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. Supported operations:
  6. - Write a new feature
  7. - Rewrite existing feature
  8. - Delete existing feature
  9. - Restore deleted feature
  10. (C) 2001-2010, 2012-2013 by the GRASS Development Team
  11. This program is free software under the GNU General Public License
  12. (>=v2). Read the file COPYING that comes with GRASS for details.
  13. \author Radim Blazek
  14. \author Updated by Martin Landa <landa.martin gmail.com> (restore lines, OGR & PostGIS support)
  15. */
  16. #include <sys/types.h>
  17. #include <grass/glocale.h>
  18. #include <grass/vector.h>
  19. static off_t write_dummy()
  20. {
  21. G_warning("Vect_write_line() %s",
  22. _("for this format/level not supported"));
  23. return -1;
  24. }
  25. static off_t rewrite_dummy()
  26. {
  27. G_warning("Vect_rewrite_line() %s",
  28. _("for this format/level not supported"));
  29. return -1;
  30. }
  31. static int delete_dummy()
  32. {
  33. G_warning("Vect_delete_line() %s",
  34. _("for this format/level not supported"));
  35. return -1;
  36. }
  37. static int restore_dummy()
  38. {
  39. G_warning("Vect_restore_line() %s",
  40. _("for this format/level not supported"));
  41. return -1;
  42. }
  43. #if !defined HAVE_OGR || !defined HAVE_POSTGRES
  44. static int format()
  45. {
  46. G_fatal_error(_("Requested format is not compiled in this version"));
  47. return 0;
  48. }
  49. static off_t format_l()
  50. {
  51. G_fatal_error(_("Requested format is not compiled in this version"));
  52. return 0;
  53. }
  54. #endif
  55. static off_t (*Vect_write_line_array[][3]) () = {
  56. {
  57. write_dummy, V1_write_line_nat, V2_write_line_nat}
  58. #ifdef HAVE_OGR
  59. , {
  60. write_dummy, V1_write_line_ogr, V2_write_line_sfa}
  61. , {
  62. write_dummy, V1_write_line_ogr, V2_write_line_sfa}
  63. #else
  64. , {
  65. write_dummy, format_l, format_l}
  66. , {
  67. write_dummy, format_l, format_l}
  68. #endif
  69. #ifdef HAVE_POSTGRES
  70. , {
  71. write_dummy, V1_write_line_pg, V2_write_line_pg}
  72. #else
  73. , {
  74. write_dummy, format_l, format_l}
  75. #endif
  76. };
  77. static off_t (*Vect_rewrite_line_array[][3]) () = {
  78. {
  79. rewrite_dummy, V1_rewrite_line_nat, V2_rewrite_line_nat}
  80. #ifdef HAVE_OGR
  81. , {
  82. rewrite_dummy, V1_rewrite_line_ogr, V2_rewrite_line_sfa}
  83. , {
  84. rewrite_dummy, V1_rewrite_line_ogr, V2_rewrite_line_sfa}
  85. #else
  86. , {
  87. rewrite_dummy, format_l, format_l}
  88. , {
  89. rewrite_dummy, format_l, format_l}
  90. #endif
  91. #ifdef HAVE_POSTGRES
  92. , {
  93. rewrite_dummy, V1_rewrite_line_pg, V2_rewrite_line_pg}
  94. #else
  95. , {
  96. rewrite_dummy, format_l, format_l}
  97. #endif
  98. };
  99. static int (*Vect_delete_line_array[][3]) () = {
  100. {
  101. delete_dummy, V1_delete_line_nat, V2_delete_line_nat}
  102. #ifdef HAVE_OGR
  103. , {
  104. delete_dummy, V1_delete_line_ogr, V2_delete_line_sfa}
  105. , {
  106. delete_dummy, V1_delete_line_ogr, V2_delete_line_sfa}
  107. #else
  108. , {
  109. delete_dummy, format, format}
  110. , {
  111. delete_dummy, format, format}
  112. #endif
  113. #ifdef HAVE_POSTGRES
  114. , {
  115. delete_dummy, V1_delete_line_pg, V2_delete_line_pg}
  116. #else
  117. , {
  118. delete_dummy, format, format}
  119. #endif
  120. };
  121. static int (*Vect_restore_line_array[][3]) () = {
  122. {
  123. restore_dummy, V1_restore_line_nat, V2_restore_line_nat}
  124. #ifdef HAVE_OGR
  125. , {
  126. restore_dummy, restore_dummy, restore_dummy}
  127. , {
  128. restore_dummy, restore_dummy, restore_dummy}
  129. #else
  130. , {
  131. restore_dummy, format, format}
  132. , {
  133. restore_dummy, format, format}
  134. #endif
  135. #ifdef HAVE_POSTGRES
  136. , {
  137. restore_dummy, restore_dummy, restore_dummy}
  138. #else
  139. , {
  140. restore_dummy, format, format}
  141. #endif
  142. };
  143. static int check_map(const struct Map_info *);
  144. /*!
  145. \brief Writes a new feature
  146. New feature is written to the end of file (in the case of native
  147. format). Topological level is not required.
  148. A warning is printed on error.
  149. \param Map pointer to Map_info structure
  150. \param type feature type (see dig_defines.h for supported types)
  151. \param points pointer to line_pnts structure (feature geometry)
  152. \param cats pointer to line_cats structure (feature categories)
  153. \return new feature id (on level 2) (or 0 when build level < GV_BUILD_BASE)
  154. \return offset into file where the feature starts (on level 1)
  155. \return -1 on error
  156. */
  157. off_t Vect_write_line(struct Map_info *Map, int type,
  158. const struct line_pnts *points, const struct line_cats *cats)
  159. {
  160. off_t offset;
  161. G_debug(3, "Vect_write_line(): name = %s, format = %d, level = %d",
  162. Map->name, Map->format, Map->level);
  163. if (!check_map(Map))
  164. return -1;
  165. offset =
  166. (*Vect_write_line_array[Map->format][Map->level]) (Map, type,
  167. points, cats);
  168. if (offset < 0)
  169. G_warning(_("Unable to write feature in vector map <%s>"),
  170. Vect_get_name(Map));
  171. return offset;
  172. }
  173. /*!
  174. \brief Rewrites existing feature (topological level required)
  175. Note: Topology must be built at level >= GV_BUILD_BASE
  176. A warning is printed on error.
  177. The number of points or cats or type may change. If necessary, the
  178. old feature is deleted and new is written.
  179. \param Map pointer to Map_info structure
  180. \param line feature id (level 2) or feature offset (level 1)
  181. \param type feature type (GV_POINT, GV_LINE, ...)
  182. \param points feature geometry
  183. \param cats feature categories
  184. \return new feature id (on level 2) (or 0 when build level < GV_BUILD_BASE)
  185. \return offset into file where the feature starts (on level 1)
  186. \return -1 on error
  187. */
  188. off_t Vect_rewrite_line(struct Map_info *Map, off_t line, int type,
  189. const struct line_pnts *points, const struct line_cats *cats)
  190. {
  191. off_t ret;
  192. G_debug(3, "Vect_rewrite_line(): name = %s, format = %d, level = %d, line/offset = %" PRI_OFF_T,
  193. Map->name, Map->format, Map->level, line);
  194. if (!check_map(Map))
  195. return -1;
  196. ret = (*Vect_rewrite_line_array[Map->format][Map->level]) (Map, line, type,
  197. points, cats);
  198. if (ret == -1)
  199. G_warning(_("Unable to rewrite feature/offset %" PRI_OFF_T " in vector map <%s>"),
  200. line, Vect_get_name(Map));
  201. return ret;
  202. }
  203. /*!
  204. \brief Delete existing feature (topological level required)
  205. Note: Topology must be built at level >= GV_BUILD_BASE
  206. A warning is printed on error.
  207. \param Map pointer to Map_info structure
  208. \param line feature id (level 2) or feature offset (level 1)
  209. \return 0 on success
  210. \return -1 on error
  211. */
  212. int Vect_delete_line(struct Map_info *Map, off_t line)
  213. {
  214. int ret;
  215. G_debug(3, "Vect_delete_line(): name = %s, line/offset = %" PRI_OFF_T,
  216. Map->name, line);
  217. if (!check_map(Map))
  218. return -1;
  219. ret = (*Vect_delete_line_array[Map->format][Map->level]) (Map, line);
  220. if (ret == -1)
  221. G_warning(_("Unable to delete feature/offset %" PRI_OFF_T " from vector map <%s>"),
  222. line, Vect_get_name(Map));
  223. return ret;
  224. }
  225. /*!
  226. \brief Restore previously deleted feature (topological level required)
  227. Note: Topology must be built at level >= GV_BUILD_BASE
  228. A warning is printed on error.
  229. \param Map pointer to Map_info structure
  230. \param offset feature offset to be restored
  231. \param line feature id to be restored (used only on level 2)
  232. \return 0 on success
  233. \return -1 on error
  234. */
  235. int Vect_restore_line(struct Map_info *Map, off_t offset, off_t line)
  236. {
  237. int ret;
  238. G_debug(3, "Vect_restore_line(): name = %s, level = %d, offset = %" PRI_OFF_T ", line = %" PRI_OFF_T,
  239. Map->name, Map->level, offset, line);
  240. if (!check_map(Map))
  241. return -1;
  242. ret = (*Vect_restore_line_array[Map->format][Map->level]) (Map, offset, line);
  243. if (ret == -1)
  244. G_warning(_("Unable to restore feature/offset %" PRI_OFF_T " in vector map <%s>"),
  245. offset, Vect_get_name(Map));
  246. return ret;
  247. }
  248. int check_map(const struct Map_info *Map)
  249. {
  250. if (!VECT_OPEN(Map)) {
  251. G_warning(_("Vector map <%s> is not opened"), Vect_get_name(Map));
  252. return 0;
  253. }
  254. if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
  255. G_warning(_("Vector map <%s> is not opened in write mode"),
  256. Vect_get_name(Map));
  257. return 0;
  258. }
  259. return 1;
  260. }