close_ogr.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*!
  2. \file lib/vector/Vlib/close_ogr.c
  3. \brief Vector library - Close map (OGR)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009, 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 Piero Cavalieri.
  10. */
  11. #include <stdlib.h>
  12. #include <grass/vector.h>
  13. #include <grass/glocale.h>
  14. #ifdef HAVE_OGR
  15. #include <ogr_api.h>
  16. #endif
  17. #include "local_proto.h"
  18. /*!
  19. \brief Close vector map (OGR dsn & layer) on level 1
  20. \param Map pointer to Map_info structure
  21. \return 0 on success
  22. \return non-zero on error
  23. */
  24. int V1_close_ogr(struct Map_info *Map)
  25. {
  26. #ifdef HAVE_OGR
  27. struct Format_info_ogr *ogr_info;
  28. G_debug(3, "V1_close_ogr() name = %s mapset = %s", Map->name, Map->mapset);
  29. if (!VECT_OPEN(Map))
  30. return -1;
  31. ogr_info = &(Map->fInfo.ogr);
  32. if (Map->format != GV_FORMAT_OGR_DIRECT &&
  33. (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW)) {
  34. /* write header */
  35. Vect__write_head(Map);
  36. if (G_find_file2("", "OGR", G_mapset())) {
  37. /* write frmt file for created PG-link */
  38. Vect_save_frmt(Map);
  39. }
  40. }
  41. if (ogr_info->feature_cache)
  42. OGR_F_Destroy(ogr_info->feature_cache);
  43. /* destroy OGR datasource */
  44. OGR_DS_Destroy(ogr_info->ds);
  45. Vect__free_cache(&(ogr_info->cache));
  46. /* close DB connection (for atgtributes) */
  47. if (ogr_info->dbdriver) {
  48. db_close_database_shutdown_driver(ogr_info->dbdriver);
  49. }
  50. G_free(ogr_info->driver_name);
  51. G_free(ogr_info->dsn);
  52. G_free(ogr_info->layer_name);
  53. if (ogr_info->layer_options)
  54. G_free_tokens(ogr_info->layer_options);
  55. return 0;
  56. #else
  57. G_fatal_error(_("GRASS is not compiled with OGR support"));
  58. return -1;
  59. #endif
  60. }
  61. /*!
  62. \brief Close vector map on topological level (write out fidx file)
  63. \param Map pointer to Map_info structure
  64. \return 0 on success
  65. \return non-zero on error
  66. */
  67. int V2_close_ogr(struct Map_info *Map)
  68. {
  69. #ifdef HAVE_OGR
  70. struct Format_info_ogr *ogr_info;
  71. G_debug(3, "V2_close_ogr() name = %s mapset = %s", Map->name, Map->mapset);
  72. if (!VECT_OPEN(Map))
  73. return -1;
  74. ogr_info = &(Map->fInfo.ogr);
  75. /* write fidx for maps in the current mapset */
  76. if (Vect_save_fidx(Map, &(ogr_info->offset)) != 1)
  77. G_warning(_("Unable to save feature index file for vector map <%s>"),
  78. Map->name);
  79. Vect__free_offset(&(ogr_info->offset));
  80. return 0;
  81. #else
  82. G_fatal_error(_("GRASS is not compiled with OGR support"));
  83. return -1;
  84. #endif
  85. }