close_ogr.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*!
  2. \file close_ogr.c
  3. \brief Vector library - Close map (OGR)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009 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 <grass/config.h>
  12. #include <stdlib.h>
  13. #include <grass/vector.h>
  14. #include <grass/glocale.h>
  15. #ifdef HAVE_OGR
  16. #include <ogr_api.h>
  17. /*!
  18. \brief Close OGR layer
  19. \param Map vector map
  20. \return 0 on success
  21. \return non-zero on error
  22. */
  23. int V1_close_ogr(struct Map_info *Map)
  24. {
  25. int i;
  26. if (!VECT_OPEN(Map))
  27. return -1;
  28. if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW)
  29. Vect__write_head(Map);
  30. if (Map->fInfo.ogr.feature_cache)
  31. OGR_F_Destroy(Map->fInfo.ogr.feature_cache);
  32. OGR_DS_Destroy(Map->fInfo.ogr.ds);
  33. for (i = 0; i < Map->fInfo.ogr.lines_alloc; i++) {
  34. Vect_destroy_line_struct(Map->fInfo.ogr.lines[i]);
  35. }
  36. G_free(Map->fInfo.ogr.lines);
  37. G_free(Map->fInfo.ogr.lines_types);
  38. G_free(Map->fInfo.ogr.dsn);
  39. G_free(Map->fInfo.ogr.layer_name);
  40. return 0;
  41. }
  42. /*!
  43. \brief Write OGR specific files (fidx)
  44. \param Map vector map
  45. \return 0 on success
  46. \return non-zero on error
  47. */
  48. int V2_close_ogr(struct Map_info *Map)
  49. {
  50. char fname[1000], elem[1000];
  51. char buf[5];
  52. long length = 9;
  53. struct gvfile fp;
  54. struct Port_info port;
  55. G_debug(3, "V2_close_ogr()");
  56. if (!VECT_OPEN(Map))
  57. return -1;
  58. if (strcmp(Map->mapset, G_mapset()) == 0 && Map->support_updated &&
  59. Map->plus.built == GV_BUILD_ALL) {
  60. sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name);
  61. G__file_name(fname, elem, "fidx", Map->mapset);
  62. G_debug(4, "Open fidx: %s", fname);
  63. dig_file_init(&fp);
  64. fp.file = fopen(fname, "w");
  65. if (fp.file == NULL) {
  66. G_warning(_("Unable to open fidx file for write <%s>"), fname);
  67. return 1;
  68. }
  69. dig_init_portable(&port, dig__byte_order_out());
  70. dig_set_cur_port(&port);
  71. /* Header */
  72. /* bytes 1 - 5 */
  73. buf[0] = 5;
  74. buf[1] = 0;
  75. buf[2] = 5;
  76. buf[3] = 0;
  77. buf[4] = (char)dig__byte_order_out();
  78. if (0 >= dig__fwrite_port_C(buf, 5, &fp))
  79. return (1);
  80. /* bytes 6 - 9 : header size */
  81. if (0 >= dig__fwrite_port_L(&length, 1, &fp))
  82. return (1);
  83. /* Body */
  84. /* number of records */
  85. if (0 >= dig__fwrite_port_I(&(Map->fInfo.ogr.offset_num), 1, &fp))
  86. return (1);
  87. /* offsets */
  88. if (0 >= dig__fwrite_port_I(Map->fInfo.ogr.offset,
  89. Map->fInfo.ogr.offset_num, &fp))
  90. return (1);
  91. fclose(fp.file);
  92. }
  93. G_free(Map->fInfo.ogr.offset);
  94. return 0;
  95. }
  96. #endif