GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
close_ogr.c
Go to the documentation of this file.
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 */
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) {
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 */
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 }
int V1_close_ogr(struct Map_info *Map)
Close vector map (OGR dsn & layer) on level 1.
Definition: close_ogr.c:35
const char * G_find_file2(const char *, const char *, const char *)
Searches for a file from the mapset search list or in a specified mapset. (look but don&#39;t touch) ...
Definition: find_file.c:247
char * name
Map name (for 4.0)
Definition: dig_structs.h:1332
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int Vect_save_frmt(struct Map_info *)
Save format definition file for vector map.
OGRDataSourceH ds
Pointer to OGRDataSource.
Definition: dig_structs.h:542
struct Format_info_cache cache
Lines cache for reading feature.
Definition: dig_structs.h:573
struct Format_info_offset offset
Offset list used for building pseudo-topology.
Definition: dig_structs.h:589
void Vect__free_cache(struct Format_info_cache *cache)
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
char * dsn
OGR datasource name.
Definition: dig_structs.h:525
struct Format_info fInfo
Format info for non-native formats.
Definition: dig_structs.h:1415
char ** layer_options
Array of OGR layer options.
Definition: dig_structs.h:568
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
Definition: db.c:62
#define GV_MODE_RW
Read-write vector map open mode.
Definition: dig_defines.h:108
#define VECT_OPEN(Map)
Check if vector map is open.
Definition: dig_defines.h:136
void Vect__free_offset(struct Format_info_offset *offset)
#define GV_FORMAT_OGR_DIRECT
OGR format (direct access)
Definition: dig_defines.h:87
int V2_close_ogr(struct Map_info *Map)
Close vector map on topological level (write out fidx file)
Definition: close_ogr.c:90
Non-native format info (OGR)
Definition: dig_structs.h:516
struct Format_info_ogr ogr
OGR info.
Definition: dig_structs.h:722
char * mapset
Mapset name.
Definition: dig_structs.h:1336
Vector map info.
Definition: dig_structs.h:1259
char * layer_name
OGR layer name.
Definition: dig_structs.h:529
OGRFeatureH feature_cache
Cache to avoid repeated reading (level 2)
Definition: dig_structs.h:581
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
void G_free_tokens(char **)
Free memory allocated to tokens.
Definition: gis/token.c:204
void G_warning(const char *,...) __attribute__((format(printf
char * driver_name
OGR driver name.
Definition: dig_structs.h:521
dbDriver * dbdriver
Open DB driver when writing attributes.
Definition: dig_structs.h:559
#define _(str)
Definition: glocale.h:10
int format
Map format (native, ogr, postgis)
Definition: dig_structs.h:1271
#define GV_MODE_WRITE
Write vector map open mode.
Definition: dig_defines.h:106
int Vect__write_head(const struct Map_info *)
Writes head information to text file (GV_HEAD_ELEMENT)
int Vect_save_fidx(struct Map_info *, struct Format_info_offset *)
Save feature index file for vector map.
Definition: build_ogr.c:116
int G_debug(int, const char *,...) __attribute__((format(printf