GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
close_ogr.c
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * MODULE: Vector library
5 *
6 * AUTHOR(S): Radim Blazek, Piero Cavalieri
7 *
8 * PURPOSE: Higher level functions for reading/writing/manipulating vectors.
9 *
10 * COPYRIGHT: (C) 2001 by the GRASS Development Team
11 *
12 * This program is free software under the GNU General Public
13 * License (>=v2). Read the file COPYING that comes with GRASS
14 * for details.
15 *
16 *****************************************************************************/
17 #include <grass/Vect.h>
18 #include <stdlib.h>
19 
20 #ifdef HAVE_OGR
21 #include <ogr_api.h>
22 
23 /*
24  ** return 0 on success
25  ** non-zero on error
26  */
27 int V1_close_ogr(struct Map_info *Map)
28 {
29  int i;
30 
31  if (!VECT_OPEN(Map))
32  return -1;
33 
34  if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW)
35  Vect__write_head(Map);
36 
37  if (Map->fInfo.ogr.feature_cache)
38  OGR_F_Destroy(Map->fInfo.ogr.feature_cache);
39 
40  OGR_DS_Destroy(Map->fInfo.ogr.ds);
41 
42  for (i = 0; i < Map->fInfo.ogr.lines_alloc; i++) {
43  Vect_destroy_line_struct(Map->fInfo.ogr.lines[i]);
44  }
45 
46  free(Map->fInfo.ogr.lines);
47  free(Map->fInfo.ogr.lines_types);
48 
49  free(Map->fInfo.ogr.dsn);
50  free(Map->fInfo.ogr.layer_name);
51 
52  return 0;
53 }
54 
55 /*
56  * Write OGR specific files (fidx)
57  *
58  * return 0 on success
59  * non-zero on error
60  */
61 int V2_close_ogr(struct Map_info *Map)
62 {
63  char fname[1000], elem[1000];
64  char buf[5];
65  long length = 9;
66  GVFILE fp;
67  struct Port_info port;
68 
69  G_debug(3, "V2_close_ogr()");
70 
71  if (!VECT_OPEN(Map))
72  return -1;
73 
74  if (strcmp(Map->mapset, G_mapset()) == 0 && Map->support_updated &&
75  Map->plus.built == GV_BUILD_ALL) {
76  sprintf(elem, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
77  G__file_name(fname, elem, "fidx", Map->mapset);
78  G_debug(4, "Open fidx: %s", fname);
79  dig_file_init(&fp);
80  fp.file = fopen(fname, "w");
81  if (fp.file == NULL) {
82  G_warning("Can't open fidx file for write: %s\n", fname);
83  return 1;
84  }
85 
87  dig_set_cur_port(&port);
88 
89  /* Header */
90  /* bytes 1 - 5 */
91  buf[0] = 5;
92  buf[1] = 0;
93  buf[2] = 5;
94  buf[3] = 0;
95  buf[4] = (char)dig__byte_order_out();
96  if (0 >= dig__fwrite_port_C(buf, 5, &fp))
97  return (1);
98 
99  /* bytes 6 - 9 : header size */
100  if (0 >= dig__fwrite_port_L(&length, 1, &fp))
101  return (1);
102 
103  /* Body */
104  /* number of records */
105  if (0 >= dig__fwrite_port_I(&(Map->fInfo.ogr.offset_num), 1, &fp))
106  return (1);
107 
108  /* offsets */
109  if (0 >= dig__fwrite_port_I(Map->fInfo.ogr.offset,
110  Map->fInfo.ogr.offset_num, &fp))
111  return (1);
112 
113  fclose(fp.file);
114 
115  }
116 
117  free(Map->fInfo.ogr.offset);
118 
119  return 0;
120 }
121 #endif
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
int V1_close_ogr(struct Map_info *Map)
Definition: close_ogr.c:27
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int dig_set_cur_port(struct Port_info *port)
Definition: portable.c:640
void dig_init_portable(struct Port_info *port, int byte_order)
Definition: portable.c:568
int dig__fwrite_port_I(int *buf, int cnt, GVFILE *fp)
Definition: portable.c:465
char * G__file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:33
int dig__fwrite_port_L(long *buf, int cnt, GVFILE *fp)
Definition: portable.c:422
int V2_close_ogr(struct Map_info *Map)
Definition: close_ogr.c:61
int Vect__write_head(struct Map_info *Map)
Writes head information to text file.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
tuple Map
Definition: render.py:1310
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
void dig_file_init(GVFILE *file)
Initialize GVFILE.
Definition: file.c:168
fclose(fd)
void free(void *)
int dig__fwrite_port_C(char *buf, int cnt, GVFILE *fp)
Definition: portable.c:558
int dig__byte_order_out()
Definition: portable.c:646
int Vect_destroy_line_struct(struct line_pnts *p)
Frees all memory associated with a struct line_pnts, including the struct itself. ...
Definition: line.c:90