close.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 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. /*!
  54. \brief Close vector map
  55. \param Map pointer to Map_info
  56. \return 0 on success
  57. \return non-zero on error
  58. */
  59. int Vect_close(struct Map_info *Map)
  60. {
  61. struct Coor_info CInfo;
  62. G_debug(1, "Vect_close(): name = %s, mapset = %s, format = %d, level = %d",
  63. Map->name, Map->mapset, Map->format, Map->level);
  64. /* store support files for vector maps in the current mapset if in
  65. write mode on level 2 */
  66. if (strcmp(Map->mapset, G_mapset()) == 0 &&
  67. Map->support_updated &&
  68. Map->plus.built == GV_BUILD_ALL) {
  69. char buf[GPATH_MAX];
  70. char file_path[GPATH_MAX];
  71. /* delete old support files if available */
  72. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  73. G_file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
  74. if (access(file_path, F_OK) == 0) /* file exists? */
  75. unlink(file_path);
  76. G_file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
  77. if (access(file_path, F_OK) == 0) /* file exists? */
  78. unlink(file_path);
  79. G_file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
  80. if (access(file_path, F_OK) == 0) /* file exists? */
  81. unlink(file_path);
  82. if (Map->format == GV_FORMAT_OGR ||
  83. Map->format == GV_FORMAT_POSTGIS) {
  84. G_file_name(file_path, buf, GV_FIDX_ELEMENT, G_mapset());
  85. if (access(file_path, F_OK) == 0) /* file exists? */
  86. unlink(file_path);
  87. }
  88. Vect_coor_info(Map, &CInfo);
  89. Map->plus.coor_size = CInfo.size;
  90. Map->plus.coor_mtime = CInfo.mtime;
  91. /* write out topo file */
  92. Vect_save_topo(Map);
  93. /* write out sidx file */
  94. Map->plus.Spidx_new = TRUE;
  95. Vect_save_sidx(Map);
  96. /* write out cidx file */
  97. Vect_cidx_save(Map);
  98. /* write out fidx file */
  99. if (Map->format == GV_FORMAT_OGR)
  100. V2_close_ogr(Map);
  101. else if (Map->format == GV_FORMAT_POSTGIS && Map->level == 2)
  102. V2_close_pg(Map);
  103. }
  104. else {
  105. /* spatial index must also be closed when opened with topo but
  106. * not modified */
  107. /* NOTE: also close sidx for GV_FORMAT_OGR if not direct OGR access */
  108. if (Map->format != GV_FORMAT_OGR_DIRECT &&
  109. Map->plus.Spidx_built == TRUE &&
  110. Map->plus.built == GV_BUILD_ALL)
  111. fclose(Map->plus.spidx_fp.file);
  112. }
  113. if (Map->level > 1 && Map->plus.release_support) {
  114. G_debug(1, "free topology");
  115. dig_free_plus(&(Map->plus));
  116. G_debug(1, "free spatial index");
  117. dig_spidx_free(&(Map->plus));
  118. G_debug(1, "free category index");
  119. dig_cidx_free(&(Map->plus));
  120. }
  121. if (Map->format == GV_FORMAT_NATIVE) {
  122. G_debug(1, "close history file");
  123. if (Map->hist_fp != NULL)
  124. fclose(Map->hist_fp);
  125. }
  126. /* close level 1 files / data sources if not head_only */
  127. if (!Map->head_only) {
  128. if (((*Close_array[Map->format][1]) (Map)) != 0) {
  129. G_warning(_("Unable to close vector <%s>"),
  130. Vect_get_full_name(Map));
  131. return 1;
  132. }
  133. }
  134. G_free(Map->name);
  135. G_free(Map->mapset);
  136. G_free(Map->location);
  137. G_free(Map->gisdbase);
  138. Map->open = VECT_CLOSED_CODE;
  139. return 0;
  140. }