close.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*!
  2. \file lib/vector/Vlib/close.c
  3. \brief Vector library - Close vector map
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009, 2011-2012 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 Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. \author Update to GRASS 7 Martin Landa <landa.martin gmail.com>
  11. */
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #include <grass/vector.h>
  19. #include <grass/glocale.h>
  20. static int clo_dummy()
  21. {
  22. return -1;
  23. }
  24. #if !defined HAVE_OGR || !defined HAVE_POSTGRES
  25. static int format()
  26. {
  27. G_fatal_error(_("Requested format is not compiled in this version"));
  28. return 0;
  29. }
  30. #endif
  31. static int (*Close_array[][2]) () = {
  32. {
  33. clo_dummy, V1_close_nat}
  34. #ifdef HAVE_OGR
  35. , {
  36. clo_dummy, V1_close_ogr}
  37. , {
  38. clo_dummy, V1_close_ogr}
  39. #else
  40. , {
  41. clo_dummy, format}
  42. , {
  43. clo_dummy, format}
  44. #endif
  45. #ifdef HAVE_POSTGRES
  46. , {
  47. clo_dummy, V1_close_pg}
  48. #else
  49. , {
  50. clo_dummy, format}
  51. #endif
  52. };
  53. static void unlink_file(const struct Map_info *, const char *);
  54. /*!
  55. \brief Close vector map
  56. \param Map pointer to Map_info
  57. \return 0 on success
  58. \return non-zero on error
  59. */
  60. int Vect_close(struct Map_info *Map)
  61. {
  62. struct Coor_info CInfo;
  63. G_debug(1, "Vect_close(): name = %s, mapset = %s, format = %d, level = %d",
  64. Map->name, Map->mapset, Map->format, Map->level);
  65. /* store support files for vector maps in the current mapset if in
  66. write mode on level 2 */
  67. if (strcmp(Map->mapset, G_mapset()) == 0 &&
  68. Map->support_updated &&
  69. Map->plus.built == GV_BUILD_ALL) {
  70. unlink_file(Map, GV_TOPO_ELEMENT); /* topo */
  71. unlink_file(Map, GV_SIDX_ELEMENT); /* sidx */
  72. unlink_file(Map, GV_CIDX_ELEMENT); /* cidx */
  73. if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_POSTGIS) {
  74. unlink_file(Map, GV_FIDX_ELEMENT); /* fidx */
  75. }
  76. Vect_coor_info(Map, &CInfo);
  77. Map->plus.coor_size = CInfo.size;
  78. Map->plus.coor_mtime = CInfo.mtime;
  79. /* write out topo file */
  80. Vect_save_topo(Map);
  81. /* write out sidx file */
  82. Map->plus.Spidx_new = TRUE; /* force writing */
  83. Vect_save_sidx(Map);
  84. /* write out cidx file */
  85. Vect_cidx_save(Map);
  86. /* write out fidx file */
  87. if (Map->format == GV_FORMAT_OGR)
  88. V2_close_ogr(Map);
  89. else if (Map->format == GV_FORMAT_POSTGIS)
  90. V2_close_pg(Map);
  91. }
  92. else {
  93. /* spatial index must also be closed when opened with topo but
  94. * not modified */
  95. /* NOTE: also close sidx for GV_FORMAT_OGR if not direct OGR access */
  96. if (Map->format != GV_FORMAT_OGR_DIRECT &&
  97. Map->plus.Spidx_built == TRUE &&
  98. Map->plus.built == GV_BUILD_ALL)
  99. fclose(Map->plus.spidx_fp.file);
  100. }
  101. if (Map->level > 1 && Map->plus.release_support) {
  102. G_debug(1, "free topology");
  103. dig_free_plus(&(Map->plus));
  104. G_debug(1, "free spatial index");
  105. dig_spidx_free(&(Map->plus));
  106. G_debug(1, "free category index");
  107. dig_cidx_free(&(Map->plus));
  108. }
  109. G_debug(1, "close history file");
  110. if (Map->hist_fp)
  111. fclose(Map->hist_fp);
  112. /* close level 1 files / data sources if not head_only */
  113. if (!Map->head_only) {
  114. if (((*Close_array[Map->format][1]) (Map)) != 0) {
  115. G_warning(_("Unable to close vector <%s>"),
  116. Vect_get_full_name(Map));
  117. return 1;
  118. }
  119. }
  120. G_free(Map->name);
  121. G_free(Map->mapset);
  122. G_free(Map->location);
  123. G_free(Map->gisdbase);
  124. Map->open = VECT_CLOSED_CODE;
  125. return 0;
  126. }
  127. /*!
  128. \brief Save format definition file for vector map
  129. \param Map pointer to Map_info structure
  130. \return 1 on success
  131. \return 0 on error
  132. */
  133. int Vect_save_frmt(struct Map_info *Map)
  134. {
  135. FILE *fd;
  136. char buf[GPATH_MAX];
  137. if (Map->format != GV_FORMAT_OGR &&
  138. Map->format != GV_FORMAT_POSTGIS) {
  139. G_warning(_("Invalid request for writing frmt file - map format is %d"), Map->format);
  140. return 0;
  141. }
  142. /* create frmt file */
  143. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  144. fd = G_fopen_new(buf, GV_FRMT_ELEMENT);
  145. if (fd == NULL) {
  146. G_fatal_error("Unable to create file '%s'", buf);
  147. }
  148. if (Map->format == GV_FORMAT_POSTGIS) {
  149. #ifdef HAVE_POSTGRES
  150. fprintf(fd, "FORMAT: postgis\n");
  151. fprintf(fd, "CONNINFO: %s\n", Map->fInfo.pg.conninfo);
  152. fprintf(fd, "SCHEMA: %s\n", Map->fInfo.pg.schema_name);
  153. fprintf(fd, "TABLE: %s\n", Map->fInfo.pg.table_name);
  154. #else
  155. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  156. return 0;
  157. #endif
  158. } else if (Map->format == GV_FORMAT_OGR) {
  159. #ifdef HAVE_OGR
  160. fprintf(fd, "FORMAT: ogr\n");
  161. fprintf(fd, "DSN: %s\n", Map->fInfo.ogr.dsn);
  162. fprintf(fd, "LAYER: %s\n", Map->fInfo.ogr.layer_name);
  163. #else
  164. G_fatal_error(_("GRASS is not compiled with OGR support"));
  165. return 0;
  166. #endif
  167. }
  168. G_verbose_message(_("Link to vector map <%s> created"), Map->name);
  169. /* close frmt file */
  170. fclose(fd);
  171. return 1;
  172. }
  173. void unlink_file(const struct Map_info *Map, const char *name)
  174. {
  175. char buf[GPATH_MAX];
  176. char file_path[GPATH_MAX];
  177. /* delete old support files if available */
  178. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  179. G_file_name(file_path, buf, name, G_mapset());
  180. if (access(file_path, F_OK) == 0) /* file exists? */
  181. unlink(file_path);
  182. }