close.c 7.1 KB

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