close.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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-2015 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. if (Map->temporary &&
  68. (Map->fInfo.ogr.dsn || Map->fInfo.pg.conninfo)) {
  69. /* transfer features for external output format */
  70. struct Map_info Out;
  71. putenv("GRASS_VECTOR_EXTERNAL_IMMEDIATE=1");
  72. if (-1 == Vect_open_new(&Out, Vect_get_name(Map), Vect_is_3d(Map))) {
  73. G_warning(_("Unable to create vector map <%s>"),
  74. Vect_get_name(Map));
  75. return 1;
  76. }
  77. /* copy metadata */
  78. Vect_hist_copy(Map, &Out);
  79. Vect_copy_head_data(Map, &Out);
  80. /* copy dblinks (temporary map -> output map) to transfer
  81. (input map -> output map) attributes */
  82. Vect_copy_map_dblinks(Map, &Out, TRUE);
  83. /* afterwords, dblinks must be removed from temporary map
  84. otherwise when deleting temporary map also original
  85. attribute tables would be deteled */
  86. Vect_map_del_dblink(Map, -1); /* delete db links for all layers */
  87. if (0 != Vect_copy_map_lines_field(Map, 1, &Out)) { /* always layer = 1 for OGR/PG maps */
  88. G_warning(_("Copying features failed"));
  89. return -1;
  90. }
  91. Vect_build(&Out);
  92. Vect_close(&Out);
  93. putenv("GRASS_VECTOR_EXTERNAL_IMMEDIATE="); /* unset variable */
  94. }
  95. /* check for external formats whether to create a link */
  96. create_link = TRUE;
  97. if (Map->format == GV_FORMAT_OGR ||
  98. Map->format == GV_FORMAT_POSTGIS) {
  99. char *def_file;
  100. if (Map->format == GV_FORMAT_POSTGIS) {
  101. if (getenv("GRASS_VECTOR_PGFILE"))
  102. def_file = getenv("GRASS_VECTOR_PGFILE");
  103. else
  104. def_file = "PG";
  105. }
  106. else {
  107. def_file = "OGR";
  108. }
  109. if (G_find_file2("", def_file, G_mapset())) {
  110. FILE *fp;
  111. const char *p;
  112. struct Key_Value *key_val;
  113. fp = G_fopen_old("", def_file, G_mapset());
  114. if (!fp) {
  115. G_warning(_("Unable to open %s file"), def_file);
  116. }
  117. else {
  118. key_val = G_fread_key_value(fp);
  119. fclose(fp);
  120. /* create a vector link in the current mapset ? */
  121. p = G_find_key_value("link", key_val);
  122. if (p && G_strcasecmp(p, "no") == 0) {
  123. create_link = FALSE;
  124. }
  125. else {
  126. p = G_find_key_value("link_name", key_val);
  127. if (p) {
  128. /* use different name for a link */
  129. G_free(Map->name);
  130. Map->name = G_store(p);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. /* store support files for vector maps in the current mapset if in
  137. write mode on level 2 */
  138. if (strcmp(Map->mapset, G_mapset()) == 0 &&
  139. Map->support_updated &&
  140. Map->plus.built == GV_BUILD_ALL &&
  141. create_link) {
  142. unlink_file(Map, GV_TOPO_ELEMENT); /* topo */
  143. unlink_file(Map, GV_SIDX_ELEMENT); /* sidx */
  144. unlink_file(Map, GV_CIDX_ELEMENT); /* cidx */
  145. if (Map->format == GV_FORMAT_OGR || Map->format == GV_FORMAT_POSTGIS) {
  146. unlink_file(Map, GV_FIDX_ELEMENT); /* fidx */
  147. }
  148. Vect_coor_info(Map, &CInfo);
  149. Map->plus.coor_size = CInfo.size;
  150. Map->plus.coor_mtime = CInfo.mtime;
  151. /* write out topo file */
  152. Vect_save_topo(Map);
  153. /* write out sidx file */
  154. Map->plus.Spidx_new = TRUE; /* force writing */
  155. Vect_save_sidx(Map);
  156. /* write out cidx file */
  157. Vect_cidx_save(Map);
  158. /* write out fidx file */
  159. if (Map->format == GV_FORMAT_OGR)
  160. V2_close_ogr(Map);
  161. else if (Map->format == GV_FORMAT_POSTGIS)
  162. V2_close_pg(Map);
  163. }
  164. /* spatial index must also be closed when opened with topo but not
  165. * modified */
  166. if (Map->plus.spidx_fp.file &&
  167. Map->plus.Spidx_built == TRUE &&
  168. !Map->support_updated &&
  169. Map->plus.built == GV_BUILD_ALL) {
  170. G_debug(1, "spatial index file closed");
  171. fclose(Map->plus.spidx_fp.file);
  172. }
  173. /* release memory if required */
  174. if (Map->level > 1 && Map->plus.release_support) {
  175. G_debug(1, "free topology, spatial index, and category index");
  176. dig_free_plus(&(Map->plus));
  177. }
  178. G_debug(1, "close history file");
  179. if (Map->hist_fp)
  180. fclose(Map->hist_fp);
  181. /* close level 1 files / data sources if not head_only */
  182. if (!Map->head_only) {
  183. if (create_link && ((*Close_array[Map->format][1]) (Map)) != 0) {
  184. G_warning(_("Unable to close vector <%s>"),
  185. Vect_get_full_name(Map));
  186. return 1;
  187. }
  188. }
  189. G_free(Map->name);
  190. G_free(Map->mapset);
  191. G_free(Map->location);
  192. G_free(Map->gisdbase);
  193. Map->open = VECT_CLOSED_CODE;
  194. return 0;
  195. }
  196. /*!
  197. \brief Save format definition file for vector map
  198. \param Map pointer to Map_info structure
  199. \return 1 on success
  200. \return 0 on error
  201. */
  202. int Vect_save_frmt(struct Map_info *Map)
  203. {
  204. FILE *fd;
  205. char buf[GPATH_MAX];
  206. if (Map->format != GV_FORMAT_OGR &&
  207. Map->format != GV_FORMAT_POSTGIS) {
  208. G_warning(_("Invalid request for writing frmt file - map format is %d"), Map->format);
  209. return 0;
  210. }
  211. /* create frmt file */
  212. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  213. fd = G_fopen_new(buf, GV_FRMT_ELEMENT);
  214. if (fd == NULL) {
  215. G_fatal_error("Unable to create file '%s'", buf);
  216. }
  217. if (Map->format == GV_FORMAT_POSTGIS) {
  218. #ifdef HAVE_POSTGRES
  219. fprintf(fd, "format: postgis\n");
  220. fprintf(fd, "conninfo: %s\n", Map->fInfo.pg.conninfo);
  221. fprintf(fd, "schema: %s\n", Map->fInfo.pg.schema_name);
  222. fprintf(fd, "table: %s\n", Map->fInfo.pg.table_name);
  223. #else
  224. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  225. return 0;
  226. #endif
  227. } else if (Map->format == GV_FORMAT_OGR) {
  228. #ifdef HAVE_OGR
  229. fprintf(fd, "format: ogr\n");
  230. fprintf(fd, "dsn: %s\n", Map->fInfo.ogr.dsn);
  231. fprintf(fd, "layer: %s\n", Map->fInfo.ogr.layer_name);
  232. #else
  233. G_fatal_error(_("GRASS is not compiled with OGR support"));
  234. return 0;
  235. #endif
  236. }
  237. G_verbose_message(_("Link to vector map <%s> created"), Map->name);
  238. /* close frmt file */
  239. fclose(fd);
  240. return 1;
  241. }
  242. /*! Free memory of line cache
  243. \param cache pointer to lines cache to be freed
  244. */
  245. void Vect__free_cache(struct Format_info_cache *cache) {
  246. int i;
  247. /* destroy lines in cache */
  248. for (i = 0; i < cache->lines_alloc; i++) {
  249. Vect_destroy_line_struct(cache->lines[i]);
  250. }
  251. G_free(cache->lines);
  252. G_free(cache->lines_types);
  253. G_free(cache->lines_cats);
  254. G_zero(cache, sizeof(struct Format_info_cache));
  255. }
  256. /*! Free memory of offset array
  257. \param cache pointer to offset array to be freed
  258. */
  259. void Vect__free_offset(struct Format_info_offset *offset)
  260. {
  261. G_free(offset->array);
  262. G_zero(offset, sizeof(struct Format_info_offset));
  263. }
  264. void unlink_file(const struct Map_info *Map, const char *name)
  265. {
  266. char path[GPATH_MAX];
  267. /* delete old support files if available */
  268. Vect__get_element_path(path, Map, name);
  269. if (access(path, F_OK) == 0) { /* file exists? */
  270. G_debug(2, "\t%s: unlink", path);
  271. unlink(path);
  272. }
  273. }