1 /*!
2    \file lib/vector/Vlib/close_ogr.c
3 
4    \brief Vector library - Close map (OGR)
5 
6    Higher level functions for reading/writing/manipulating vectors.
7 
8    (C) 2001-2009, 2012 by the GRASS Development Team
9 
10    This program is free software under the GNU General Public License
11    (>=v2).  Read the file COPYING that comes with GRASS for details.
12 
13    \author Original author CERL, probably Dave Gerdes or Mike Higgins.
14    \author Update to GRASS 5.7 Radim Blazek and Piero Cavalieri.
15 */
16 
17 #include <stdlib.h>
18 #include <grass/vector.h>
19 #include <grass/glocale.h>
20 
21 #ifdef HAVE_OGR
22 #include <ogr_api.h>
23 #endif
24 
25 #include "local_proto.h"
26 
27 /*!
28   \brief Close vector map (OGR dsn & layer) on level 1
29 
30   \param Map pointer to Map_info structure
31 
32   \return 0 on success
33   \return non-zero on error
34 */
V1_close_ogr(struct Map_info * Map)35 int V1_close_ogr(struct Map_info *Map)
36 {
37 #ifdef HAVE_OGR
38     struct Format_info_ogr *ogr_info;
39 
40     G_debug(3, "V1_close_ogr() name = %s mapset = %s", Map->name, Map->mapset);
41 
42     if (!VECT_OPEN(Map))
43 	return -1;
44 
45     ogr_info = &(Map->fInfo.ogr);
46     if (Map->format != GV_FORMAT_OGR_DIRECT &&
47         (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW)) {
48         /* write header */
49         Vect__write_head(Map);
50         if (G_find_file2("", "OGR", G_mapset())) {
51             /* write frmt file for created PG-link */
52             Vect_save_frmt(Map);
53         }
54     }
55 
56     if (ogr_info->feature_cache)
57 	OGR_F_Destroy(ogr_info->feature_cache);
58 
59     /* destroy OGR datasource */
60     OGR_DS_Destroy(ogr_info->ds);
61 
62     Vect__free_cache(&(ogr_info->cache));
63 
64     /* close DB connection (for atgtributes) */
65     if (ogr_info->dbdriver) {
66 	db_close_database_shutdown_driver(ogr_info->dbdriver);
67     }
68 
69     G_free(ogr_info->driver_name);
70     G_free(ogr_info->dsn);
71     G_free(ogr_info->layer_name);
72     if (ogr_info->layer_options)
73 	G_free_tokens(ogr_info->layer_options);
74 
75     return 0;
76 #else
77     G_fatal_error(_("GRASS is not compiled with OGR support"));
78     return -1;
79 #endif
80 }
81 
82 /*!
83   \brief Close vector map on topological level (write out fidx file)
84 
85   \param Map pointer to Map_info structure
86 
87   \return 0 on success
88   \return non-zero on error
89 */
V2_close_ogr(struct Map_info * Map)90 int V2_close_ogr(struct Map_info *Map)
91 {
92 #ifdef HAVE_OGR
93     struct Format_info_ogr *ogr_info;
94 
95     G_debug(3, "V2_close_ogr() name = %s mapset = %s", Map->name, Map->mapset);
96 
97     if (!VECT_OPEN(Map))
98 	return -1;
99 
100     ogr_info = &(Map->fInfo.ogr);
101 
102     /* write fidx for maps in the current mapset */
103     if (Vect_save_fidx(Map, &(ogr_info->offset)) != 1)
104 	G_warning(_("Unable to save feature index file for vector map <%s>"),
105 		  Map->name);
106 
107     Vect__free_offset(&(ogr_info->offset));
108 
109     return 0;
110 #else
111     G_fatal_error(_("GRASS is not compiled with OGR support"));
112     return -1;
113 #endif
114 }
115