GRASS 8 Programmer's Manual 8.6.0dev(2026)-5f4f7ad06c
Loading...
Searching...
No Matches
open_ogr.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/open_ogr.c
3
4 \brief Vector library - Open OGR layer as vector map layer
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 (C) 2001-2010 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 David D. Gray.
15 \author Update to GRASS 7.0 Martin Landa <landa.martin gmail.com> (2009)
16 */
17
18#include <unistd.h>
19#include <string.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22
23#include <grass/vector.h>
24#include <grass/dbmi.h>
25#include <grass/glocale.h>
26
27#include <ogr_api.h>
28
29/*!
30 \brief Open existing OGR layer on non-topological level
31
32 Note: Map->name, Map->mapset, Map->fInfo.ogr.dsn and
33 Map->fInfo.ogr.layer_name must be set before.
34
35 \param[in,out] Map pointer to Map_info structure
36 \param update TRUE for write mode, otherwise read-only
37
38 \return 0 success
39 \return -1 error
40 */
41int V1_open_old_ogr(struct Map_info *Map, int update)
42{
43 int i, layer, nLayers;
44
46
51
54
55 ogr_info = &(Map->fInfo.ogr);
56 if (!ogr_info->dsn) {
57 G_fatal_error(_("OGR datasource not defined"));
58 return -1;
59 }
60
61 if (!ogr_info->layer_name) {
62 G_fatal_error(_("OGR layer not defined"));
63 return -1;
64 }
65
66 G_debug(2, "V1_open_old_ogr(): dsn = %s layer = %s", ogr_info->dsn,
67 ogr_info->layer_name);
68
70
71 /* open data source handle */
73 if (Ogr_ds == NULL)
74 G_fatal_error(_("Unable to open OGR data source '%s'"), ogr_info->dsn);
75 ogr_info->ds = Ogr_ds;
76
77 /* get layer number */
78 layer = -1;
80 G_debug(2, "%d layers found in data source", nLayers);
81
82 for (i = 0; i < nLayers; i++) {
85 if (strcmp(OGR_FD_GetName(Ogr_featuredefn), ogr_info->layer_name) ==
86 0) {
88 layer = i;
89 break;
90 }
91 }
92 if (layer == -1) {
94 G_fatal_error(_("OGR layer <%s> not found"), ogr_info->layer_name);
95 }
96 G_debug(2, "OGR layer %d opened", layer);
97
98 ogr_info->layer = Ogr_layer;
99 if (update && OGR_L_TestCapability(ogr_info->layer, OLCTransactions) &&
102 G_warning(_("OGR transaction with layer <%s> failed to start"),
103 ogr_info->layer_name);
104 return -1;
105 }
106
107 switch (Ogr_geom_type) {
108 case wkbPoint25D:
109 case wkbLineString25D:
110 case wkbPolygon25D:
111 case wkbMultiPoint25D:
115 Map->head.with_z = WITH_Z;
116 break;
117 default:
118 Map->head.with_z = WITHOUT_Z;
119 break;
120 }
121
122 ogr_info->cache.fid = -1; /* FID >= 0 */
123
124 return 0;
125}
126
127/*!
128 \brief Open existing OGR layer on topological level
129
130 This functions reads feature index (fidx) file required for
131 pseudo-topology.
132
133 \param[in,out] Map pointer to Map_info structure
134
135 \return 0 success
136 \return -1 error
137 */
139{
140 G_debug(3, "V2_open_old_ogr(): name = %s mapset = %s", Map->name,
141 Map->mapset);
142
143 if (Vect_open_fidx(Map, &(Map->fInfo.ogr.offset)) != 0) {
144 const char *map_name = Vect_get_full_name(Map);
145 G_warning(_("Unable to open feature index file for vector map <%s>"),
146 map_name);
147 G_free((void *)map_name);
148 G_zero(&(Map->fInfo.ogr.offset), sizeof(struct Format_info_offset));
149 }
150
151 Map->fInfo.ogr.next_line = 1; /* reset feature cache */
152
153 return 0;
154}
155
156/*!
157 \brief Prepare OGR datasource for creating new OGR layer (level 1)
158
159 New OGR layer is created when writing features by
160 Vect_wrile_line().
161
162 \param[out] Map pointer to Map_info structure
163 \param name name of OGR layer to create
164 \param with_z WITH_Z for 3D vector data otherwise WITHOUT_Z
165
166 \return 0 success
167 \return -1 error
168 */
169int V1_open_new_ogr(struct Map_info *Map, const char *name, int with_z)
170{
171 int i, nlayers;
172
174
179
181
182 ogr_info = &(Map->fInfo.ogr);
183
184 G_debug(1, "V1_open_new_ogr(): name = %s with_z = %d", name, with_z);
185 Ogr_driver = OGRGetDriverByName(ogr_info->driver_name);
186 if (!Ogr_driver) {
187 G_warning(_("Unable to get OGR driver <%s>"), ogr_info->driver_name);
188 return -1;
189 }
190 ogr_info->driver = Ogr_driver;
191
192 /* TODO: creation options */
194 if (!Ogr_ds) {
195 G_warning(_("Unable to create OGR data source '%s'"), ogr_info->dsn);
196 return -1;
197 }
198 ogr_info->ds = Ogr_ds;
199
201 for (i = 0; i < nlayers; i++) {
205 if (G_get_overwrite()) {
206 G_warning(
207 _("OGR layer <%s> already exists and will be overwritten"),
208 ogr_info->layer_name);
209
211 G_warning(_("Unable to delete OGR layer <%s>"),
212 ogr_info->layer_name);
213 return -1;
214 }
215 }
216 else {
218 _("OGR layer <%s> already exists in datasource '%s'"),
219 ogr_info->layer_name, ogr_info->dsn);
220 }
221 ogr_info->layer = NULL;
222 break;
223 }
224 }
225
226 return 0;
227}
228
229/*!
230 \brief Open feature index file
231
232 \param[in,out] Map pointer to Map_info struct
233 \param[out] offset pointer to Format_info_offset (OGR or PG)
234
235 \return 0 on success
236 \return -1 on error
237 */
239{
240 char elem[GPATH_MAX];
241 char buf[5]; /* used for format version */
242 long length;
244
245 struct gvfile fp;
246 struct Port_info port;
247
248 G_debug(1, "Vect_open_fidx(): name = %s mapset = %s format = %d", Map->name,
249 Map->mapset, Map->format);
250
251 snprintf(elem, sizeof(elem), "%s/%s", GV_DIRECTORY, Map->name);
252 dig_file_init(&fp);
253 fp.file = G_fopen_old(elem, GV_FIDX_ELEMENT, Map->mapset);
254 if (fp.file == NULL) {
255 const char *map_name = Vect_get_full_name(Map);
256 G_debug(1, "unable to open fidx file for vector map <%s>", map_name);
257 G_free((void *)map_name);
258 return -1;
259 }
260
261 /* Header */
262 if (0 >= dig__fread_port_C(buf, 5, &fp)) {
263 fclose(fp.file);
264 return -1;
265 }
266 Version_Major = buf[0];
267 Version_Minor = buf[1];
268 Back_Major = buf[2];
269 Back_Minor = buf[3];
270 byte_order = buf[4];
271
272 /* check version numbers */
273 if (Version_Major > 5 || Version_Minor > 0) {
274 if (Back_Major > 5 || Back_Minor > 0) {
275 G_fatal_error(_("Feature index format version %d.%d is not "
276 "supported by this release."
277 " Try to rebuild topology or upgrade GRASS."),
279 return -1;
280 }
281 G_warning(_("Your GRASS version does not fully support feature index "
282 "format %d.%d of the vector."
283 " Consider to rebuild topology or upgrade GRASS."),
285 }
286
288 dig_set_cur_port(&port);
289
290 /* Body */
291 /* bytes 6 - 9 : header size */
292 if (0 >= dig__fread_port_L(&length, 1, &fp)) {
293 fclose(fp.file);
294 return -1;
295 }
296 G_debug(4, " header size %ld", length);
297
298 G_fseek(fp.file, length, SEEK_SET);
299
300 /* number of records */
301 if (0 >= dig__fread_port_I(&(offset->array_num), 1, &fp)) {
302 fclose(fp.file);
303 return -1;
304 }
305
306 /* alloc space */
307 offset->array = (int *)G_malloc(offset->array_num * sizeof(int));
308 offset->array_alloc = offset->array_num;
309
310 /* offsets */
311 if (0 >= dig__fread_port_I(offset->array, offset->array_num, &fp)) {
312 fclose(fp.file);
313 return -1;
314 }
315
316 fclose(fp.file);
317
318 G_debug(3, "%d records read from fidx", offset->array_num);
319
320 return 0;
321}
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
Main header of GRASS DataBase Management Interface.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition gis/zero.c:23
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:147
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:139
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
Definition gis/seek.c:50
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
Definition gis/open.c:253
int G_get_overwrite(void)
Get overwrite value.
Definition parser.c:959
int G_debug(int, const char *,...) __attribute__((format(printf
const char * Vect_get_full_name(struct Map_info *)
Get fully qualified name of vector map.
#define GV_DIRECTORY
Name of vector directory.
Definition dig_defines.h:8
#define WITH_Z
#define GV_FIDX_ELEMENT
External format (OGR), feature index.
Definition dig_defines.h:26
#define WITHOUT_Z
2D/3D vector data
void dig_init_portable(struct Port_info *, int)
Set Port_info structure to byte order of file.
Definition portable.c:900
int dig__fread_port_L(long *, size_t, struct gvfile *)
Read longs from the Portable Vector Format.
Definition portable.c:262
int dig_set_cur_port(struct Port_info *)
Set current Port_info structure.
Definition portable.c:996
int dig__fread_port_C(char *, size_t, struct gvfile *)
Read chars from the Portable Vector Format.
Definition portable.c:511
int dig__fread_port_I(int *, size_t, struct gvfile *)
Read integers from the Portable Vector Format.
Definition portable.c:345
void dig_file_init(struct gvfile *file)
Initialize gvfile structure.
Definition file.c:171
#define GPATH_MAX
Definition gis.h:199
#define FALSE
Definition gis.h:82
#define _(str)
Definition glocale.h:10
const char * name
Definition named_colr.c:6
int V1_open_new_ogr(struct Map_info *Map, const char *name, int with_z)
Prepare OGR datasource for creating new OGR layer (level 1)
Definition open_ogr.c:169
int Vect_open_fidx(struct Map_info *Map, struct Format_info_offset *offset)
Open feature index file.
Definition open_ogr.c:238
int V2_open_old_ogr(struct Map_info *Map)
Open existing OGR layer on topological level.
Definition open_ogr.c:138
int V1_open_old_ogr(struct Map_info *Map, int update)
Open existing OGR layer on non-topological level.
Definition open_ogr.c:41
Data structure used for building pseudo-topology.
int * array
Offset list.
int array_alloc
Space allocated for offset list.
int array_num
Number of items in offset list.
Non-native format info (OGR)
OGRLayerH layer
Pointer to OGRLayer.
struct Format_info_offset offset
Offset list used for building pseudo-topology.
Vector map info.
Portability info.
int byte_order
File byte order.
File definition.
Definition dig_structs.h:92
FILE * file
File descriptor.
Definition dig_structs.h:96