write.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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, restore_dummy, 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 (!VECT_OPEN(Map)) {
  164. G_warning(_("Vector map <%s> is not opened"), Vect_get_name(Map));
  165. return -1;
  166. }
  167. if (!(Map->plus.update_cidx)) {
  168. Map->plus.cidx_up_to_date = FALSE; /* category index will be outdated */
  169. }
  170. offset =
  171. (*Vect_write_line_array[Map->format][Map->level]) (Map, type, points,
  172. cats);
  173. if (offset < 0)
  174. G_warning(_("Unable to write feature in vector map <%s>"), Vect_get_name(Map));
  175. return offset;
  176. }
  177. /*!
  178. \brief Rewrites existing feature (topological level required)
  179. Note: Topology must be built at level >= GV_BUILD_BASE
  180. A warning is printed on error.
  181. The number of points or cats or type may change. If necessary, the
  182. old feature is deleted and new is written.
  183. \param Map pointer to Map_info structure
  184. \param line feature id
  185. \param type feature type (GV_POINT, GV_LINE, ...)
  186. \param points feature geometry
  187. \param cats feature categories
  188. \return new feature offset
  189. \return -1 on error
  190. */
  191. off_t Vect_rewrite_line(struct Map_info *Map, int line, int type,
  192. const struct line_pnts *points, const struct line_cats *cats)
  193. {
  194. off_t ret, offset;
  195. if (!check_map(Map))
  196. return -1;
  197. if (line < 1 || line > Map->plus.n_lines) {
  198. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  199. return -1;
  200. }
  201. if (!(Map->plus.update_cidx)) {
  202. Map->plus.cidx_up_to_date = FALSE; /* category index will be outdated */
  203. }
  204. offset = Map->plus.Line[line]->offset;
  205. G_debug(3, "Vect_rewrite_line(): name = %s, line = %d, offset = %lu",
  206. Map->name, line, offset);
  207. ret = (*Vect_rewrite_line_array[Map->format][Map->level]) (Map, line, type,
  208. offset,
  209. points, cats);
  210. if (ret == -1)
  211. G_warning(_("Unable to rewrite feature %d in vector map <%s>"), line, Vect_get_name(Map));
  212. return ret;
  213. }
  214. /*!
  215. \brief Delete existing feature (topological level required)
  216. Note: Topology must be built at level >= GV_BUILD_BASE
  217. A warning is printed on error.
  218. \param Map pointer to Map_info structure
  219. \param line feature id
  220. \return 0 on success
  221. \return -1 on error
  222. */
  223. int Vect_delete_line(struct Map_info *Map, int line)
  224. {
  225. int ret;
  226. G_debug(3, "Vect_delete_line(): name = %s, line = %d", Map->name, line);
  227. if (!check_map(Map))
  228. return -1;
  229. if (line < 1 || line > Map->plus.n_lines) {
  230. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  231. return -1;
  232. }
  233. if (!(Map->plus.update_cidx)) {
  234. Map->plus.cidx_up_to_date = FALSE; /* category index will be outdated */
  235. }
  236. ret = (*Vect_delete_line_array[Map->format][Map->level]) (Map, line);
  237. if (ret == -1)
  238. G_warning(_("Unable to delete feature %d from vector map <%s>"),
  239. line, Vect_get_name(Map));
  240. return ret;
  241. }
  242. /*!
  243. \brief Restore previously deleted feature (topological level required)
  244. Note: Topology must be built at level >= GV_BUILD_BASE
  245. A warning is printed on error.
  246. \param Map pointer to Map_info structure
  247. \param line feature id to be restored
  248. \param offset feature offset
  249. \return 0 on success
  250. \return -1 on error
  251. */
  252. int Vect_restore_line(struct Map_info *Map, int line, off_t offset)
  253. {
  254. int ret;
  255. G_debug(3, "Vect_restore_line(): name = %s, line = %d", Map->name, line);
  256. if (!check_map(Map))
  257. return -1;
  258. if (line < 1 || line > Map->plus.n_lines) {
  259. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  260. return -1;
  261. }
  262. if (!(Map->plus.update_cidx)) {
  263. Map->plus.cidx_up_to_date = 0;
  264. }
  265. ret = (*Vect_restore_line_array[Map->format][Map->level]) (Map, line, offset);
  266. if (ret == -1)
  267. G_warning(_("Unable to restore feature %d in vector map <%s>"),
  268. line, Vect_get_name(Map));
  269. return ret;
  270. }
  271. int check_map(const struct Map_info *Map)
  272. {
  273. if (!VECT_OPEN(Map)) {
  274. G_warning(_("Vector map <%s> is not opened"), Vect_get_name(Map));
  275. return 0;
  276. }
  277. if (Map->level < 2) {
  278. G_warning(_("Vector map <%s> is not opened on topology level"),
  279. Vect_get_name(Map));
  280. return 0;
  281. }
  282. if (Map->mode != GV_MODE_RW && Map->mode != GV_MODE_WRITE) {
  283. G_warning(_("Vector map <%s> is not opened in write mode"),
  284. Vect_get_name(Map));
  285. return 0;
  286. }
  287. return 1;
  288. }