close.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. int create_link; /* used for external formats only */
  63. struct Coor_info CInfo;
  64. G_debug(1, "Vect_close(): name = %s, mapset = %s, format = %d, level = %d",
  65. Map->name, Map->mapset, Map->format, Map->level);
  66. /* check for external formats whether to create a link */
  67. create_link = TRUE;
  68. if (Map->format == GV_FORMAT_OGR ||
  69. Map->format == GV_FORMAT_POSTGIS) {
  70. char *def_file;
  71. if (Map->format == GV_FORMAT_POSTGIS) {
  72. if (getenv("GRASS_VECTOR_PGFILE"))
  73. def_file = getenv("GRASS_VECTOR_PGFILE");
  74. else
  75. def_file = "PG";
  76. }
  77. else {
  78. def_file = "OGR";
  79. }
  80. if (G_find_file2("", def_file, G_mapset())) {
  81. FILE *fp;
  82. const char *p;
  83. struct Key_Value *key_val;
  84. fp = G_fopen_old("", def_file, G_mapset());
  85. if (!fp) {
  86. G_warning(_("Unable to open %s file"), def_file);
  87. }
  88. else {
  89. key_val = G_fread_key_value(fp);
  90. fclose(fp);
  91. /* create a vector link in the current mapset ? */
  92. p = G_find_key_value("link", key_val);
  93. if (p && G_strcasecmp(p, "no") == 0) {
  94. create_link = FALSE;
  95. }
  96. else {
  97. p = G_find_key_value("link_name", key_val);
  98. if (p) {
  99. /* use different name for a link */
  100. G_free(Map->name);
  101. Map->name = G_store(p);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. /* store support files for vector maps in the current mapset if in
  108. write mode on level 2 */
  109. if (strcmp(Map->mapset, G_mapset()) == 0 &&
  110. Map->support_updated &&
  111. Map->plus.built == GV_BUILD_ALL &&
  112. create_link) {
  113. unlink_file(Map, GV_TOPO_ELEMENT); /* topo */
  114. unlink_file(Map, GV_SIDX_ELEMENT); /* sidx */
  115. unlink_file(Map, GV_CIDX_ELEMENT); /* cidx */
  116. if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_POSTGIS) {
  117. unlink_file(Map, GV_FIDX_ELEMENT); /* fidx */
  118. }
  119. Vect_coor_info(Map, &CInfo);
  120. Map->plus.coor_size = CInfo.size;
  121. Map->plus.coor_mtime = CInfo.mtime;
  122. /* write out topo file */
  123. Vect_save_topo(Map);
  124. /* write out sidx file */
  125. Map->plus.Spidx_new = TRUE; /* force writing */
  126. Vect_save_sidx(Map);
  127. /* write out cidx file */
  128. Vect_cidx_save(Map);
  129. /* write out fidx file */
  130. if (Map->format == GV_FORMAT_OGR)
  131. V2_close_ogr(Map);
  132. else if (Map->format == GV_FORMAT_POSTGIS)
  133. V2_close_pg(Map);
  134. }
  135. /* spatial index must also be closed when opened with topo but
  136. * not modified */
  137. /* NOTE: also close sidx for GV_FORMAT_OGR if not direct OGR access */
  138. if (Map->format != GV_FORMAT_OGR_DIRECT &&
  139. Map->plus.Spidx_built == TRUE &&
  140. !Map->support_updated &&
  141. Map->plus.built == GV_BUILD_ALL &&
  142. create_link) {
  143. fclose(Map->plus.spidx_fp.file);
  144. }
  145. if (Map->level > 1 && Map->plus.release_support) {
  146. G_debug(1, "free topology, spatial index, and category index");
  147. dig_free_plus(&(Map->plus));
  148. }
  149. G_debug(1, "close history file");
  150. if (Map->hist_fp)
  151. fclose(Map->hist_fp);
  152. /* close level 1 files / data sources if not head_only */
  153. if (!Map->head_only) {
  154. if (create_link && ((*Close_array[Map->format][1]) (Map)) != 0) {
  155. G_warning(_("Unable to close vector <%s>"),
  156. Vect_get_full_name(Map));
  157. return 1;
  158. }
  159. }
  160. G_free(Map->name);
  161. G_free(Map->mapset);
  162. G_free(Map->location);
  163. G_free(Map->gisdbase);
  164. Map->open = VECT_CLOSED_CODE;
  165. return 0;
  166. }
  167. /*!
  168. \brief Save format definition file for vector map
  169. \param Map pointer to Map_info structure
  170. \return 1 on success
  171. \return 0 on error
  172. */
  173. int Vect_save_frmt(struct Map_info *Map)
  174. {
  175. FILE *fd;
  176. char buf[GPATH_MAX];
  177. if (Map->format != GV_FORMAT_OGR &&
  178. Map->format != GV_FORMAT_POSTGIS) {
  179. G_warning(_("Invalid request for writing frmt file - map format is %d"), Map->format);
  180. return 0;
  181. }
  182. /* create frmt file */
  183. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  184. fd = G_fopen_new(buf, GV_FRMT_ELEMENT);
  185. if (fd == NULL) {
  186. G_fatal_error("Unable to create file '%s'", buf);
  187. }
  188. if (Map->format == GV_FORMAT_POSTGIS) {
  189. #ifdef HAVE_POSTGRES
  190. fprintf(fd, "format: postgis\n");
  191. fprintf(fd, "conninfo: %s\n", Map->fInfo.pg.conninfo);
  192. fprintf(fd, "schema: %s\n", Map->fInfo.pg.schema_name);
  193. fprintf(fd, "table: %s\n", Map->fInfo.pg.table_name);
  194. #else
  195. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  196. return 0;
  197. #endif
  198. } else if (Map->format == GV_FORMAT_OGR) {
  199. #ifdef HAVE_OGR
  200. fprintf(fd, "format: ogr\n");
  201. fprintf(fd, "dsn: %s\n", Map->fInfo.ogr.dsn);
  202. fprintf(fd, "layer: %s\n", Map->fInfo.ogr.layer_name);
  203. #else
  204. G_fatal_error(_("GRASS is not compiled with OGR support"));
  205. return 0;
  206. #endif
  207. }
  208. G_verbose_message(_("Link to vector map <%s> created"), Map->name);
  209. /* close frmt file */
  210. fclose(fd);
  211. return 1;
  212. }
  213. void unlink_file(const struct Map_info *Map, const char *name)
  214. {
  215. char buf[GPATH_MAX];
  216. char file_path[GPATH_MAX];
  217. /* delete old support files if available */
  218. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  219. G_file_name(file_path, buf, name, G_mapset());
  220. if (access(file_path, F_OK) == 0) /* file exists? */
  221. unlink(file_path);
  222. }