close.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. getenv("GRASS_VECTOR_PGFILE") == NULL) { /* GRASS_VECTOR_PGFILE defined by v.out.postgis */
  71. unlink_file(Map, GV_TOPO_ELEMENT); /* topo */
  72. unlink_file(Map, GV_SIDX_ELEMENT); /* sidx */
  73. unlink_file(Map, GV_CIDX_ELEMENT); /* cidx */
  74. if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_POSTGIS) {
  75. unlink_file(Map, GV_FIDX_ELEMENT); /* fidx */
  76. }
  77. Vect_coor_info(Map, &CInfo);
  78. Map->plus.coor_size = CInfo.size;
  79. Map->plus.coor_mtime = CInfo.mtime;
  80. /* write out topo file */
  81. Vect_save_topo(Map);
  82. /* write out sidx file */
  83. Map->plus.Spidx_new = TRUE; /* force writing */
  84. Vect_save_sidx(Map);
  85. /* write out cidx file */
  86. Vect_cidx_save(Map);
  87. /* write out fidx file */
  88. if (Map->format == GV_FORMAT_OGR)
  89. V2_close_ogr(Map);
  90. else if (Map->format == GV_FORMAT_POSTGIS)
  91. V2_close_pg(Map);
  92. }
  93. else {
  94. /* spatial index must also be closed when opened with topo but
  95. * not modified */
  96. /* NOTE: also close sidx for GV_FORMAT_OGR if not direct OGR access */
  97. if (Map->format != GV_FORMAT_OGR_DIRECT &&
  98. Map->plus.Spidx_built == TRUE &&
  99. Map->plus.built == GV_BUILD_ALL &&
  100. getenv("GRASS_VECTOR_PGFILE") == NULL) /* GRASS_VECTOR_PGFILE defined by v.out.postgis */
  101. fclose(Map->plus.spidx_fp.file);
  102. }
  103. if (Map->level > 1 && Map->plus.release_support) {
  104. G_debug(1, "free topology");
  105. dig_free_plus(&(Map->plus));
  106. G_debug(1, "free spatial index");
  107. dig_spidx_free(&(Map->plus));
  108. G_debug(1, "free category index");
  109. dig_cidx_free(&(Map->plus));
  110. }
  111. G_debug(1, "close history file");
  112. if (Map->hist_fp)
  113. fclose(Map->hist_fp);
  114. /* close level 1 files / data sources if not head_only */
  115. if (!Map->head_only) {
  116. if (((*Close_array[Map->format][1]) (Map)) != 0) {
  117. G_warning(_("Unable to close vector <%s>"),
  118. Vect_get_full_name(Map));
  119. return 1;
  120. }
  121. }
  122. G_free(Map->name);
  123. G_free(Map->mapset);
  124. G_free(Map->location);
  125. G_free(Map->gisdbase);
  126. Map->open = VECT_CLOSED_CODE;
  127. return 0;
  128. }
  129. /*!
  130. \brief Save format definition file for vector map
  131. \param Map pointer to Map_info structure
  132. \return 1 on success
  133. \return 0 on error
  134. */
  135. int Vect_save_frmt(struct Map_info *Map)
  136. {
  137. FILE *fd;
  138. char buf[GPATH_MAX];
  139. if (Map->format != GV_FORMAT_OGR &&
  140. Map->format != GV_FORMAT_POSTGIS) {
  141. G_warning(_("Invalid request for writing frmt file - map format is %d"), Map->format);
  142. return 0;
  143. }
  144. /* create frmt file */
  145. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  146. fd = G_fopen_new(buf, GV_FRMT_ELEMENT);
  147. if (fd == NULL) {
  148. G_fatal_error("Unable to create file '%s'", buf);
  149. }
  150. if (Map->format == GV_FORMAT_POSTGIS) {
  151. #ifdef HAVE_POSTGRES
  152. fprintf(fd, "FORMAT: postgis\n");
  153. fprintf(fd, "CONNINFO: %s\n", Map->fInfo.pg.conninfo);
  154. fprintf(fd, "SCHEMA: %s\n", Map->fInfo.pg.schema_name);
  155. fprintf(fd, "TABLE: %s\n", Map->fInfo.pg.table_name);
  156. #else
  157. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  158. return 0;
  159. #endif
  160. } else if (Map->format == GV_FORMAT_OGR) {
  161. #ifdef HAVE_OGR
  162. fprintf(fd, "FORMAT: ogr\n");
  163. fprintf(fd, "DSN: %s\n", Map->fInfo.ogr.dsn);
  164. fprintf(fd, "LAYER: %s\n", Map->fInfo.ogr.layer_name);
  165. #else
  166. G_fatal_error(_("GRASS is not compiled with OGR support"));
  167. return 0;
  168. #endif
  169. }
  170. G_verbose_message(_("Link to vector map <%s> created"), Map->name);
  171. /* close frmt file */
  172. fclose(fd);
  173. return 1;
  174. }
  175. void unlink_file(const struct Map_info *Map, const char *name)
  176. {
  177. char buf[GPATH_MAX];
  178. char file_path[GPATH_MAX];
  179. /* delete old support files if available */
  180. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  181. G_file_name(file_path, buf, name, G_mapset());
  182. if (access(file_path, F_OK) == 0) /* file exists? */
  183. unlink(file_path);
  184. }