GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
open_ogr.c
Go to the documentation of this file.
1 
21 #include <unistd.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 
26 #include <grass/Vect.h>
27 #include <grass/gis.h>
28 #include <grass/glocale.h>
29 
30 #ifdef HAVE_OGR
31 #include <ogr_api.h>
32 
45 int V1_open_old_ogr(struct Map_info *Map, int update)
46 {
47  int i, layer, nLayers;
48  OGRDataSourceH Ogr_ds;
49  OGRLayerH Ogr_layer = NULL;
50  OGRFeatureDefnH Ogr_featuredefn;
51 
52  if (update) {
53  G_warning(_("OGR format cannot be updated"));
54  return -1;
55  }
56 
57  G_debug(2, "V1_open_old_ogr(): dsn = %s layer = %s", Map->fInfo.ogr.dsn,
58  Map->fInfo.ogr.layer_name);
59 
60  OGRRegisterAll();
61 
62  /*Data source handle */
63  Ogr_ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
64  if (Ogr_ds == NULL)
65  G_fatal_error(_("Unable to open OGR data source '%s'"),
66  Map->fInfo.ogr.dsn);
67  Map->fInfo.ogr.ds = Ogr_ds;
68 
69  /* Layer number */
70  layer = -1;
71  nLayers = OGR_DS_GetLayerCount(Ogr_ds);
72  G_debug(2, "%d layers found in data source", nLayers);
73 
74  for (i = 0; i < nLayers; i++) {
75  Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
76  Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
77  if (strcmp(OGR_FD_GetName(Ogr_featuredefn), Map->fInfo.ogr.layer_name)
78  == 0) {
79  layer = i;
80  break;
81  }
82  }
83  if (layer == -1) {
84  OGR_DS_Destroy(Ogr_ds);
85  G_fatal_error(_("Unable to open layer <%s>"),
86  Map->fInfo.ogr.layer_name);
87  }
88  G_debug(2, "OGR layer %d opened", layer);
89 
90  Map->fInfo.ogr.layer = Ogr_layer;
91 
92  Map->fInfo.ogr.lines = NULL;
93  Map->fInfo.ogr.lines_types = NULL;
94  Map->fInfo.ogr.lines_alloc = 0;
95  Map->fInfo.ogr.lines_num = 0;
96  Map->fInfo.ogr.lines_next = 0;
97 
98  Map->head.with_z = WITHOUT_Z; /* TODO: 3D */
99 
100  Map->fInfo.ogr.feature_cache = NULL;
101  Map->fInfo.ogr.feature_cache_id = -1; /* FID >= 0 */
102 
103  return (0);
104 }
105 
114 int V2_open_old_ogr(struct Map_info *Map)
115 {
116  char elem[GPATH_MAX];
117  char buf[5]; /* used for format version */
118  long length;
119  GVFILE fp;
120  struct Port_info port;
121  int Version_Major, Version_Minor, Back_Major, Back_Minor, byte_order;
122 
123  G_debug(3, "V2_open_old_ogr()");
124 
125  sprintf(elem, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
126  dig_file_init(&fp);
127  fp.file = G_fopen_old(elem, "fidx", Map->mapset);
128  if (fp.file == NULL) {
129  G_warning(_("Unable to open fidx file for vector map <%s@%s>"),
130  Map->name, Map->mapset);
131  return -1;
132  }
133 
134  /* Header */
135  if (0 >= dig__fread_port_C(buf, 5, &fp))
136  return (-1);
137  Version_Major = buf[0];
138  Version_Minor = buf[1];
139  Back_Major = buf[2];
140  Back_Minor = buf[3];
141  byte_order = buf[4];
142 
143 
144  /* check version numbers */
145  if (Version_Major > 5 || Version_Minor > 0) {
146  if (Back_Major > 5 || Back_Minor > 0) {
147  G_fatal_error(_("Feature index format version %d.%d is not supported by this release."
148  " Try to rebuild topology or upgrade GRASS."),
149  Version_Major, Version_Minor);
150  return (-1);
151  }
152  G_warning(_("Your GRASS version does not fully support feature index format %d.%d of the vector."
153  " Consider to rebuild topology or upgrade GRASS."),
154  Version_Major, Version_Minor);
155  }
156 
157  dig_init_portable(&port, byte_order);
158  dig_set_cur_port(&port);
159 
160  /* Body */
161  /* bytes 6 - 9 : header size */
162  if (0 >= dig__fread_port_L(&length, 1, &fp))
163  return (-1);
164  G_debug(3, " header size %ld", length);
165 
166  fseek(fp.file, length, SEEK_SET);
167 
168  /* number of records */
169  if (0 >= dig__fread_port_I(&(Map->fInfo.ogr.offset_num), 1, &fp))
170  return (-1);
171 
172  /* alloc space */
173  Map->fInfo.ogr.offset =
174  (int *)G_malloc(Map->fInfo.ogr.offset_num * sizeof(int));
175  Map->fInfo.ogr.offset_alloc = Map->fInfo.ogr.offset_num;
176 
177  /* offsets */
178  if (0 >= dig__fread_port_I(Map->fInfo.ogr.offset,
179  Map->fInfo.ogr.offset_num, &fp))
180  return (-1);
181 
182  fclose(fp.file);
183 
184  G_debug(3, "%d records read from fidx", Map->fInfo.ogr.offset_num);
185 
186 
187  Map->fInfo.ogr.next_line = 1;
188 
189  return 0;
190 }
191 
192 #endif