GRASS Programmer's Manual  6.5.svn(2014)-r66266
 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  OGRwkbGeometryType Ogr_geom_type;
52 
53  if (update) {
54  G_fatal_error(_("OGR format cannot be updated"));
55  return -1;
56  }
57 
58  G_debug(2, "V1_open_old_ogr(): dsn = %s layer = %s", Map->fInfo.ogr.dsn,
59  Map->fInfo.ogr.layer_name);
60 
61  OGRRegisterAll();
62 
63  /*Data source handle */
64  Ogr_ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
65  if (Ogr_ds == NULL)
66  G_fatal_error(_("Unable to open OGR data source '%s'"),
67  Map->fInfo.ogr.dsn);
68  Map->fInfo.ogr.ds = Ogr_ds;
69 
70  /* Layer number */
71  layer = -1;
72  nLayers = OGR_DS_GetLayerCount(Ogr_ds);
73  G_debug(2, "%d layers found in data source", nLayers);
74 
75  for (i = 0; i < nLayers; i++) {
76  Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
77  Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
78  if (strcmp(OGR_FD_GetName(Ogr_featuredefn), Map->fInfo.ogr.layer_name) == 0) {
79  Ogr_geom_type = OGR_FD_GetGeomType(Ogr_featuredefn);
80  layer = i;
81  break;
82  }
83  }
84  if (layer == -1) {
85  OGR_DS_Destroy(Ogr_ds);
86  G_fatal_error(_("Unable to open layer <%s>"),
87  Map->fInfo.ogr.layer_name);
88  }
89  G_debug(2, "OGR layer %d opened", layer);
90 
91  Map->fInfo.ogr.layer = Ogr_layer;
92 
93  Map->fInfo.ogr.lines = NULL;
94  Map->fInfo.ogr.lines_types = NULL;
95  Map->fInfo.ogr.lines_alloc = 0;
96  Map->fInfo.ogr.lines_num = 0;
97  Map->fInfo.ogr.lines_next = 0;
98 
99  switch(Ogr_geom_type) {
100  case wkbPoint25D: case wkbLineString25D: case wkbPolygon25D:
101  case wkbMultiPoint25D: case wkbMultiLineString25D: case wkbMultiPolygon25D:
102  case wkbGeometryCollection25D:
103  Map->head.with_z = WITH_Z;
104  break;
105  default:
106  Map->head.with_z = WITHOUT_Z;
107  break;
108  }
109 
110  Map->fInfo.ogr.feature_cache = NULL;
111  Map->fInfo.ogr.feature_cache_id = -1; /* FID >= 0 */
112 
113  return (0);
114 }
115 
124 int V2_open_old_ogr(struct Map_info *Map)
125 {
126  char elem[GPATH_MAX];
127  char buf[5]; /* used for format version */
128  long length;
129  GVFILE fp;
130  struct Port_info port;
131  int Version_Major, Version_Minor, Back_Major, Back_Minor, byte_order;
132 
133  G_debug(3, "V2_open_old_ogr()");
134 
135  sprintf(elem, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
136  dig_file_init(&fp);
137  fp.file = G_fopen_old(elem, "fidx", Map->mapset);
138  if (fp.file == NULL) {
139  G_warning(_("Unable to open fidx file for vector map <%s@%s>"),
140  Map->name, Map->mapset);
141  return -1;
142  }
143 
144  /* Header */
145  if (0 >= dig__fread_port_C(buf, 5, &fp))
146  return (-1);
147  Version_Major = buf[0];
148  Version_Minor = buf[1];
149  Back_Major = buf[2];
150  Back_Minor = buf[3];
151  byte_order = buf[4];
152 
153 
154  /* check version numbers */
155  if (Version_Major > 5 || Version_Minor > 0) {
156  if (Back_Major > 5 || Back_Minor > 0) {
157  G_fatal_error(_("Feature index format version %d.%d is not supported by this release."
158  " Try to rebuild topology or upgrade GRASS."),
159  Version_Major, Version_Minor);
160  return (-1);
161  }
162  G_warning(_("Your GRASS version does not fully support feature index format %d.%d of the vector."
163  " Consider to rebuild topology or upgrade GRASS."),
164  Version_Major, Version_Minor);
165  }
166 
167  dig_init_portable(&port, byte_order);
168  dig_set_cur_port(&port);
169 
170  /* Body */
171  /* bytes 6 - 9 : header size */
172  if (0 >= dig__fread_port_L(&length, 1, &fp))
173  return (-1);
174  G_debug(3, " header size %ld", length);
175 
176  fseek(fp.file, length, SEEK_SET);
177 
178  /* number of records */
179  if (0 >= dig__fread_port_I(&(Map->fInfo.ogr.offset_num), 1, &fp))
180  return (-1);
181 
182  /* alloc space */
183  Map->fInfo.ogr.offset =
184  (int *)G_malloc(Map->fInfo.ogr.offset_num * sizeof(int));
185  Map->fInfo.ogr.offset_alloc = Map->fInfo.ogr.offset_num;
186 
187  /* offsets */
188  if (0 >= dig__fread_port_I(Map->fInfo.ogr.offset,
189  Map->fInfo.ogr.offset_num, &fp))
190  return (-1);
191 
192  fclose(fp.file);
193 
194  G_debug(3, "%d records read from fidx", Map->fInfo.ogr.offset_num);
195 
196 
197  Map->fInfo.ogr.next_line = 1;
198 
199  return 0;
200 }
201 
202 #endif
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int dig_set_cur_port(struct Port_info *port)
Definition: portable.c:640
#define FALSE
Definition: dbfopen.c:117
int V1_open_old_ogr(struct Map_info *Map, int update)
Open existing vector map.
Definition: open_ogr.c:45
void dig_init_portable(struct Port_info *port, int byte_order)
Definition: portable.c:568
int dig__fread_port_L(long *buf, int cnt, GVFILE *fp)
Definition: portable.c:137
int V2_open_old_ogr(struct Map_info *Map)
Open OGR specific level 2 files (feature index)
Definition: open_ogr.c:124
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int dig__fread_port_C(char *buf, int cnt, GVFILE *fp)
Definition: portable.c:347
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)
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:226
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int dig__fread_port_I(int *buf, int cnt, GVFILE *fp)
Definition: portable.c:207