close_ogr.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*****************************************************************************
  2. *
  3. * MODULE: Vector library
  4. *
  5. * AUTHOR(S): Radim Blazek, Piero Cavalieri
  6. *
  7. * PURPOSE: Higher level functions for reading/writing/manipulating vectors.
  8. *
  9. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <grass/Vect.h>
  17. #include <stdlib.h>
  18. #ifdef HAVE_OGR
  19. #include <ogr_api.h>
  20. /*
  21. ** return 0 on success
  22. ** non-zero on error
  23. */
  24. int V1_close_ogr(struct Map_info *Map)
  25. {
  26. int i;
  27. if (!VECT_OPEN(Map))
  28. return -1;
  29. if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW)
  30. Vect__write_head(Map);
  31. if (Map->fInfo.ogr.feature_cache)
  32. OGR_F_Destroy(Map->fInfo.ogr.feature_cache);
  33. OGR_DS_Destroy(Map->fInfo.ogr.ds);
  34. for (i = 0; i < Map->fInfo.ogr.lines_alloc; i++) {
  35. Vect_destroy_line_struct(Map->fInfo.ogr.lines[i]);
  36. }
  37. free(Map->fInfo.ogr.lines);
  38. free(Map->fInfo.ogr.lines_types);
  39. free(Map->fInfo.ogr.dsn);
  40. free(Map->fInfo.ogr.layer_name);
  41. return 0;
  42. }
  43. /*
  44. * Write OGR specific files (fidx)
  45. *
  46. * return 0 on success
  47. * non-zero on error
  48. */
  49. int V2_close_ogr(struct Map_info *Map)
  50. {
  51. char fname[1000], elem[1000];
  52. char buf[5];
  53. long length = 9;
  54. GVFILE fp;
  55. struct Port_info port;
  56. G_debug(3, "V2_close_ogr()");
  57. if (!VECT_OPEN(Map))
  58. return -1;
  59. if (strcmp(Map->mapset, G_mapset()) == 0 && Map->support_updated &&
  60. Map->plus.built == GV_BUILD_ALL) {
  61. sprintf(elem, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
  62. G__file_name(fname, elem, "fidx", Map->mapset);
  63. G_debug(4, "Open fidx: %s", fname);
  64. dig_file_init(&fp);
  65. fp.file = fopen(fname, "w");
  66. if (fp.file == NULL) {
  67. G_warning("Can't open fidx file for write: %s\n", fname);
  68. return 1;
  69. }
  70. dig_init_portable(&port, dig__byte_order_out());
  71. dig_set_cur_port(&port);
  72. /* Header */
  73. /* bytes 1 - 5 */
  74. buf[0] = 5;
  75. buf[1] = 0;
  76. buf[2] = 5;
  77. buf[3] = 0;
  78. buf[4] = (char)dig__byte_order_out();
  79. if (0 >= dig__fwrite_port_C(buf, 5, &fp))
  80. return (1);
  81. /* bytes 6 - 9 : header size */
  82. if (0 >= dig__fwrite_port_L(&length, 1, &fp))
  83. return (1);
  84. /* Body */
  85. /* number of records */
  86. if (0 >= dig__fwrite_port_I(&(Map->fInfo.ogr.offset_num), 1, &fp))
  87. return (1);
  88. /* offsets */
  89. if (0 >= dig__fwrite_port_I(Map->fInfo.ogr.offset,
  90. Map->fInfo.ogr.offset_num, &fp))
  91. return (1);
  92. fclose(fp.file);
  93. }
  94. free(Map->fInfo.ogr.offset);
  95. return 0;
  96. }
  97. #endif