GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
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 SPDX-FileCopyrightText: 2001-2010 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Original author CERL, probably Dave Gerdes or Mike Higgins.
12 \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
13 \author Update to GRASS 7.0 Martin Landa <landa.martin gmail.com> (2009)
14 */
15
16#include <unistd.h>
17#include <string.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20
21#include <grass/vector.h>
22#include <grass/dbmi.h>
23#include <grass/glocale.h>
24
25#include <ogr_api.h>
26
27/*!
28 \brief Open existing OGR layer on non-topological level
29
30 Note: Map->name, Map->mapset, Map->fInfo.ogr.dsn and
31 Map->fInfo.ogr.layer_name must be set before.
32
33 \param[in,out] Map pointer to Map_info structure
34 \param update TRUE for write mode, otherwise read-only
35
36 \return 0 success
37 \return -1 error
38 */
39int V1_open_old_ogr(struct Map_info *Map, int update)
40{
41 int i, layer, nLayers;
42
44
49
52
53 ogr_info = &(Map->fInfo.ogr);
54 if (!ogr_info->dsn) {
55 G_fatal_error(_("OGR datasource not defined"));
56 return -1;
57 }
58
59 if (!ogr_info->layer_name) {
60 G_fatal_error(_("OGR layer not defined"));
61 return -1;
62 }
63
64 G_debug(2, "V1_open_old_ogr(): dsn = %s layer = %s", ogr_info->dsn,
65 ogr_info->layer_name);
66
68
69 /* open data source handle */
71 if (Ogr_ds == NULL)
72 G_fatal_error(_("Unable to open OGR data source '%s'"), ogr_info->dsn);
73 ogr_info->ds = Ogr_ds;
74
75 /* get layer number */
76 layer = -1;
78 G_debug(2, "%d layers found in data source", nLayers);
79
80 for (i = 0; i < nLayers; i++) {
83 if (strcmp(OGR_FD_GetName(Ogr_featuredefn), ogr_info->layer_name) ==
84 0) {
86 layer = i;
87 break;
88 }
89 }
90 if (layer == -1) {
92 G_fatal_error(_("OGR layer <%s> not found"), ogr_info->layer_name);
93 }
94 G_debug(2, "OGR layer %d opened", layer);
95
96 ogr_info->layer = Ogr_layer;
97 if (update && OGR_L_TestCapability(ogr_info->layer, OLCTransactions) &&
100 G_warning(_("OGR transaction with layer <%s> failed to start"),
101 ogr_info->layer_name);
102 return -1;
103 }
104
105 switch (Ogr_geom_type) {
106 case wkbPoint25D:
107 case wkbLineString25D:
108 case wkbPolygon25D:
109 case wkbMultiPoint25D:
113 Map->head.with_z = WITH_Z;
114 break;
115 default:
116 Map->head.with_z = WITHOUT_Z;
117 break;
118 }
119
120 ogr_info->cache.fid = -1; /* FID >= 0 */
121
122 return 0;
123}
124
125/*!
126 \brief Open existing OGR layer on topological level
127
128 This functions reads feature index (fidx) file required for
129 pseudo-topology.
130
131 \param[in,out] Map pointer to Map_info structure
132
133 \return 0 success
134 \return -1 error
135 */
137{
138 G_debug(3, "V2_open_old_ogr(): name = %s mapset = %s", Map->name,
139 Map->mapset);
140
141 if (Vect_open_fidx(Map, &(Map->fInfo.ogr.offset)) != 0) {
142 const char *map_name = Vect_get_full_name(Map);
143 G_warning(_("Unable to open feature index file for vector map <%s>"),
144 map_name);
145 G_free((void *)map_name);
146 G_zero(&(Map->fInfo.ogr.offset), sizeof(struct Format_info_offset));
147 }
148
149 Map->fInfo.ogr.next_line = 1; /* reset feature cache */
150
151 return 0;
152}
153
154/*!
155 \brief Prepare OGR datasource for creating new OGR layer (level 1)
156
157 New OGR layer is created when writing features by
158 Vect_wrile_line().
159
160 \param[out] Map pointer to Map_info structure
161 \param name name of OGR layer to create
162 \param with_z WITH_Z for 3D vector data otherwise WITHOUT_Z
163
164 \return 0 success
165 \return -1 error
166 */
167int V1_open_new_ogr(struct Map_info *Map, const char *name, int with_z)
168{
169 int i, nlayers;
170
172
177
179
180 ogr_info = &(Map->fInfo.ogr);
181
182 G_debug(1, "V1_open_new_ogr(): name = %s with_z = %d", name, with_z);
183 Ogr_driver = OGRGetDriverByName(ogr_info->driver_name);
184 if (!Ogr_driver) {
185 G_warning(_("Unable to get OGR driver <%s>"), ogr_info->driver_name);
186 return -1;
187 }
188 ogr_info->driver = Ogr_driver;
189
190 /* TODO: creation options */
192 if (!Ogr_ds) {
193 G_warning(_("Unable to create OGR data source '%s'"), ogr_info->dsn);
194 return -1;
195 }
196 ogr_info->ds = Ogr_ds;
197
199 for (i = 0; i < nlayers; i++) {
203 if (G_get_overwrite()) {
204 G_warning(
205 _("OGR layer <%s> already exists and will be overwritten"),
206 ogr_info->layer_name);
207
209 G_warning(_("Unable to delete OGR layer <%s>"),
210 ogr_info->layer_name);
211 return -1;
212 }
213 }
214 else {
216 _("OGR layer <%s> already exists in datasource '%s'"),
217 ogr_info->layer_name, ogr_info->dsn);
218 }
219 ogr_info->layer = NULL;
220 break;
221 }
222 }
223
224 return 0;
225}
226
227/*!
228 \brief Open feature index file
229
230 \param[in,out] Map pointer to Map_info struct
231 \param[out] offset pointer to Format_info_offset (OGR or PG)
232
233 \return 0 on success
234 \return -1 on error
235 */
237{
238 char elem[GPATH_MAX];
239 char buf[5]; /* used for format version */
240 long length;
242
243 struct gvfile fp;
244 struct Port_info port;
245
246 G_debug(1, "Vect_open_fidx(): name = %s mapset = %s format = %d", Map->name,
247 Map->mapset, Map->format);
248
249 snprintf(elem, sizeof(elem), "%s/%s", GV_DIRECTORY, Map->name);
250 dig_file_init(&fp);
251 fp.file = G_fopen_old(elem, GV_FIDX_ELEMENT, Map->mapset);
252 if (fp.file == NULL) {
253 const char *map_name = Vect_get_full_name(Map);
254 G_debug(1, "unable to open fidx file for vector map <%s>", map_name);
255 G_free((void *)map_name);
256 return -1;
257 }
258
259 /* Header */
260 if (0 >= dig__fread_port_C(buf, 5, &fp)) {
261 fclose(fp.file);
262 return -1;
263 }
264 Version_Major = buf[0];
265 Version_Minor = buf[1];
266 Back_Major = buf[2];
267 Back_Minor = buf[3];
268 byte_order = buf[4];
269
270 /* check version numbers */
271 if (Version_Major > 5 || Version_Minor > 0) {
272 if (Back_Major > 5 || Back_Minor > 0) {
273 G_fatal_error(_("Feature index format version %d.%d is not "
274 "supported by this release."
275 " Try to rebuild topology or upgrade GRASS."),
277 return -1;
278 }
279 G_warning(_("Your GRASS version does not fully support feature index "
280 "format %d.%d of the vector."
281 " Consider to rebuild topology or upgrade GRASS."),
283 }
284
286 dig_set_cur_port(&port);
287
288 /* Body */
289 /* bytes 6 - 9 : header size */
290 if (0 >= dig__fread_port_L(&length, 1, &fp)) {
291 fclose(fp.file);
292 return -1;
293 }
294 G_debug(4, " header size %ld", length);
295
296 G_fseek(fp.file, length, SEEK_SET);
297
298 /* number of records */
299 if (0 >= dig__fread_port_I(&(offset->array_num), 1, &fp)) {
300 fclose(fp.file);
301 return -1;
302 }
303
304 /* alloc space */
305 offset->array = (int *)G_malloc(offset->array_num * sizeof(int));
306 offset->array_alloc = offset->array_num;
307
308 /* offsets */
309 if (0 >= dig__fread_port_I(offset->array, offset->array_num, &fp)) {
310 fclose(fp.file);
311 return -1;
312 }
313
314 fclose(fp.file);
315
316 G_debug(3, "%d records read from fidx", offset->array_num);
317
318 return 0;
319}
#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:21
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
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:136
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
Definition gis/seek.c:48
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
Definition gis/open.c:250
int G_get_overwrite(void)
Get overwrite value.
Definition parser.c:957
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:898
int dig__fread_port_L(long *, size_t, struct gvfile *)
Read longs from the Portable Vector Format.
Definition portable.c:260
int dig_set_cur_port(struct Port_info *)
Set current Port_info structure.
Definition portable.c:994
int dig__fread_port_C(char *, size_t, struct gvfile *)
Read chars from the Portable Vector Format.
Definition portable.c:509
int dig__fread_port_I(int *, size_t, struct gvfile *)
Read integers from the Portable Vector Format.
Definition portable.c:343
void dig_file_init(struct gvfile *file)
Initialize gvfile structure.
Definition file.c:169
#define GPATH_MAX
Definition gis.h:196
#define FALSE
Definition gis.h:79
#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:167
int Vect_open_fidx(struct Map_info *Map, struct Format_info_offset *offset)
Open feature index file.
Definition open_ogr.c:236
int V2_open_old_ogr(struct Map_info *Map)
Open existing OGR layer on topological level.
Definition open_ogr.c:136
int V1_open_old_ogr(struct Map_info *Map, int update)
Open existing OGR layer on non-topological level.
Definition open_ogr.c:39
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