close_ogr.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /*!
  18. \brief Close vector map (OGR dsn & layer) on level 1
  19. \param Map pointer to Map_info structure
  20. \return 0 on success
  21. \return non-zero on error
  22. */
  23. int V1_close_ogr(struct Map_info *Map)
  24. {
  25. #ifdef HAVE_OGR
  26. int i;
  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. /* destroy lines in cache */
  46. for (i = 0; i < ogr_info->cache.lines_alloc; i++) {
  47. Vect_destroy_line_struct(ogr_info->cache.lines[i]);
  48. }
  49. G_free(ogr_info->cache.lines);
  50. G_free(ogr_info->cache.lines_types);
  51. /* close DB connection (for atgtributes) */
  52. if (ogr_info->dbdriver) {
  53. db_close_database_shutdown_driver(ogr_info->dbdriver);
  54. }
  55. G_free(ogr_info->driver_name);
  56. G_free(ogr_info->dsn);
  57. G_free(ogr_info->layer_name);
  58. if (ogr_info->layer_options)
  59. G_free_tokens(ogr_info->layer_options);
  60. return 0;
  61. #else
  62. G_fatal_error(_("GRASS is not compiled with OGR support"));
  63. return -1;
  64. #endif
  65. }
  66. /*!
  67. \brief Close vector map on topological level (write out fidx file)
  68. \param Map pointer to Map_info structure
  69. \return 0 on success
  70. \return non-zero on error
  71. */
  72. int V2_close_ogr(struct Map_info *Map)
  73. {
  74. #ifdef HAVE_OGR
  75. struct Format_info_ogr *ogr_info;
  76. G_debug(3, "V2_close_ogr() name = %s mapset = %s", Map->name, Map->mapset);
  77. if (!VECT_OPEN(Map))
  78. return -1;
  79. ogr_info = &(Map->fInfo.ogr);
  80. /* write fidx for maps in the current mapset */
  81. if (Vect_save_fidx(Map, &(ogr_info->offset)) != 1)
  82. G_warning(_("Unable to save feature index file for vector map <%s>"),
  83. Map->name);
  84. G_free(ogr_info->offset.array);
  85. return 0;
  86. #else
  87. G_fatal_error(_("GRASS is not compiled with OGR support"));
  88. return -1;
  89. #endif
  90. }